module SimpleListing::Sortable

Constants

SORT_DIRECTIONS

Public Instance Methods

inverted_sort_direction() click to toggle source
# File lib/simple_listing/sortable.rb, line 50
def inverted_sort_direction
  sort_direction == :asc ? :desc : :asc
end
perform() click to toggle source
Calls superclass method
# File lib/simple_listing/sortable.rb, line 30
def perform
  super
  apply_sorting if should_be_sorted?
  scope
end
should_be_sorted?() click to toggle source
# 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_direction() click to toggle source
# 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_value() click to toggle source
# 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

Private Instance Methods

apply_sorting() click to toggle source
# File lib/simple_listing/sortable.rb, line 60
def apply_sorting
  self.scope = if handler = sorting_handlers[sort_value]
                 handler.call(scope, sort_direction, self)
               else
                 scope.order(sort_value => sort_direction)
               end
end
sortable_by?(value) click to toggle source
# File lib/simple_listing/sortable.rb, line 68
def sortable_by?(value)
  sortable_keys.include? value
end