Class: Mincer::Processors::Sorting::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/mincer/processors/sorting/processor.rb

Instance Method Summary collapse

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

#applyObject



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_orderObject



45
46
47
# File 'lib/mincer/processors/sorting/processor.rb', line 45

def default_order
  @mincer.try(:default_sort_order)
end

#default_sortObject



37
38
39
# File 'lib/mincer/processors/sorting/processor.rb', line 37

def default_sort
  @mincer.try(:default_sort_attribute)
end

#orderObject



41
42
43
# File 'lib/mincer/processors/sorting/processor.rb', line 41

def order
  @args[::Mincer.config.sorting.order_param_name]
end

#order_attrObject



29
30
31
# File 'lib/mincer/processors/sorting/processor.rb', line 29

def order_attr
  (%w{asc desc}.include?(order.try(:downcase)) && order)
end

#sortObject



33
34
35
# File 'lib/mincer/processors/sorting/processor.rb', line 33

def sort
  @args[::Mincer.config.sorting.sort_param_name]
end

#sort_attrObject



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_stringObject



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