summaryrefslogtreecommitdiffstats
path: root/lib/redmine/sort_criteria.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2017-03-13 19:17:59 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2017-03-13 19:17:59 +0000
commit71d88e55352a51b20dd5e2a4a44aa695834a36b9 (patch)
tree8d5708333c51b7da7319a5c182350758b00dc535 /lib/redmine/sort_criteria.rb
parentb91489ce6bb0943cff50946af6e64be65d3ccd80 (diff)
downloadredmine-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 'lib/redmine/sort_criteria.rb')
-rw-r--r--lib/redmine/sort_criteria.rb104
1 files changed, 104 insertions, 0 deletions
diff --git a/lib/redmine/sort_criteria.rb b/lib/redmine/sort_criteria.rb
new file mode 100644
index 000000000..5fd2e978d
--- /dev/null
+++ b/lib/redmine/sort_criteria.rb
@@ -0,0 +1,104 @@
+# Redmine - project management software
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+module Redmine
+ class SortCriteria < Array
+ def initialize(arg=nil)
+ super()
+ if arg.is_a?(Array)
+ replace arg
+ elsif arg.is_a?(String)
+ replace arg.split(',').collect {|s| s.split(':')[0..1]}
+ elsif arg.is_a?(Hash)
+ replace arg.values
+ elsif arg
+ raise ArgumentError.new("SortCriteria#new takes an Array, String or Hash, not a #{arg.class.name}.")
+ end
+ normalize!
+ end
+
+ def to_param
+ self.collect {|k,o| k + (o == 'desc' ? ':desc' : '')}.join(',')
+ end
+
+ def to_a
+ Array.new(self)
+ end
+
+ def add!(key, asc)
+ key = key.to_s
+ delete_if {|k,o| k == key}
+ prepend([key, asc])
+ normalize!
+ end
+
+ def add(*args)
+ self.class.new(self).add!(*args)
+ end
+
+ def first_key
+ first.try(:first)
+ end
+
+ def first_asc?
+ first.try(:last) == 'asc'
+ end
+
+ def key_at(arg)
+ self[arg].try(:first)
+ end
+
+ def order_at(arg)
+ self[arg].try(:last)
+ end
+
+ def order_for(key)
+ detect {|k, order| key.to_s == k}.try(:last)
+ end
+
+ def sort_clause(sortable_columns)
+ if sortable_columns.is_a?(Array)
+ sortable_columns = sortable_columns.inject({}) {|h,k| h[k]=k; h}
+ end
+
+ sql = self.collect do |k,o|
+ if s = sortable_columns[k]
+ s = [s] unless s.is_a?(Array)
+ s.collect {|c| append_order(c, o)}
+ end
+ end.flatten.compact
+ sql.blank? ? nil : sql
+ end
+
+ private
+
+ def normalize!
+ self.collect! {|s| s = Array(s); [s.first, (s.last == false || s.last.to_s == 'desc') ? 'desc' : 'asc']}
+ self.slice!(3)
+ self
+ end
+
+ # Appends ASC/DESC to the sort criterion unless it has a fixed order
+ def append_order(criterion, order)
+ if criterion =~ / (asc|desc)$/i
+ criterion
+ else
+ "#{criterion} #{order.to_s.upcase}"
+ end
+ end
+ end
+end