The requested changes are too big and content was truncated. Show full diff
@@ -1,256 +1,261 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | # |
|
2 | # | |
3 | # Helpers to sort tables using clickable column headers. |
|
3 | # Helpers to sort tables using clickable column headers. | |
4 | # |
|
4 | # | |
5 | # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005. |
|
5 | # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005. | |
6 | # Jean-Philippe Lang, 2009 |
|
6 | # Jean-Philippe Lang, 2009 | |
7 | # License: This source code is released under the MIT license. |
|
7 | # License: This source code is released under the MIT license. | |
8 | # |
|
8 | # | |
9 | # - Consecutive clicks toggle the column's sort order. |
|
9 | # - Consecutive clicks toggle the column's sort order. | |
10 | # - Sort state is maintained by a session hash entry. |
|
10 | # - Sort state is maintained by a session hash entry. | |
11 | # - CSS classes identify sort column and state. |
|
11 | # - CSS classes identify sort column and state. | |
12 | # - Typically used in conjunction with the Pagination module. |
|
12 | # - Typically used in conjunction with the Pagination module. | |
13 | # |
|
13 | # | |
14 | # Example code snippets: |
|
14 | # Example code snippets: | |
15 | # |
|
15 | # | |
16 | # Controller: |
|
16 | # Controller: | |
17 | # |
|
17 | # | |
18 | # helper :sort |
|
18 | # helper :sort | |
19 | # include SortHelper |
|
19 | # include SortHelper | |
20 | # |
|
20 | # | |
21 | # def list |
|
21 | # def list | |
22 | # sort_init 'last_name' |
|
22 | # sort_init 'last_name' | |
23 | # sort_update %w(first_name last_name) |
|
23 | # sort_update %w(first_name last_name) | |
24 | # @items = Contact.find_all nil, sort_clause |
|
24 | # @items = Contact.find_all nil, sort_clause | |
25 | # end |
|
25 | # end | |
26 | # |
|
26 | # | |
27 | # Controller (using Pagination module): |
|
27 | # Controller (using Pagination module): | |
28 | # |
|
28 | # | |
29 | # helper :sort |
|
29 | # helper :sort | |
30 | # include SortHelper |
|
30 | # include SortHelper | |
31 | # |
|
31 | # | |
32 | # def list |
|
32 | # def list | |
33 | # sort_init 'last_name' |
|
33 | # sort_init 'last_name' | |
34 | # sort_update %w(first_name last_name) |
|
34 | # sort_update %w(first_name last_name) | |
35 | # @contact_pages, @items = paginate :contacts, |
|
35 | # @contact_pages, @items = paginate :contacts, | |
36 | # :order_by => sort_clause, |
|
36 | # :order_by => sort_clause, | |
37 | # :per_page => 10 |
|
37 | # :per_page => 10 | |
38 | # end |
|
38 | # end | |
39 | # |
|
39 | # | |
40 | # View (table header in list.rhtml): |
|
40 | # View (table header in list.rhtml): | |
41 | # |
|
41 | # | |
42 | # <thead> |
|
42 | # <thead> | |
43 | # <tr> |
|
43 | # <tr> | |
44 | # <%= sort_header_tag('id', :title => 'Sort by contact ID') %> |
|
44 | # <%= sort_header_tag('id', :title => 'Sort by contact ID') %> | |
45 | # <%= sort_header_tag('last_name', :caption => 'Name') %> |
|
45 | # <%= sort_header_tag('last_name', :caption => 'Name') %> | |
46 | # <%= sort_header_tag('phone') %> |
|
46 | # <%= sort_header_tag('phone') %> | |
47 | # <%= sort_header_tag('address', :width => 200) %> |
|
47 | # <%= sort_header_tag('address', :width => 200) %> | |
48 | # </tr> |
|
48 | # </tr> | |
49 | # </thead> |
|
49 | # </thead> | |
50 | # |
|
50 | # | |
51 | # - Introduces instance variables: @sort_default, @sort_criteria |
|
51 | # - Introduces instance variables: @sort_default, @sort_criteria | |
52 | # - Introduces param :sort |
|
52 | # - Introduces param :sort | |
53 | # |
|
53 | # | |
54 |
|
54 | |||
55 | module SortHelper |
|
55 | module SortHelper | |
56 | class SortCriteria |
|
56 | class SortCriteria | |
57 |
|
57 | |||
58 | def initialize |
|
58 | def initialize | |
59 | @criteria = [] |
|
59 | @criteria = [] | |
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 | def available_criteria=(criteria) |
|
62 | def available_criteria=(criteria) | |
63 | unless criteria.is_a?(Hash) |
|
63 | unless criteria.is_a?(Hash) | |
64 | criteria = criteria.inject({}) {|h,k| h[k] = k; h} |
|
64 | criteria = criteria.inject({}) {|h,k| h[k] = k; h} | |
65 | end |
|
65 | end | |
66 | @available_criteria = criteria |
|
66 | @available_criteria = criteria | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def from_param(param) |
|
69 | def from_param(param) | |
70 | @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]} |
|
70 | @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]} | |
71 | normalize! |
|
71 | normalize! | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | def criteria=(arg) |
|
74 | def criteria=(arg) | |
75 | @criteria = arg |
|
75 | @criteria = arg | |
76 | normalize! |
|
76 | normalize! | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | def to_param |
|
79 | def to_param | |
80 | @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',') |
|
80 | @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',') | |
81 | end |
|
81 | end | |
82 |
|
82 | |||
83 | # Returns an array of SQL fragments used to sort the list |
|
83 | # Returns an array of SQL fragments used to sort the list | |
84 | def to_sql |
|
84 | def to_sql | |
85 | sql = @criteria.collect do |k,o| |
|
85 | sql = @criteria.collect do |k,o| | |
86 | if s = @available_criteria[k] |
|
86 | if s = @available_criteria[k] | |
87 | s = [s] unless s.is_a?(Array) |
|
87 | s = [s] unless s.is_a?(Array) | |
88 |
|
|
88 | s.collect {|c| append_order(c, o ? "ASC" : "DESC")} | |
89 | end |
|
89 | end | |
90 | end.flatten.compact |
|
90 | end.flatten.compact | |
91 | sql.blank? ? nil : sql |
|
91 | sql.blank? ? nil : sql | |
92 | end |
|
92 | end | |
93 |
|
93 | |||
94 | def to_a |
|
94 | def to_a | |
95 | @criteria.dup |
|
95 | @criteria.dup | |
96 | end |
|
96 | end | |
97 |
|
97 | |||
98 | def add!(key, asc) |
|
98 | def add!(key, asc) | |
99 | @criteria.delete_if {|k,o| k == key} |
|
99 | @criteria.delete_if {|k,o| k == key} | |
100 | @criteria = [[key, asc]] + @criteria |
|
100 | @criteria = [[key, asc]] + @criteria | |
101 | normalize! |
|
101 | normalize! | |
102 | end |
|
102 | end | |
103 |
|
103 | |||
104 | def add(*args) |
|
104 | def add(*args) | |
105 | r = self.class.new.from_param(to_param) |
|
105 | r = self.class.new.from_param(to_param) | |
106 | r.add!(*args) |
|
106 | r.add!(*args) | |
107 | r |
|
107 | r | |
108 | end |
|
108 | end | |
109 |
|
109 | |||
110 | def first_key |
|
110 | def first_key | |
111 | @criteria.first && @criteria.first.first |
|
111 | @criteria.first && @criteria.first.first | |
112 | end |
|
112 | end | |
113 |
|
113 | |||
114 | def first_asc? |
|
114 | def first_asc? | |
115 | @criteria.first && @criteria.first.last |
|
115 | @criteria.first && @criteria.first.last | |
116 | end |
|
116 | end | |
117 |
|
117 | |||
118 | def empty? |
|
118 | def empty? | |
119 | @criteria.empty? |
|
119 | @criteria.empty? | |
120 | end |
|
120 | end | |
121 |
|
121 | |||
122 | private |
|
122 | private | |
123 |
|
123 | |||
124 | def normalize! |
|
124 | def normalize! | |
125 | @criteria ||= [] |
|
125 | @criteria ||= [] | |
126 | @criteria = @criteria.collect {|s| s = Array(s); [s.first, (s.last == false || s.last == 'desc') ? false : true]} |
|
126 | @criteria = @criteria.collect {|s| s = Array(s); [s.first, (s.last == false || s.last == 'desc') ? false : true]} | |
127 | @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria |
|
127 | @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria | |
128 | @criteria.slice!(3) |
|
128 | @criteria.slice!(3) | |
129 | self |
|
129 | self | |
130 | end |
|
130 | end | |
131 |
|
131 | |||
132 | # Appends DESC to the sort criterion unless it has a fixed order |
|
132 | # Appends ASC/DESC to the sort criterion unless it has a fixed order | |
133 |
def append_ |
|
133 | def append_order(criterion, order) | |
134 | if criterion =~ / (asc|desc)$/i |
|
134 | if criterion =~ / (asc|desc)$/i | |
135 | criterion |
|
135 | criterion | |
136 | else |
|
136 | else | |
137 |
"#{criterion} |
|
137 | "#{criterion} #{order}" | |
138 | end |
|
138 | end | |
139 | end |
|
139 | end | |
|
140 | ||||
|
141 | # Appends DESC to the sort criterion unless it has a fixed order | |||
|
142 | def append_desc(criterion) | |||
|
143 | append_order(criterion, "DESC") | |||
|
144 | end | |||
140 | end |
|
145 | end | |
141 |
|
146 | |||
142 | def sort_name |
|
147 | def sort_name | |
143 | controller_name + '_' + action_name + '_sort' |
|
148 | controller_name + '_' + action_name + '_sort' | |
144 | end |
|
149 | end | |
145 |
|
150 | |||
146 | # Initializes the default sort. |
|
151 | # Initializes the default sort. | |
147 | # Examples: |
|
152 | # Examples: | |
148 | # |
|
153 | # | |
149 | # sort_init 'name' |
|
154 | # sort_init 'name' | |
150 | # sort_init 'id', 'desc' |
|
155 | # sort_init 'id', 'desc' | |
151 | # sort_init ['name', ['id', 'desc']] |
|
156 | # sort_init ['name', ['id', 'desc']] | |
152 | # sort_init [['name', 'desc'], ['id', 'desc']] |
|
157 | # sort_init [['name', 'desc'], ['id', 'desc']] | |
153 | # |
|
158 | # | |
154 | def sort_init(*args) |
|
159 | def sort_init(*args) | |
155 | case args.size |
|
160 | case args.size | |
156 | when 1 |
|
161 | when 1 | |
157 | @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]] |
|
162 | @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]] | |
158 | when 2 |
|
163 | when 2 | |
159 | @sort_default = [[args.first, args.last]] |
|
164 | @sort_default = [[args.first, args.last]] | |
160 | else |
|
165 | else | |
161 | raise ArgumentError |
|
166 | raise ArgumentError | |
162 | end |
|
167 | end | |
163 | end |
|
168 | end | |
164 |
|
169 | |||
165 | # Updates the sort state. Call this in the controller prior to calling |
|
170 | # Updates the sort state. Call this in the controller prior to calling | |
166 | # sort_clause. |
|
171 | # sort_clause. | |
167 | # - criteria can be either an array or a hash of allowed keys |
|
172 | # - criteria can be either an array or a hash of allowed keys | |
168 | # |
|
173 | # | |
169 | def sort_update(criteria, sort_name=nil) |
|
174 | def sort_update(criteria, sort_name=nil) | |
170 | sort_name ||= self.sort_name |
|
175 | sort_name ||= self.sort_name | |
171 | @sort_criteria = SortCriteria.new |
|
176 | @sort_criteria = SortCriteria.new | |
172 | @sort_criteria.available_criteria = criteria |
|
177 | @sort_criteria.available_criteria = criteria | |
173 | @sort_criteria.from_param(params[:sort] || session[sort_name]) |
|
178 | @sort_criteria.from_param(params[:sort] || session[sort_name]) | |
174 | @sort_criteria.criteria = @sort_default if @sort_criteria.empty? |
|
179 | @sort_criteria.criteria = @sort_default if @sort_criteria.empty? | |
175 | session[sort_name] = @sort_criteria.to_param |
|
180 | session[sort_name] = @sort_criteria.to_param | |
176 | end |
|
181 | end | |
177 |
|
182 | |||
178 | # Clears the sort criteria session data |
|
183 | # Clears the sort criteria session data | |
179 | # |
|
184 | # | |
180 | def sort_clear |
|
185 | def sort_clear | |
181 | session[sort_name] = nil |
|
186 | session[sort_name] = nil | |
182 | end |
|
187 | end | |
183 |
|
188 | |||
184 | # Returns an SQL sort clause corresponding to the current sort state. |
|
189 | # Returns an SQL sort clause corresponding to the current sort state. | |
185 | # Use this to sort the controller's table items collection. |
|
190 | # Use this to sort the controller's table items collection. | |
186 | # |
|
191 | # | |
187 | def sort_clause() |
|
192 | def sort_clause() | |
188 | @sort_criteria.to_sql |
|
193 | @sort_criteria.to_sql | |
189 | end |
|
194 | end | |
190 |
|
195 | |||
191 | def sort_criteria |
|
196 | def sort_criteria | |
192 | @sort_criteria |
|
197 | @sort_criteria | |
193 | end |
|
198 | end | |
194 |
|
199 | |||
195 | # Returns a link which sorts by the named column. |
|
200 | # Returns a link which sorts by the named column. | |
196 | # |
|
201 | # | |
197 | # - column is the name of an attribute in the sorted record collection. |
|
202 | # - column is the name of an attribute in the sorted record collection. | |
198 | # - the optional caption explicitly specifies the displayed link text. |
|
203 | # - the optional caption explicitly specifies the displayed link text. | |
199 | # - 2 CSS classes reflect the state of the link: sort and asc or desc |
|
204 | # - 2 CSS classes reflect the state of the link: sort and asc or desc | |
200 | # |
|
205 | # | |
201 | def sort_link(column, caption, default_order) |
|
206 | def sort_link(column, caption, default_order) | |
202 | css, order = nil, default_order |
|
207 | css, order = nil, default_order | |
203 |
|
208 | |||
204 | if column.to_s == @sort_criteria.first_key |
|
209 | if column.to_s == @sort_criteria.first_key | |
205 | if @sort_criteria.first_asc? |
|
210 | if @sort_criteria.first_asc? | |
206 | css = 'sort asc' |
|
211 | css = 'sort asc' | |
207 | order = 'desc' |
|
212 | order = 'desc' | |
208 | else |
|
213 | else | |
209 | css = 'sort desc' |
|
214 | css = 'sort desc' | |
210 | order = 'asc' |
|
215 | order = 'asc' | |
211 | end |
|
216 | end | |
212 | end |
|
217 | end | |
213 | caption = column.to_s.humanize unless caption |
|
218 | caption = column.to_s.humanize unless caption | |
214 |
|
219 | |||
215 | sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param } |
|
220 | sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param } | |
216 | url_options = params.merge(sort_options) |
|
221 | url_options = params.merge(sort_options) | |
217 |
|
222 | |||
218 | # Add project_id to url_options |
|
223 | # Add project_id to url_options | |
219 | url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id) |
|
224 | url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id) | |
220 |
|
225 | |||
221 | link_to_content_update(h(caption), url_options, :class => css) |
|
226 | link_to_content_update(h(caption), url_options, :class => css) | |
222 | end |
|
227 | end | |
223 |
|
228 | |||
224 | # Returns a table header <th> tag with a sort link for the named column |
|
229 | # Returns a table header <th> tag with a sort link for the named column | |
225 | # attribute. |
|
230 | # attribute. | |
226 | # |
|
231 | # | |
227 | # Options: |
|
232 | # Options: | |
228 | # :caption The displayed link name (defaults to titleized column name). |
|
233 | # :caption The displayed link name (defaults to titleized column name). | |
229 | # :title The tag's 'title' attribute (defaults to 'Sort by :caption'). |
|
234 | # :title The tag's 'title' attribute (defaults to 'Sort by :caption'). | |
230 | # |
|
235 | # | |
231 | # Other options hash entries generate additional table header tag attributes. |
|
236 | # Other options hash entries generate additional table header tag attributes. | |
232 | # |
|
237 | # | |
233 | # Example: |
|
238 | # Example: | |
234 | # |
|
239 | # | |
235 | # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> |
|
240 | # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> | |
236 | # |
|
241 | # | |
237 | def sort_header_tag(column, options = {}) |
|
242 | def sort_header_tag(column, options = {}) | |
238 | caption = options.delete(:caption) || column.to_s.humanize |
|
243 | caption = options.delete(:caption) || column.to_s.humanize | |
239 | default_order = options.delete(:default_order) || 'asc' |
|
244 | default_order = options.delete(:default_order) || 'asc' | |
240 | options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title] |
|
245 | options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title] | |
241 | content_tag('th', sort_link(column, caption, default_order), options) |
|
246 | content_tag('th', sort_link(column, caption, default_order), options) | |
242 | end |
|
247 | end | |
243 |
|
248 | |||
244 | # Returns the css classes for the current sort order |
|
249 | # Returns the css classes for the current sort order | |
245 | # |
|
250 | # | |
246 | # Example: |
|
251 | # Example: | |
247 | # |
|
252 | # | |
248 | # sort_css_classes |
|
253 | # sort_css_classes | |
249 | # # => "sort-by-created-on sort-desc" |
|
254 | # # => "sort-by-created-on sort-desc" | |
250 | def sort_css_classes |
|
255 | def sort_css_classes | |
251 | if @sort_criteria.first_key |
|
256 | if @sort_criteria.first_key | |
252 | "sort-by-#{@sort_criteria.first_key.to_s.dasherize} sort-#{@sort_criteria.first_asc? ? 'asc' : 'desc'}" |
|
257 | "sort-by-#{@sort_criteria.first_key.to_s.dasherize} sort-#{@sort_criteria.first_asc? ? 'asc' : 'desc'}" | |
253 | end |
|
258 | end | |
254 | end |
|
259 | end | |
255 | end |
|
260 | end | |
256 |
|
261 |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
@@ -1,109 +1,109 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2015 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.expand_path('../../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class SortHelperTest < ActionView::TestCase |
|
20 | class SortHelperTest < ActionView::TestCase | |
21 | include SortHelper |
|
21 | include SortHelper | |
22 | include Redmine::I18n |
|
22 | include Redmine::I18n | |
23 | include ERB::Util |
|
23 | include ERB::Util | |
24 |
|
24 | |||
25 | def setup |
|
25 | def setup | |
26 | @session = nil |
|
26 | @session = nil | |
27 | @sort_param = nil |
|
27 | @sort_param = nil | |
28 | end |
|
28 | end | |
29 |
|
29 | |||
30 | def test_default_sort_clause_with_array |
|
30 | def test_default_sort_clause_with_array | |
31 | sort_init 'attr1', 'desc' |
|
31 | sort_init 'attr1', 'desc' | |
32 | sort_update(['attr1', 'attr2']) |
|
32 | sort_update(['attr1', 'attr2']) | |
33 |
|
33 | |||
34 | assert_equal ['attr1 DESC'], sort_clause |
|
34 | assert_equal ['attr1 DESC'], sort_clause | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | def test_default_sort_clause_with_hash |
|
37 | def test_default_sort_clause_with_hash | |
38 | sort_init 'attr1', 'desc' |
|
38 | sort_init 'attr1', 'desc' | |
39 | sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'}) |
|
39 | sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'}) | |
40 |
|
40 | |||
41 | assert_equal ['table1.attr1 DESC'], sort_clause |
|
41 | assert_equal ['table1.attr1 DESC'], sort_clause | |
42 | end |
|
42 | end | |
43 |
|
43 | |||
44 | def test_default_sort_clause_with_multiple_columns |
|
44 | def test_default_sort_clause_with_multiple_columns | |
45 | sort_init 'attr1', 'desc' |
|
45 | sort_init 'attr1', 'desc' | |
46 | sort_update({'attr1' => ['table1.attr1', 'table1.attr2'], 'attr2' => 'table2.attr2'}) |
|
46 | sort_update({'attr1' => ['table1.attr1', 'table1.attr2'], 'attr2' => 'table2.attr2'}) | |
47 |
|
47 | |||
48 | assert_equal ['table1.attr1 DESC', 'table1.attr2 DESC'], sort_clause |
|
48 | assert_equal ['table1.attr1 DESC', 'table1.attr2 DESC'], sort_clause | |
49 | end |
|
49 | end | |
50 |
|
50 | |||
51 | def test_params_sort |
|
51 | def test_params_sort | |
52 | @sort_param = 'attr1,attr2:desc' |
|
52 | @sort_param = 'attr1,attr2:desc' | |
53 |
|
53 | |||
54 | sort_init 'attr1', 'desc' |
|
54 | sort_init 'attr1', 'desc' | |
55 | sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'}) |
|
55 | sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'}) | |
56 |
|
56 | |||
57 | assert_equal ['table1.attr1', 'table2.attr2 DESC'], sort_clause |
|
57 | assert_equal ['table1.attr1 ASC', 'table2.attr2 DESC'], sort_clause | |
58 | assert_equal 'attr1,attr2:desc', @session['foo_bar_sort'] |
|
58 | assert_equal 'attr1,attr2:desc', @session['foo_bar_sort'] | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | def test_invalid_params_sort |
|
61 | def test_invalid_params_sort | |
62 | @sort_param = 'invalid_key' |
|
62 | @sort_param = 'invalid_key' | |
63 |
|
63 | |||
64 | sort_init 'attr1', 'desc' |
|
64 | sort_init 'attr1', 'desc' | |
65 | sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'}) |
|
65 | sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'}) | |
66 |
|
66 | |||
67 | assert_equal ['table1.attr1 DESC'], sort_clause |
|
67 | assert_equal ['table1.attr1 DESC'], sort_clause | |
68 | assert_equal 'attr1:desc', @session['foo_bar_sort'] |
|
68 | assert_equal 'attr1:desc', @session['foo_bar_sort'] | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | def test_invalid_order_params_sort |
|
71 | def test_invalid_order_params_sort | |
72 | @sort_param = 'attr1:foo:bar,attr2' |
|
72 | @sort_param = 'attr1:foo:bar,attr2' | |
73 |
|
73 | |||
74 | sort_init 'attr1', 'desc' |
|
74 | sort_init 'attr1', 'desc' | |
75 | sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'}) |
|
75 | sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'}) | |
76 |
|
76 | |||
77 | assert_equal ['table1.attr1', 'table2.attr2'], sort_clause |
|
77 | assert_equal ['table1.attr1 ASC', 'table2.attr2 ASC'], sort_clause | |
78 | assert_equal 'attr1,attr2', @session['foo_bar_sort'] |
|
78 | assert_equal 'attr1,attr2', @session['foo_bar_sort'] | |
79 | end |
|
79 | end | |
80 |
|
80 | |||
81 | def test_sort_css_without_params_should_use_default_sort |
|
81 | def test_sort_css_without_params_should_use_default_sort | |
82 | sort_init 'attr1', 'desc' |
|
82 | sort_init 'attr1', 'desc' | |
83 | sort_update(['attr1', 'attr2']) |
|
83 | sort_update(['attr1', 'attr2']) | |
84 |
|
84 | |||
85 | assert_equal 'sort-by-attr1 sort-desc', sort_css_classes |
|
85 | assert_equal 'sort-by-attr1 sort-desc', sort_css_classes | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | def test_sort_css_should_use_params |
|
88 | def test_sort_css_should_use_params | |
89 | @sort_param = 'attr2,attr1' |
|
89 | @sort_param = 'attr2,attr1' | |
90 | sort_init 'attr1', 'desc' |
|
90 | sort_init 'attr1', 'desc' | |
91 | sort_update(['attr1', 'attr2']) |
|
91 | sort_update(['attr1', 'attr2']) | |
92 |
|
92 | |||
93 | assert_equal 'sort-by-attr2 sort-asc', sort_css_classes |
|
93 | assert_equal 'sort-by-attr2 sort-asc', sort_css_classes | |
94 | end |
|
94 | end | |
95 |
|
95 | |||
96 | def test_sort_css_should_dasherize_sort_name |
|
96 | def test_sort_css_should_dasherize_sort_name | |
97 | sort_init 'foo_bar' |
|
97 | sort_init 'foo_bar' | |
98 | sort_update(['foo_bar']) |
|
98 | sort_update(['foo_bar']) | |
99 |
|
99 | |||
100 | assert_equal 'sort-by-foo-bar sort-asc', sort_css_classes |
|
100 | assert_equal 'sort-by-foo-bar sort-asc', sort_css_classes | |
101 | end |
|
101 | end | |
102 |
|
102 | |||
103 | private |
|
103 | private | |
104 |
|
104 | |||
105 | def controller_name; 'foo'; end |
|
105 | def controller_name; 'foo'; end | |
106 | def action_name; 'bar'; end |
|
106 | def action_name; 'bar'; end | |
107 | def params; {:sort => @sort_param}; end |
|
107 | def params; {:sort => @sort_param}; end | |
108 | def session; @session ||= {}; end |
|
108 | def session; @session ||= {}; end | |
109 | end |
|
109 | end |
General Comments 0
You need to be logged in to leave comments.
Login now