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


49
50
51
52
53
54
55
56
57
# File 'lib/sortable_table/app/helpers/application_helper.rb', line 49

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

#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(opts[:name], 
      sortable_url(opts) + anchor, 
      :title => opts[:title]),
    :class => class_name_for_sortable_table_header_tag(opts)
end

#sortable_url(opts) ⇒ Object



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

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