"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/lib/redmine/sort_criteria.rb" (6 Apr 2020, 2836 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "sort_criteria.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 module Redmine
   21   class SortCriteria < Array
   22     def initialize(arg=nil)
   23       super()
   24       if arg.is_a?(Array)
   25         replace arg
   26       elsif arg.is_a?(String)
   27         replace arg.split(',').collect {|s| s.split(':')[0..1]}
   28       elsif arg.respond_to?(:values)
   29         replace arg.values
   30       elsif arg
   31         raise ArgumentError.new("SortCriteria#new takes an Array, String or Hash, not a #{arg.class.name}.")
   32       end
   33       normalize!
   34     end
   35 
   36     def to_param
   37       self.collect {|k,o| k + (o == 'desc' ? ':desc' : '')}.join(',')
   38     end
   39 
   40     def to_a
   41       Array.new(self)
   42     end
   43 
   44     def add!(key, asc)
   45       key = key.to_s
   46       delete_if {|k,o| k == key}
   47       prepend([key, asc])
   48       normalize!
   49     end
   50 
   51     def add(*args)
   52       self.class.new(self).add!(*args)
   53     end
   54 
   55     def first_key
   56       first.try(:first)
   57     end
   58 
   59     def first_asc?
   60       first.try(:last) == 'asc'
   61     end
   62 
   63     def key_at(arg)
   64       self[arg].try(:first)
   65     end
   66 
   67     def order_at(arg)
   68       self[arg].try(:last)
   69     end
   70 
   71     def order_for(key)
   72       detect {|k, order| key.to_s == k}.try(:last)
   73     end
   74 
   75     def sort_clause(sortable_columns)
   76       if sortable_columns.is_a?(Array)
   77         sortable_columns = sortable_columns.inject({}) {|h,k| h[k]=k; h}
   78       end
   79 
   80       sql = self.collect do |k,o|
   81         if s = sortable_columns[k]
   82           s = [s] unless s.is_a?(Array)
   83           s.collect {|c| append_order(c, o)}
   84         end
   85       end.flatten.compact
   86       sql.blank? ? nil : sql
   87     end
   88 
   89     private
   90 
   91     def normalize!
   92       self.reject! {|s| s.first.blank? }
   93       self.uniq! {|s| s.first }
   94       self.collect! {|s| s = Array(s); [s.first, (s.last == false || s.last.to_s == 'desc') ? 'desc' : 'asc']}
   95       self.replace self.first(3)
   96     end
   97 
   98     # Appends ASC/DESC to the sort criterion unless it has a fixed order
   99     def append_order(criterion, order)
  100       if / (asc|desc)$/i.match?(criterion)
  101         criterion
  102       else
  103         Arel.sql "#{criterion} #{order.to_s.upcase}"
  104       end
  105     end
  106   end
  107 end