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