Class: Mincer::Processors::Sorting::Processor
- Inherits:
-
Object
- Object
- Mincer::Processors::Sorting::Processor
- Defined in:
- lib/mincer/processors/sorting/processor.rb
Instance Method Summary collapse
- #apply ⇒ Object
- #default_order ⇒ Object
- #default_sort ⇒ Object
-
#initialize(mincer) ⇒ Processor
constructor
A new instance of Processor.
- #order ⇒ Object
- #order_attr ⇒ Object
- #sort ⇒ Object
- #sort_attr ⇒ Object
- #sort_string ⇒ Object
Constructor Details
#initialize(mincer) ⇒ Processor
Returns a new instance of Processor.
5 6 7 |
# File 'lib/mincer/processors/sorting/processor.rb', line 5 def initialize(mincer) @mincer, @args, @relation = mincer, mincer.args, mincer.relation end |
Instance Method Details
#apply ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mincer/processors/sorting/processor.rb', line 9 def apply sorting_sting = sort_string if sorting_sting.present? @mincer.sort_attribute = (sort_attr || default_sort).to_s @mincer.sort_order = (order_attr || default_order).to_s @mincer.default_sorting = sort_attr.blank? && order_attr.blank? @relation.order(sorting_sting) else @relation end end |
#default_order ⇒ Object
45 46 47 |
# File 'lib/mincer/processors/sorting/processor.rb', line 45 def default_order @mincer.try(:default_sort_order) end |
#default_sort ⇒ Object
37 38 39 |
# File 'lib/mincer/processors/sorting/processor.rb', line 37 def default_sort @mincer.try(:default_sort_attribute) end |
#order ⇒ Object
41 42 43 |
# File 'lib/mincer/processors/sorting/processor.rb', line 41 def order @args[::Mincer.config.sorting.order_param_name] end |
#order_attr ⇒ Object
29 30 31 |
# File 'lib/mincer/processors/sorting/processor.rb', line 29 def order_attr (%w{asc desc}.include?(order.try(:downcase)) && order) end |
#sort ⇒ Object
33 34 35 |
# File 'lib/mincer/processors/sorting/processor.rb', line 33 def sort @args[::Mincer.config.sorting.sort_param_name] end |
#sort_attr ⇒ Object
25 26 27 |
# File 'lib/mincer/processors/sorting/processor.rb', line 25 def sort_attr (@mincer.send(:allowed_sort_attributes).include?(sort) && sort) end |
#sort_string ⇒ Object
21 22 23 |
# File 'lib/mincer/processors/sorting/processor.rb', line 21 def sort_string [(sort_attr || default_sort), (order_attr || default_order)].compact.uniq.join(' ') end |