class ArPagination::Helpers::Sort

Public Class Methods

new(scope) click to toggle source
# File lib/ar_pagination/helpers/sort.rb, line 4
def initialize(scope)
  @scope = scope
end

Public Instance Methods

sort(sort) click to toggle source
# File lib/ar_pagination/helpers/sort.rb, line 8
def sort(sort)
  order_hash = {}
  order_options = sort.split(',') if sort
  Array.wrap(order_options).each do |order_option|
    case order_option.first
    when '-'
      direction = :desc
    when '+'
      direction = :asc
    else
      next
    end
    sort_attr = order_option[1..-1]
    order_hash[sort_attr.to_sym] = direction
  end
  @scope = @scope.order(order_hash) unless order_hash.empty?
  @scope
end