To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / helpers / sort_helper.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (6.22 KB)
| 1 | 909:cbb26bc654de | Chris | # encoding: utf-8
|
|---|---|---|---|
| 2 | #
|
||
| 3 | 0:513646585e45 | Chris | # Helpers to sort tables using clickable column headers.
|
| 4 | #
|
||
| 5 | # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005.
|
||
| 6 | # Jean-Philippe Lang, 2009
|
||
| 7 | # License: This source code is released under the MIT license.
|
||
| 8 | #
|
||
| 9 | # - Consecutive clicks toggle the column's sort order.
|
||
| 10 | # - Sort state is maintained by a session hash entry.
|
||
| 11 | # - CSS classes identify sort column and state.
|
||
| 12 | # - Typically used in conjunction with the Pagination module.
|
||
| 13 | #
|
||
| 14 | # Example code snippets:
|
||
| 15 | #
|
||
| 16 | # Controller:
|
||
| 17 | #
|
||
| 18 | # helper :sort
|
||
| 19 | # include SortHelper
|
||
| 20 | 909:cbb26bc654de | Chris | #
|
| 21 | 0:513646585e45 | Chris | # def list
|
| 22 | # sort_init 'last_name'
|
||
| 23 | # sort_update %w(first_name last_name)
|
||
| 24 | # @items = Contact.find_all nil, sort_clause
|
||
| 25 | # end
|
||
| 26 | 909:cbb26bc654de | Chris | #
|
| 27 | 0:513646585e45 | Chris | # Controller (using Pagination module):
|
| 28 | #
|
||
| 29 | # helper :sort
|
||
| 30 | # include SortHelper
|
||
| 31 | 909:cbb26bc654de | Chris | #
|
| 32 | 0:513646585e45 | Chris | # def list
|
| 33 | # sort_init 'last_name'
|
||
| 34 | # sort_update %w(first_name last_name)
|
||
| 35 | # @contact_pages, @items = paginate :contacts,
|
||
| 36 | # :order_by => sort_clause,
|
||
| 37 | # :per_page => 10
|
||
| 38 | # end
|
||
| 39 | 909:cbb26bc654de | Chris | #
|
| 40 | 0:513646585e45 | Chris | # View (table header in list.rhtml):
|
| 41 | 909:cbb26bc654de | Chris | #
|
| 42 | 0:513646585e45 | Chris | # <thead>
|
| 43 | # <tr>
|
||
| 44 | # <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
|
||
| 45 | # <%= sort_header_tag('last_name', :caption => 'Name') %>
|
||
| 46 | # <%= sort_header_tag('phone') %>
|
||
| 47 | # <%= sort_header_tag('address', :width => 200) %>
|
||
| 48 | # </tr>
|
||
| 49 | # </thead>
|
||
| 50 | #
|
||
| 51 | # - Introduces instance variables: @sort_default, @sort_criteria
|
||
| 52 | # - Introduces param :sort
|
||
| 53 | #
|
||
| 54 | |||
| 55 | module SortHelper |
||
| 56 | class SortCriteria |
||
| 57 | 909:cbb26bc654de | Chris | |
| 58 | 0:513646585e45 | Chris | def initialize |
| 59 | @criteria = []
|
||
| 60 | end
|
||
| 61 | 909:cbb26bc654de | Chris | |
| 62 | 0:513646585e45 | Chris | def available_criteria=(criteria) |
| 63 | unless criteria.is_a?(Hash) |
||
| 64 | criteria = criteria.inject({}) {|h,k| h[k] = k; h}
|
||
| 65 | end
|
||
| 66 | @available_criteria = criteria
|
||
| 67 | end
|
||
| 68 | 909:cbb26bc654de | Chris | |
| 69 | 0:513646585e45 | Chris | def from_param(param) |
| 70 | @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]} |
||
| 71 | normalize! |
||
| 72 | end
|
||
| 73 | 909:cbb26bc654de | Chris | |
| 74 | 0:513646585e45 | Chris | def criteria=(arg) |
| 75 | @criteria = arg
|
||
| 76 | normalize! |
||
| 77 | end
|
||
| 78 | 909:cbb26bc654de | Chris | |
| 79 | 0:513646585e45 | Chris | def to_param |
| 80 | @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',') |
||
| 81 | end
|
||
| 82 | 909:cbb26bc654de | Chris | |
| 83 | 1295:622f24f53b42 | Chris | # Returns an array of SQL fragments used to sort the list
|
| 84 | 0:513646585e45 | Chris | def to_sql |
| 85 | sql = @criteria.collect do |k,o| |
||
| 86 | if s = @available_criteria[k] |
||
| 87 | 1295:622f24f53b42 | Chris | (o ? s.to_a : s.to_a.collect {|c| append_desc(c)})
|
| 88 | 0:513646585e45 | Chris | end
|
| 89 | 1295:622f24f53b42 | Chris | end.flatten.compact
|
| 90 | 0:513646585e45 | Chris | sql.blank? ? nil : sql
|
| 91 | end
|
||
| 92 | 909:cbb26bc654de | Chris | |
| 93 | 1115:433d4f72a19b | Chris | def to_a |
| 94 | @criteria.dup
|
||
| 95 | end
|
||
| 96 | |||
| 97 | 0:513646585e45 | Chris | def add!(key, asc) |
| 98 | @criteria.delete_if {|k,o| k == key}
|
||
| 99 | @criteria = [[key, asc]] + @criteria |
||
| 100 | normalize! |
||
| 101 | end
|
||
| 102 | 909:cbb26bc654de | Chris | |
| 103 | 0:513646585e45 | Chris | def add(*args) |
| 104 | r = self.class.new.from_param(to_param)
|
||
| 105 | r.add!(*args) |
||
| 106 | r |
||
| 107 | end
|
||
| 108 | 909:cbb26bc654de | Chris | |
| 109 | 0:513646585e45 | Chris | def first_key |
| 110 | @criteria.first && @criteria.first.first |
||
| 111 | end
|
||
| 112 | 909:cbb26bc654de | Chris | |
| 113 | 0:513646585e45 | Chris | def first_asc? |
| 114 | @criteria.first && @criteria.first.last |
||
| 115 | end
|
||
| 116 | 909:cbb26bc654de | Chris | |
| 117 | 0:513646585e45 | Chris | def empty? |
| 118 | @criteria.empty?
|
||
| 119 | end
|
||
| 120 | 909:cbb26bc654de | Chris | |
| 121 | 0:513646585e45 | Chris | private |
| 122 | 909:cbb26bc654de | Chris | |
| 123 | 0:513646585e45 | Chris | def normalize! |
| 124 | @criteria ||= []
|
||
| 125 | @criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]} |
||
| 126 | @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria |
||
| 127 | @criteria.slice!(3) |
||
| 128 | self
|
||
| 129 | end
|
||
| 130 | 909:cbb26bc654de | Chris | |
| 131 | 0:513646585e45 | Chris | # Appends DESC to the sort criterion unless it has a fixed order
|
| 132 | def append_desc(criterion) |
||
| 133 | if criterion =~ / (asc|desc)$/i |
||
| 134 | criterion |
||
| 135 | else
|
||
| 136 | "#{criterion} DESC"
|
||
| 137 | end
|
||
| 138 | end
|
||
| 139 | end
|
||
| 140 | 909:cbb26bc654de | Chris | |
| 141 | 0:513646585e45 | Chris | def sort_name |
| 142 | controller_name + '_' + action_name + '_sort' |
||
| 143 | end
|
||
| 144 | |||
| 145 | # Initializes the default sort.
|
||
| 146 | # Examples:
|
||
| 147 | 909:cbb26bc654de | Chris | #
|
| 148 | 0:513646585e45 | Chris | # sort_init 'name'
|
| 149 | # sort_init 'id', 'desc'
|
||
| 150 | # sort_init ['name', ['id', 'desc']]
|
||
| 151 | # sort_init [['name', 'desc'], ['id', 'desc']]
|
||
| 152 | #
|
||
| 153 | def sort_init(*args) |
||
| 154 | case args.size
|
||
| 155 | when 1 |
||
| 156 | @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]] |
||
| 157 | when 2 |
||
| 158 | @sort_default = [[args.first, args.last]]
|
||
| 159 | else
|
||
| 160 | raise ArgumentError
|
||
| 161 | end
|
||
| 162 | end
|
||
| 163 | |||
| 164 | # Updates the sort state. Call this in the controller prior to calling
|
||
| 165 | # sort_clause.
|
||
| 166 | # - criteria can be either an array or a hash of allowed keys
|
||
| 167 | #
|
||
| 168 | 1115:433d4f72a19b | Chris | def sort_update(criteria, sort_name=nil) |
| 169 | sort_name ||= self.sort_name
|
||
| 170 | 0:513646585e45 | Chris | @sort_criteria = SortCriteria.new |
| 171 | @sort_criteria.available_criteria = criteria
|
||
| 172 | @sort_criteria.from_param(params[:sort] || session[sort_name]) |
||
| 173 | @sort_criteria.criteria = @sort_default if @sort_criteria.empty? |
||
| 174 | session[sort_name] = @sort_criteria.to_param
|
||
| 175 | end
|
||
| 176 | 909:cbb26bc654de | Chris | |
| 177 | 0:513646585e45 | Chris | # Clears the sort criteria session data
|
| 178 | #
|
||
| 179 | def sort_clear |
||
| 180 | session[sort_name] = nil
|
||
| 181 | end
|
||
| 182 | |||
| 183 | # Returns an SQL sort clause corresponding to the current sort state.
|
||
| 184 | # Use this to sort the controller's table items collection.
|
||
| 185 | #
|
||
| 186 | def sort_clause() |
||
| 187 | @sort_criteria.to_sql
|
||
| 188 | end
|
||
| 189 | |||
| 190 | 1115:433d4f72a19b | Chris | def sort_criteria |
| 191 | @sort_criteria
|
||
| 192 | end
|
||
| 193 | |||
| 194 | 0:513646585e45 | Chris | # Returns a link which sorts by the named column.
|
| 195 | #
|
||
| 196 | # - column is the name of an attribute in the sorted record collection.
|
||
| 197 | # - the optional caption explicitly specifies the displayed link text.
|
||
| 198 | # - 2 CSS classes reflect the state of the link: sort and asc or desc
|
||
| 199 | #
|
||
| 200 | def sort_link(column, caption, default_order) |
||
| 201 | css, order = nil, default_order
|
||
| 202 | 909:cbb26bc654de | Chris | |
| 203 | 0:513646585e45 | Chris | if column.to_s == @sort_criteria.first_key |
| 204 | if @sort_criteria.first_asc? |
||
| 205 | css = 'sort asc'
|
||
| 206 | order = 'desc'
|
||
| 207 | else
|
||
| 208 | css = 'sort desc'
|
||
| 209 | order = 'asc'
|
||
| 210 | end
|
||
| 211 | end
|
||
| 212 | caption = column.to_s.humanize unless caption
|
||
| 213 | 909:cbb26bc654de | Chris | |
| 214 | 0:513646585e45 | Chris | sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param }
|
| 215 | 441:cbce1fd3b1b7 | Chris | url_options = params.merge(sort_options) |
| 216 | 909:cbb26bc654de | Chris | |
| 217 | 0:513646585e45 | Chris | # Add project_id to url_options
|
| 218 | url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id) |
||
| 219 | |||
| 220 | 909:cbb26bc654de | Chris | link_to_content_update(h(caption), url_options, :class => css)
|
| 221 | 0:513646585e45 | Chris | end
|
| 222 | |||
| 223 | # Returns a table header <th> tag with a sort link for the named column
|
||
| 224 | # attribute.
|
||
| 225 | #
|
||
| 226 | # Options:
|
||
| 227 | # :caption The displayed link name (defaults to titleized column name).
|
||
| 228 | # :title The tag's 'title' attribute (defaults to 'Sort by :caption').
|
||
| 229 | #
|
||
| 230 | # Other options hash entries generate additional table header tag attributes.
|
||
| 231 | #
|
||
| 232 | # Example:
|
||
| 233 | #
|
||
| 234 | # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %>
|
||
| 235 | #
|
||
| 236 | def sort_header_tag(column, options = {}) |
||
| 237 | caption = options.delete(:caption) || column.to_s.humanize
|
||
| 238 | default_order = options.delete(:default_order) || 'asc' |
||
| 239 | options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title] |
||
| 240 | content_tag('th', sort_link(column, caption, default_order), options)
|
||
| 241 | end
|
||
| 242 | end
|