Module: Tableficate::Helper

Defined in:
lib/tableficate/helper.rb

Instance Method Summary collapse

Instance Method Details

#table_for(rows, options = {}) {|t| ... } ⇒ Object

Yields:

  • (t)


3
4
5
6
7
# File 'lib/tableficate/helper.rb', line 3

def table_for(rows, options = {})
  t = Tableficate::Table.new(self, rows, options, rows.tableficate_get_data)
  yield(t)
  t.template.render(partial: Tableficate::Utils::template_path(t.template, 'table_for', t.theme), locals:  {table: t})
end

#tableficate_check_box_tags(filter) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/tableficate/helper.rb', line 76

def tableficate_check_box_tags(filter)
  if filter.collection.empty?
    check_box_tag(filter.field_name, true, filter.field_value(params[filter.table.as]) == 'true', filter.attrs)
  else
    tableficate_collection_of_tags(filter)
  end
end

#tableficate_data_tag(row, column) ⇒ Object



18
19
20
21
# File 'lib/tableficate/helper.rb', line 18

def tableficate_data_tag(row, column)
  table = column.table
  render partial: Tableficate::Utils::template_path(table.template, 'data', table.theme), locals: {row: row, column: column}
end

#tableficate_filter_form_tag(table) ⇒ Object



28
29
30
# File 'lib/tableficate/helper.rb', line 28

def tableficate_filter_form_tag(table)
  render partial: Tableficate::Utils::template_path(table.template, 'filters/form', table.theme), locals: {table: table}
end

#tableficate_filter_tag(filter) ⇒ Object



32
33
34
35
# File 'lib/tableficate/helper.rb', line 32

def tableficate_filter_tag(filter)
  table = filter.table
  render partial: Tableficate::Utils::template_path(table.template, filter.template, table.theme), locals: {filter: filter}
end

#tableficate_header_tag(column) ⇒ Object



13
14
15
16
# File 'lib/tableficate/helper.rb', line 13

def tableficate_header_tag(column)
  table = column.table
  render partial: Tableficate::Utils::template_path(table.template, 'header', table.theme), locals: {column: column}
end

#tableficate_label_tag(filter) ⇒ Object



37
38
39
# File 'lib/tableficate/helper.rb', line 37

def tableficate_label_tag(filter)
  label_tag(filter.field_name, filter.label, filter.label_options)
end

#tableficate_radio_tags(filter) ⇒ Object



72
73
74
# File 'lib/tableficate/helper.rb', line 72

def tableficate_radio_tags(filter)
  tableficate_collection_of_tags(filter)
end

#tableficate_row_tag(row, columns) ⇒ Object



23
24
25
26
# File 'lib/tableficate/helper.rb', line 23

def tableficate_row_tag(row, columns)
  table = columns.first.table
  render partial: Tableficate::Utils::template_path(table.template, 'row', table.theme), locals: {row: row, columns: columns}
end

#tableficate_select_tag(filter) ⇒ Object



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

def tableficate_select_tag(filter)
  field_value = filter.field_value(params[filter.table.as])

  collection = filter.collection

  if field_value.present? and collection.is_a?(String)
    Array.wrap(field_value).each do |fv|
      if collection.match(/<option[^>]*value\s*=/)
        collection.gsub!(/(<option[^>]*value\s*=\s*['"]?#{fv}[^>]*)/, '\1 selected="selected"')
      else
        collection.gsub!(/>#{fv}</, " selected=\"selected\">#{fv}<")
      end
    end
  elsif not collection.is_a?(String)
    collection = Tableficate::Filter::Collection.new(collection, selected: field_value).map {|choice|
      html_attributes = choice.attrs.length > 0 ? ' ' + choice.attrs.map {|k, v| %(#{k.to_s}="#{v}")}.join(' ') : ''
      selected_attribute = choice.selected? ? ' selected="selected"' : ''

      %(<option value="#{ERB::Util.html_escape(choice.value)}"#{selected_attribute}#{html_attributes}>#{ERB::Util.html_escape(choice.name)}</option>)
    }.join("\n")
  end

  collection = collection.html_safe

  select_tag(filter.field_name, collection, filter.attrs)
end

#tableficate_table_tag(table) ⇒ Object



9
10
11
# File 'lib/tableficate/helper.rb', line 9

def tableficate_table_tag(table)
  render partial: Tableficate::Utils::template_path(table.template, 'table', table.theme), locals: {table: table}
end

#tableficate_text_field_tag(filter) ⇒ Object



41
42
43
# File 'lib/tableficate/helper.rb', line 41

def tableficate_text_field_tag(filter)
  text_field_tag(filter.field_name, filter.field_value(params[filter.table.as]), filter.attrs)
end