module PageAndSortHelper

Public Instance Methods

sort_column() click to toggle source

read the sort column from the params hash, returning it as a symbol, or nil @return [Symbol]

# File lib/page_and_sort_helper.rb, line 5
def sort_column
  params[:sort].try(:to_sym) || @_sort_order
end
sort_direction(default=nil) click to toggle source

return the sort direction, defaulting to ascending, as a symbol. :asc or :desc @return [Symbol]

# File lib/page_and_sort_helper.rb, line 11
def sort_direction(default=nil)
  direction = (params[:sort_direction] || @_sort_direction || default || :asc).to_sym
  [:asc, :desc].include?(direction) ? direction : :asc
end
sortable_title(field, options={}) click to toggle source

create a link for the field by localising the field name and appending an up or down arrow indicating the current sort order if this field is the currently sorted field. The sort order in the link is opposite to the current sort order so that the user can toggle the sort order by clicking the link again.

This defaults to looking up the field name on the class used in the controller for page_and_sort.

# File lib/page_and_sort_helper.rb, line 21
def sortable_title(field, options={})
  klass = options[:class]
  title = h(options[:title] || (klass || @_sort_klass).human_attribute_name(field))

  direction = :asc
  if sort_column == field
    if sort_direction == :asc
      title += " ↓".html_safe
      direction = :desc
    else
      title += " ↑".html_safe
    end
  end

  link_to title, params.merge(sort: field, sort_direction: direction)
end