Module: SimpleListing::Sortable

Extended by:
ActiveSupport::Concern
Included in:
Standard
Defined in:
lib/simple_listing/sortable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

SORT_DIRECTIONS =
%w{asc desc}.freeze

Instance Method Summary collapse

Instance Method Details

#inverted_sort_directionObject



50
51
52
# File 'lib/simple_listing/sortable.rb', line 50

def inverted_sort_direction
  sort_direction == :asc ? :desc : :asc
end

#performObject



30
31
32
33
34
# File 'lib/simple_listing/sortable.rb', line 30

def perform
  super
  apply_sorting if should_be_sorted?
  scope
end

#should_be_sorted?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/simple_listing/sortable.rb', line 54

def should_be_sorted?
  params[config[:sort_by_param_key]] && params[config[:sort_direction_param_key]]
end

#sort_directionObject



42
43
44
45
46
47
48
# File 'lib/simple_listing/sortable.rb', line 42

def sort_direction
  @sort_direction ||= begin
    direction = params[config[:sort_direction_param_key]]
    guard("incorrect sorting direction '#{direction}'") { direction.in? SORT_DIRECTIONS }
    direction.to_sym
  end
end

#sort_valueObject



36
37
38
39
40
# File 'lib/simple_listing/sortable.rb', line 36

def sort_value
  @sort_value ||= params[config[:sort_by_param_key]].tap do |value|
    guard("incorrect sorting value '#{value}'") { sortable_by?(value) }
  end
end