Module: SortableTable::ActionViewExtension

Defined in:
lib/sortable_table/helpers/action_view_extension.rb

Instance Method Summary collapse

Instance Method Details

#sort_by(column, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sortable_table/helpers/action_view_extension.rb', line 3

def sort_by(column, options = {})
  current_column = options.delete(:current_column) || SortableTable::SortColumn.new(nil, nil)
  title = options.delete(:title) || column.titleize
  prefix = options.delete(:prefix)
  is_current_column = column == current_column.column
  options_class = options.delete(:class)
  css_class = is_current_column ? "current #{current_column.direction} #{options_class}" : options_class
  direction = is_current_column && current_column.direction == 'asc' ? 'desc' : 'asc'
  sort_params = { "#{prefix}sort" => column,
                              "#{prefix}direction" => direction,
                              "#{prefix}page" => nil }
  if options.has_key?(:route_method)
    goto = options.delete(:route_method).call sort_params
  else
    goto = params.merge( sort_params )
  end
  link_to title, goto, options.merge( {class: css_class } )
end