Module: PageAndSortHelper
- Included in:
- MessagesController
- Defined in:
- lib/page_and_sort_helper.rb
Defined Under Namespace
Modules: Controller
Instance Method Summary collapse
-
#sort_column ⇒ Symbol
read the sort column from the params hash, returning it as a symbol, or nil.
-
#sort_direction(default = nil) ⇒ Symbol
return the sort direction, defaulting to ascending, as a symbol.
-
#sortable_title(field, options = {}) ⇒ Object
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.
Instance Method Details
#sort_column ⇒ Symbol
read the sort column from the params hash, returning it as a symbol, or nil
5 6 7 |
# File 'lib/page_and_sort_helper.rb', line 5 def sort_column params[:sort].try(:to_sym) || @_sort_order end |
#sort_direction(default = nil) ⇒ Symbol
return the sort direction, defaulting to ascending, as a symbol. :asc or :desc
11 12 13 14 |
# 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 = {}) ⇒ Object
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
.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/page_and_sort_helper.rb', line 21 def sortable_title(field, ={}) klass = [:class] title = h([: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 |