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
# File 'lib/mincer/processors/sorting/processor.rb', line 9

def apply
  relation = @relation.order(sort_string)
  @mincer.sort_attribute, @mincer.sort_order = sort_attr, order_attr
  relation
end

#default_orderObject



43
44
45
# File 'lib/mincer/processors/sorting/processor.rb', line 43

def default_order
  @mincer.try(:default_sort_order)
end

#default_sortObject



35
36
37
# File 'lib/mincer/processors/sorting/processor.rb', line 35

def default_sort
  @mincer.try(:default_sort_attribute)
end

#orderObject



39
40
41
# File 'lib/mincer/processors/sorting/processor.rb', line 39

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

#order_attrObject



27
28
29
# File 'lib/mincer/processors/sorting/processor.rb', line 27

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

#sortObject



31
32
33
# File 'lib/mincer/processors/sorting/processor.rb', line 31

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

#sort_attrObject



23
24
25
# File 'lib/mincer/processors/sorting/processor.rb', line 23

def sort_attr
  @mincer.send(:allowed_sort_attributes).include?(sort) && sort
end

#sort_stringObject



15
16
17
18
19
20
21
# File 'lib/mincer/processors/sorting/processor.rb', line 15

def sort_string
  if sort_attr
    "#{sort_attr} #{order_attr || default_order}"
  else
    "#{default_sort} #{order_attr || default_order}"
  end
end