Module: TableFor::HelperMethods::InstanceMethods

Included in:
Base
Defined in:
lib/table_for/helper_methods.rb

Instance Method Summary collapse

Instance Method Details

#cell_content(record, column, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/table_for/helper_methods.rb', line 45

def cell_content(record, column, options={})
  if options[:link_url] || options[:link_action] || options[:link_method] || options[:link_confirm] || options[:link]
    url = options[:link_url] ? call_with_params(options[:link_url], record) : [options[:link_action], options[:link_namespace], record].flatten
  end

  if options[:formatter]
    if options[:formatter].is_a?(Proc)
      content = call_with_params(options[:formatter], record.send(column.name), options)
    else
      content = record.send(column.name).try(*options[:formatter])
    end
  elsif options[:data] || [:edit, :show, :delete].include?(column.name)
    content = call_with_params(options[:data], record)
  else
    content = record.send(column.name)
  end

  if content.blank? || url.blank? || options[:link] == false
    content
  elsif url
    view.link_to content, url, {:method => options[:link_method], :confirm => options[:link_confirm]}.merge(options[:link_html])
  end
end


8
9
10
# File 'lib/table_for/helper_methods.rb', line 8

def footer(options={}, &block)
  define(:footer_content, options, &block)
end

#header(name, options = {}, &block) ⇒ Object



4
5
6
# File 'lib/table_for/helper_methods.rb', line 4

def header(name, options={}, &block)
  define("#{name.to_s}_header", options.reverse_merge(:header => true), &block)
end

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



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/table_for/helper_methods.rb', line 31

def header_cell_content(column, options={})
  unless options[:header] == false
    header_sort_link(column, options) do
      if options[:header]
        call_with_params options[:header], column
      elsif column.anonymous
        nil
      else
        column.name.to_s.titleize
      end
    end
  end
end

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



21
22
23
24
25
26
27
28
29
# File 'lib/table_for/helper_methods.rb', line 21

def header_column_html(column, options={})
  header_column_html = call_each_hash_value_with_params(options[:header_column_html], column)
  if options[:sortable]
    order = options[:order] ? options[:order].to_s : column.name.to_s
    sort_class = (view.params[:order] != order || view.params[:sort_mode] == "reset") ? "sorting" : (view.params[:sort_mode] == "desc" ? "sorting_desc" : "sorting_asc")
    header_column_html[:class] = (header_column_html[:class] ? "#{header_column_html[:class]} #{sort_class}" : sort_class)
  end
  header_column_html
end


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/table_for/helper_methods.rb', line 73

def header_sort_link(column, options={}, &block)
  if options[:sortable] && (options[:header] || !column.anonymous)
    order = options[:order] ? options[:order].to_s : column.name.to_s
    sort_mode = (view.params[:order] != order || view.params[:sort_mode] == "reset") ? "asc" : (view.params[:sort_mode] == "desc" ? "reset" : "desc")
    parameters = view.params.merge(:order => order, :sort_mode => sort_mode)
    parameters.delete(:action)
    parameters.delete(:controller)
    url = options[:sort_url] ? options[:sort_url] : ""
    view.link_to view.capture(self, &block), "#{url}?#{parameters.to_query}"
  else
    view.capture(self, &block)
  end
end

#set_current_record(record) ⇒ Object



69
70
71
# File 'lib/table_for/helper_methods.rb', line 69

def set_current_record(record)
  self.current_record = record
end

#table_html(options) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/table_for/helper_methods.rb', line 12

def table_html(options)
  if options[:table_html]
    options[:table_html].reverse_merge!(:class => TableFor.default_table_class) if TableFor.default_table_class
    options[:table_html]
  elsif TableFor.default_table_class
    {:class => TableFor.default_table_class}
  end
end