Class: Redmine::SortCriteria

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

Instance Method Summary collapse

Methods included from Redmine::StringArrayDiff::Diffable

#diff, #patch, #replacenextlarger, #reverse_hash

Constructor Details

#initialize(arg = nil) ⇒ SortCriteria

Returns a new instance of SortCriteria.



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

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



51
52
53
# File 'lib/redmine/sort_criteria.rb', line 51

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

#add!(key, asc) ⇒ Object



44
45
46
47
48
49
# File 'lib/redmine/sort_criteria.rb', line 44

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

#first_asc?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/redmine/sort_criteria.rb', line 59

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

#first_keyObject



55
56
57
# File 'lib/redmine/sort_criteria.rb', line 55

def first_key
  first.try(:first)
end

#key_at(arg) ⇒ Object



63
64
65
# File 'lib/redmine/sort_criteria.rb', line 63

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

#order_at(arg) ⇒ Object



67
68
69
# File 'lib/redmine/sort_criteria.rb', line 67

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

#order_for(key) ⇒ Object



71
72
73
# File 'lib/redmine/sort_criteria.rb', line 71

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

#sort_clause(sortable_columns) ⇒ Object



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

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



40
41
42
# File 'lib/redmine/sort_criteria.rb', line 40

def to_a
  Array.new(self)
end

#to_paramObject



36
37
38
# File 'lib/redmine/sort_criteria.rb', line 36

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