Class: Redmine::SortCriteria

Inherits:
Array
  • Object
show all
Defined in:
lib/redmine/sort_criteria.rb

Overview

Since:

  • 3.4.0

Instance Method Summary collapse

Methods included from Diffable

#diff, #patch, #replacenextlarger, #reverse_hash

Constructor Details

#initialize(arg = nil) ⇒ SortCriteria

Returns a new instance of SortCriteria



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/redmine/sort_criteria.rb', line 20

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.respond_to?(:values)
    replace arg.values
  elsif arg
    raise ArgumentError.new("SortCriteria#new takes an Array, String or Hash, not a #{arg.class.name}.")
  end
  normalize!
end

Instance Method Details

#add(*args) ⇒ Object



49
50
51
# File 'lib/redmine/sort_criteria.rb', line 49

def add(*args)
  self.class.new(self).add!(*args)
end

#add!(key, asc) ⇒ Object



42
43
44
45
46
47
# File 'lib/redmine/sort_criteria.rb', line 42

def add!(key, asc)
  key = key.to_s
  delete_if {|k,o| k == key}
  prepend([key, asc])
  normalize!
end

#first_asc?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/redmine/sort_criteria.rb', line 57

def first_asc?
  first.try(:last) == 'asc'
end

#first_keyObject



53
54
55
# File 'lib/redmine/sort_criteria.rb', line 53

def first_key
  first.try(:first)
end

#key_at(arg) ⇒ Object



61
62
63
# File 'lib/redmine/sort_criteria.rb', line 61

def key_at(arg)
  self[arg].try(:first)
end

#order_at(arg) ⇒ Object



65
66
67
# File 'lib/redmine/sort_criteria.rb', line 65

def order_at(arg)
  self[arg].try(:last)
end

#order_for(key) ⇒ Object



69
70
71
# File 'lib/redmine/sort_criteria.rb', line 69

def order_for(key)
  detect {|k, order| key.to_s == k}.try(:last)
end

#sort_clause(sortable_columns) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/redmine/sort_criteria.rb', line 73

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

#to_aObject



38
39
40
# File 'lib/redmine/sort_criteria.rb', line 38

def to_a
  Array.new(self)
end

#to_paramObject



34
35
36
# File 'lib/redmine/sort_criteria.rb', line 34

def to_param
  self.collect {|k,o| k + (o == 'desc' ? ':desc' : '')}.join(',')
end