Module: SortingHelper

Defined in:
lib/sorting_helper.rb,
lib/sorting_helper/version.rb

Constant Summary collapse

VERSION =
'0.1.0'

Instance Method Summary collapse

Instance Method Details

#sorting_columnObject



5
6
7
# File 'lib/sorting_helper.rb', line 5

def sorting_column
  params[:sort][/^\-?(.+)/, 1] if params[:sort].present?
end

#sorting_directionObject



9
10
11
# File 'lib/sorting_helper.rb', line 9

def sorting_direction
  params[:sort] =~ /^\-/ ? :desc : :asc if params[:sort].present?
end


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sorting_helper.rb', line 22

def sorting_link(column, label)
  sorting_url(column) do |url, direction|
    link = link_to(label, url)

    if column.to_s == sorting_column
      link += direction == :asc ? ' ▴' : ' ▾'
    end

    raw link
  end
end

#sorting_url(column, direction = nil, &blk) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/sorting_helper.rb', line 13

def sorting_url(column, direction = nil, &blk)
  direction = sorting_direction || :asc

  column = column.to_s
  column.prepend('-') if direction == :asc

  blk.call(url_for(sort: column), direction)
end