Module: TableFor::ViewAdditions::ClassMethods

Defined in:
lib/table_for/view_additions.rb

Instance Method Summary collapse

Instance Method Details

#table_for(records, options = {}, &block) ⇒ Object



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

def table_for(records, options={}, &block)
  TableFor::Base.new(self, options.merge(:variable => "table", :records => records, :use_partials => false)).render_template("table_for/table_for", &block)
end

#table_for_cell_data(record, column, options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/table_for/view_additions.rb', line 38

def table_for_cell_data(record, column, options)
  if (options[:transformation])
    if options[:transformation].is_a?(Proc)
      options[:transformation].call(*[record, column, options][0, options[:transformation].arity])
    else
      record.send(column.name).try(*options[:transformation])
    end
  else
    record.send(column.name)
  end
end

#table_for_header_html(table, column, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/table_for/view_additions.rb', line 8

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


18
19
20
21
22
23
24
25
26
27
# File 'lib/table_for/view_additions.rb', line 18

def table_for_sort_link(column, options={})
  order = options[:order] ? options[:order].to_s : column.name.to_s
  label = (options[:label] ? options[:label] : column.name.to_s.titleize)
  sort_mode = (params[:order] != order || params[:sort_mode] == "reset") ? "asc" : (params[:sort_mode] == "desc" ? "reset" : "desc")
  parameters = params.merge(:order => order, :sort_mode => sort_mode)
  parameters.delete(:action)
  parameters.delete(:controller)
  url = options[:sort_url] ? options[:sort_url] : ""
  link_to label, "#{url}?#{parameters.to_query}"
end

#table_for_table_html(options) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/table_for/view_additions.rb', line 29

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