comparison app/helpers/sort_helper.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents 433d4f72a19b
children 622f24f53b42
comparison
equal deleted inserted replaced
1209:1b1138f6f55e 1338:25603efa57b5
87 end 87 end
88 end.compact.join(', ') 88 end.compact.join(', ')
89 sql.blank? ? nil : sql 89 sql.blank? ? nil : sql
90 end 90 end
91 91
92 def to_a
93 @criteria.dup
94 end
95
92 def add!(key, asc) 96 def add!(key, asc)
93 @criteria.delete_if {|k,o| k == key} 97 @criteria.delete_if {|k,o| k == key}
94 @criteria = [[key, asc]] + @criteria 98 @criteria = [[key, asc]] + @criteria
95 normalize! 99 normalize!
96 end 100 end
158 162
159 # Updates the sort state. Call this in the controller prior to calling 163 # Updates the sort state. Call this in the controller prior to calling
160 # sort_clause. 164 # sort_clause.
161 # - criteria can be either an array or a hash of allowed keys 165 # - criteria can be either an array or a hash of allowed keys
162 # 166 #
163 def sort_update(criteria) 167 def sort_update(criteria, sort_name=nil)
168 sort_name ||= self.sort_name
164 @sort_criteria = SortCriteria.new 169 @sort_criteria = SortCriteria.new
165 @sort_criteria.available_criteria = criteria 170 @sort_criteria.available_criteria = criteria
166 @sort_criteria.from_param(params[:sort] || session[sort_name]) 171 @sort_criteria.from_param(params[:sort] || session[sort_name])
167 @sort_criteria.criteria = @sort_default if @sort_criteria.empty? 172 @sort_criteria.criteria = @sort_default if @sort_criteria.empty?
168 session[sort_name] = @sort_criteria.to_param 173 session[sort_name] = @sort_criteria.to_param
177 # Returns an SQL sort clause corresponding to the current sort state. 182 # Returns an SQL sort clause corresponding to the current sort state.
178 # Use this to sort the controller's table items collection. 183 # Use this to sort the controller's table items collection.
179 # 184 #
180 def sort_clause() 185 def sort_clause()
181 @sort_criteria.to_sql 186 @sort_criteria.to_sql
187 end
188
189 def sort_criteria
190 @sort_criteria
182 end 191 end
183 192
184 # Returns a link which sorts by the named column. 193 # Returns a link which sorts by the named column.
185 # 194 #
186 # - column is the name of an attribute in the sorted record collection. 195 # - column is the name of an attribute in the sorted record collection.