Class: ArPagination::Helpers::Sort

Inherits:
Object
  • Object
show all
Defined in:
lib/ar_pagination/helpers/sort.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ Sort

Returns a new instance of Sort.



4
5
6
# File 'lib/ar_pagination/helpers/sort.rb', line 4

def initialize(scope)
  @scope = scope
end

Instance Method Details

#sort(sort) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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