Module: SortableTable::App::Helpers::ApplicationHelper::InstanceMethods

Defined in:
lib/sortable_table/app/helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#class_name_for_sortable_table_header_tag(opts) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/sortable_table/app/helpers/application_helper.rb', line 23

def class_name_for_sortable_table_header_tag(opts)
  if default_sort_to_most_recent? opts
    'descending'
  elsif re_sort? opts
    params[:order]
  else
    nil
  end
end

#default_sort_to_most_recent?(opts) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/sortable_table/app/helpers/application_helper.rb', line 33

def default_sort_to_most_recent?(opts)
  params[:sort].nil? && opts[:sort] == 'date'
end


55
56
57
58
59
60
61
62
63
# File 'lib/sortable_table/app/helpers/application_helper.rb', line 55

def link_sort_order(opts)
  if default_sort_to_most_recent? opts
    'ascending'
  elsif re_sort? opts
    reverse_order params[:order]
  else
    'ascending'
  end
end

#name_with_desc(opts) ⇒ Object



45
46
47
48
49
# File 'lib/sortable_table/app/helpers/application_helper.rb', line 45

def name_with_desc(opts)
  return opts[:name] unless opts[:sort] == params[:sort]
  
  link_sort_order(opts) == 'descending' ? opts[:name] + " (asc)" : opts[:name] + " (desc)"
end

#re_sort?(opts) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/sortable_table/app/helpers/application_helper.rb', line 37

def re_sort?(opts)
  params[:sort] == opts[:sort]
end

#reverse_order(order) ⇒ Object



41
42
43
# File 'lib/sortable_table/app/helpers/application_helper.rb', line 41

def reverse_order(order)
  order == 'ascending' ? 'descending' : 'ascending'
end

#sortable_table_header(opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
# File 'lib/sortable_table/app/helpers/application_helper.rb', line 13

def sortable_table_header(opts = {})
  raise ArgumentError if opts[:name].nil? || opts[:sort].nil?
  anchor = opts[:anchor].blank? ? "" : "##{opts[:anchor]}"
   :th, 
    link_to(name_with_desc(opts), 
      sortable_url(opts) + anchor, 
      :title => opts[:title]),
    :class => class_name_for_sortable_table_header_tag(opts)
end

#sortable_url(opts) ⇒ Object



51
52
53
# File 'lib/sortable_table/app/helpers/application_helper.rb', line 51

def sortable_url(opts)
  url_for(params.merge(:sort => opts[:sort], :order => link_sort_order(opts), :page => 1))
end