Module: ListControls::RailsHelpers

Defined in:
lib/list_controls/rails_helpers.rb

Instance Method Summary collapse

Instance Method Details

#sort(filters, options = {}, html_options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/list_controls/rails_helpers.rb', line 3

def sort(filters, options = {}, html_options = {})
  options[:params_scope] ||= :filters
  options[:ascend_scope] ||= "ascend_by_#{options[:by]}"
  options[:descend_scope] ||= "descend_by_#{options[:by]}"
  options[:sort_class] ||= 'sort'
  
  current_order = filters.order
  
  ascending = current_order == options[:ascend_scope]
  new_scope = ascending ? options[:descend_scope] : options[:ascend_scope]
  selected = [options[:ascend_scope], options[:descend_scope]].include?(current_order)
  
  css_classes = html_options[:class] ? html_options[:class].split(" ") : []

  if selected
    if ascending
      css_classes << options[:sort_class] + "-up"
    else
      css_classes << options[:sort_class] + "-down"
    end
  else
    css_classes << options[:sort_class]
  end
  
  html_options[:class] = css_classes.join(" ")

  url_options = {
    options[:params_scope] => { :order => new_scope }
  }.deep_merge(options[:params] || {})
  
  link_to options[:as] || options[:by], url_for(url_options), html_options
end