Class: SortHelper::SortCriteria
- Inherits:
-
Object
- Object
- SortHelper::SortCriteria
- Defined in:
- app/helpers/sort_helper.rb
Instance Method Summary collapse
- #add(*args) ⇒ Object
- #add!(key, asc) ⇒ Object
- #available_criteria=(criteria) ⇒ Object
- #criteria=(arg) ⇒ Object
- #empty? ⇒ Boolean
- #first_asc? ⇒ Boolean
- #first_key ⇒ Object
- #from_param(param) ⇒ Object
-
#initialize ⇒ SortCriteria
constructor
A new instance of SortCriteria.
- #to_param ⇒ Object
- #to_sql ⇒ Object
Constructor Details
#initialize ⇒ SortCriteria
Returns a new instance of SortCriteria.
56 57 58 |
# File 'app/helpers/sort_helper.rb', line 56 def initialize @criteria = [] end |
Instance Method Details
#add(*args) ⇒ Object
96 97 98 99 100 |
# File 'app/helpers/sort_helper.rb', line 96 def add(*args) r = self.class.new.from_param(to_param) r.add!(*args) r end |
#add!(key, asc) ⇒ Object
90 91 92 93 94 |
# File 'app/helpers/sort_helper.rb', line 90 def add!(key, asc) @criteria.delete_if {|k,o| k == key} @criteria = [[key, asc]] + @criteria normalize! end |
#available_criteria=(criteria) ⇒ Object
60 61 62 63 64 65 |
# File 'app/helpers/sort_helper.rb', line 60 def available_criteria=(criteria) unless criteria.is_a?(Hash) criteria = criteria.inject({}) {|h,k| h[k] = k; h} end @available_criteria = criteria end |
#criteria=(arg) ⇒ Object
72 73 74 75 |
# File 'app/helpers/sort_helper.rb', line 72 def criteria=(arg) @criteria = arg normalize! end |
#empty? ⇒ Boolean
110 111 112 |
# File 'app/helpers/sort_helper.rb', line 110 def empty? @criteria.empty? end |
#first_asc? ⇒ Boolean
106 107 108 |
# File 'app/helpers/sort_helper.rb', line 106 def first_asc? @criteria.first && @criteria.first.last end |
#first_key ⇒ Object
102 103 104 |
# File 'app/helpers/sort_helper.rb', line 102 def first_key @criteria.first && @criteria.first.first end |
#from_param(param) ⇒ Object
67 68 69 70 |
# File 'app/helpers/sort_helper.rb', line 67 def from_param(param) @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]} normalize! end |
#to_param ⇒ Object
77 78 79 |
# File 'app/helpers/sort_helper.rb', line 77 def to_param @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',') end |
#to_sql ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'app/helpers/sort_helper.rb', line 81 def to_sql sql = @criteria.collect do |k,o| if s = @available_criteria[k] (o ? s.to_a : s.to_a.collect {|c| append_desc(c)}).join(', ') end end.compact.join(', ') sql.blank? ? nil : sql end |