diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-03-12 18:06:54 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-03-12 18:06:54 +0000 |
commit | c7c8dc71f2fc8f224deb0a5340f7bbb8906e584d (patch) | |
tree | de3c8964542f8bb72cef67f462df910c4e6446f7 /app/helpers/sort_helper.rb | |
parent | 2b585407cb66a26f81ea0bcb8a922dd1203e25e0 (diff) | |
download | redmine-c7c8dc71f2fc8f224deb0a5340f7bbb8906e584d.tar.gz redmine-c7c8dc71f2fc8f224deb0a5340f7bbb8906e584d.zip |
Ability to save "sort order" in custom queries (#2899).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2572 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/helpers/sort_helper.rb')
-rw-r--r-- | app/helpers/sort_helper.rb | 57 |
1 files changed, 39 insertions, 18 deletions
diff --git a/app/helpers/sort_helper.rb b/app/helpers/sort_helper.rb index d4d8f721a..7b7fbc8a3 100644 --- a/app/helpers/sort_helper.rb +++ b/app/helpers/sort_helper.rb @@ -69,6 +69,11 @@ module SortHelper normalize! end + def criteria=(arg) + @criteria = arg + normalize! + end + def to_param @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',') end @@ -102,24 +107,42 @@ module SortHelper @criteria.first && @criteria.first.last end + def empty? + @criteria.empty? + end + private def normalize! - @criteria = @criteria.collect {|s| [s.first, (s.last == false || s.last == 'desc') ? false : true]} + @criteria ||= [] + @criteria = @criteria.collect {|s| s = s.to_a; [s.first, (s.last == false || s.last == 'desc') ? false : true]} @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria @criteria.slice!(3) self end end + + def sort_name + controller_name + '_' + action_name + '_sort' + end - # Initializes the default sort column (default_key) and sort order - # (default_order). - # - # - default_key is a column attribute name. - # - default_order is 'asc' or 'desc'. + # Initializes the default sort. + # Examples: + # + # sort_init 'name' + # sort_init 'id', 'desc' + # sort_init ['name', ['id', 'desc']] + # sort_init [['name', 'desc'], ['id', 'desc']] # - def sort_init(default_key, default_order='asc') - @sort_default = "#{default_key}:#{default_order}" + def sort_init(*args) + case args.size + when 1 + @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]] + when 2 + @sort_default = [[args.first, args.last]] + else + raise ArgumentError + end end # Updates the sort state. Call this in the controller prior to calling @@ -127,13 +150,18 @@ module SortHelper # - criteria can be either an array or a hash of allowed keys # def sort_update(criteria) - sort_name = controller_name + '_' + action_name + '_sort' - @sort_criteria = SortCriteria.new @sort_criteria.available_criteria = criteria - @sort_criteria.from_param(params[:sort] || session[sort_name] || @sort_default) + @sort_criteria.from_param(params[:sort] || session[sort_name]) + @sort_criteria.criteria = @sort_default if @sort_criteria.empty? session[sort_name] = @sort_criteria.to_param end + + # Clears the sort criteria session data + # + def sort_clear + session[sort_name] = nil + end # Returns an SQL sort clause corresponding to the current sort state. # Use this to sort the controller's table items collection. @@ -188,13 +216,6 @@ module SortHelper # # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %> # - # Renders: - # - # <th title="Sort by contact ID" width="40"> - # <a href="/contact/list?sort_order=desc&sort_key=id">Id</a> - # <img alt="Sort_asc" src="/images/sort_asc.png" /> - # </th> - # def sort_header_tag(column, options = {}) caption = options.delete(:caption) || column.to_s.humanize default_order = options.delete(:default_order) || 'asc' |