diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2017-03-13 19:17:59 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2017-03-13 19:17:59 +0000 |
commit | 71d88e55352a51b20dd5e2a4a44aa695834a36b9 (patch) | |
tree | 8d5708333c51b7da7319a5c182350758b00dc535 /app/models | |
parent | b91489ce6bb0943cff50946af6e64be65d3ccd80 (diff) | |
download | redmine-71d88e55352a51b20dd5e2a4a44aa695834a36b9.tar.gz redmine-71d88e55352a51b20dd5e2a4a44aa695834a36b9.zip |
Get rid of sort_helper when using queries.
git-svn-id: http://svn.redmine.org/redmine/trunk@16390 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/issue_query.rb | 8 | ||||
-rw-r--r-- | app/models/query.rb | 44 | ||||
-rw-r--r-- | app/models/time_entry_query.rb | 6 |
3 files changed, 39 insertions, 19 deletions
diff --git a/app/models/issue_query.rb b/app/models/issue_query.rb index 71fe8d288..9f3bf290d 100644 --- a/app/models/issue_query.rb +++ b/app/models/issue_query.rb @@ -234,6 +234,10 @@ class IssueQuery < Query Setting.issue_list_default_totals.map(&:to_sym) end + def default_sort_criteria + [['id', 'desc']] + end + def base_scope Issue.visible.joins(:status, :project).where(statement) end @@ -267,7 +271,7 @@ class IssueQuery < Query # Returns the issues # Valid options are :order, :offset, :limit, :include, :conditions def issues(options={}) - order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) + order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?) scope = Issue.visible. joins(:status, :project). @@ -309,7 +313,7 @@ class IssueQuery < Query # Returns the issues ids def issue_ids(options={}) - order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) + order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?) Issue.visible. joins(:status, :project). diff --git a/app/models/query.rb b/app/models/query.rb index 7a43ca6fc..192502024 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -377,6 +377,7 @@ class Query < ActiveRecord::Base self.group_by = params[:group_by] || (params[:query] && params[:query][:group_by]) self.column_names = params[:c] || (params[:query] && params[:query][:column_names]) self.totalable_names = params[:t] || (params[:query] && params[:query][:totalable_names]) + self.sort_criteria = params[:sort] || (params[:query] && params[:query][:sort_criteria]) self end @@ -710,37 +711,40 @@ class Query < ActiveRecord::Base options[:totalable_names] || default_totalable_names || [] end + def default_sort_criteria + [] + end + def sort_criteria=(arg) - c = [] - if arg.is_a?(Hash) - arg = arg.keys.sort.collect {|k| arg[k]} - end - if arg - c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, (o == 'desc' || o == false) ? 'desc' : 'asc']} - end - write_attribute(:sort_criteria, c) + c = Redmine::SortCriteria.new(arg) + write_attribute(:sort_criteria, c.to_a) + c end def sort_criteria - read_attribute(:sort_criteria) || [] + c = read_attribute(:sort_criteria) + if c.blank? + c = default_sort_criteria + end + Redmine::SortCriteria.new(c) end - def sort_criteria_key(arg) - sort_criteria && sort_criteria[arg] && sort_criteria[arg].first + def sort_criteria_key(index) + sort_criteria[index].try(:first) end - def sort_criteria_order(arg) - sort_criteria && sort_criteria[arg] && sort_criteria[arg].last + def sort_criteria_order(index) + sort_criteria[index].try(:last) end - def sort_criteria_order_for(key) - sort_criteria.detect {|k, order| key.to_s == k}.try(:last) + def sort_clause + sort_criteria.sort_clause(sortable_columns) end # Returns the SQL sort order that should be prepended for grouping def group_by_sort_order if column = group_by_column - order = (sort_criteria_order_for(column.name) || column.default_order || 'asc').try(:upcase) + order = (sort_criteria.order_for(column.name) || column.default_order || 'asc').try(:upcase) Array(column.sortable).map {|s| "#{s} #{order}"} end end @@ -878,6 +882,14 @@ class Query < ActiveRecord::Base totals end + def css_classes + s = sort_criteria.first + if s.present? + key, asc = s + "sort-by-#{key.to_s.dasherize} sort-#{asc}" + end + end + private def grouped_query(&block) diff --git a/app/models/time_entry_query.rb b/app/models/time_entry_query.rb index 5b39bd34e..3d37e94da 100644 --- a/app/models/time_entry_query.rb +++ b/app/models/time_entry_query.rb @@ -105,6 +105,10 @@ class TimeEntryQuery < Query def default_totalable_names [:hours] end + + def default_sort_criteria + [['spent_on', 'desc']] + end def base_scope TimeEntry.visible. @@ -114,7 +118,7 @@ class TimeEntryQuery < Query end def results_scope(options={}) - order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) + order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?) base_scope. order(order_option). |