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.render
end

#tableficate_check_box_tags(filter, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tableficate/helper.rb', line 79

def tableficate_check_box_tags(filter, &block)
  field_value = filter.field_value(params[filter.table.as])

  collection = Tableficate::Filter::Collection.new(filter.options[:collection], selected: filter.field_value(params[filter.table.as]))

  html = []
  if block_given?
    html = collection.map {|choice| capture(choice, &block)}
  else
    collection.each do |choice|
      html.push(
        check_box_tag(
          "#{filter.field_name}[#{choice.value}]", choice.value, choice.checked?, choice.options.reverse_merge(name: "#{filter.field_name}[]")
        ),
        label_tag("#{filter.field_name}[#{choice.value}]", choice.name),
        '<br/>'
      )
    end
  end

  html.join("\n").html_safe
end

#tableficate_data_tag(row, column) ⇒ Object



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

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

#tableficate_filter_tag(filter) ⇒ Object



21
22
23
# File 'lib/tableficate/helper.rb', line 21

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

#tableficate_header_tag(column) ⇒ Object



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

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

#tableficate_label_tag(filter) ⇒ Object



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

def tableficate_label_tag(filter)
  label_tag(filter.field_name, filter.label, filter.options[:label_options])
end

#tableficate_radio_tags(filter, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tableficate/helper.rb', line 58

def tableficate_radio_tags(filter, &block)
  field_value = filter.field_value(params[filter.table.as])

  collection = Tableficate::Filter::Collection.new(filter.options[:collection], selected: filter.field_value(params[filter.table.as]))

  html = []
  if block_given?
    html = collection.map {|choice| capture(choice, &block)}
  else
    collection.each do |choice|
      html.push(
        radio_button_tag(filter.field_name, choice.value, choice.checked?, choice.options),
        label_tag("#{filter.field_name}[#{choice.value}]", choice.name),
        '<br/>'
      )
    end
  end

  html.join("\n").html_safe
end

#tableficate_row_tag(row, columns) ⇒ Object



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

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

#tableficate_select_tag(filter) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tableficate/helper.rb', line 33

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

  if field_value.present? and filter.options[:collection].is_a?(String)
    Array.wrap(field_value).each do |fv|
      if filter.options[:collection].match(/<option[^>]*value\s*=/)
        filter.options[:collection].gsub!(/(<option[^>]*value\s*=\s*['"]?#{fv}[^>]*)/, '\1 selected="selected"')
      else
        filter.options[:collection].gsub!(/>#{fv}</, " selected=\"selected\">#{fv}<")
      end
    end
  elsif not filter.options[:collection].is_a?(String)
    filter.options[:collection] = Tableficate::Filter::Collection.new(filter.options[:collection], selected: field_value).map {|choice|
      html_attributes = choice.options.length > 0 ? ' ' + choice.options.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

  filter.options[:collection] = filter.options[:collection].html_safe

  select_tag(filter.field_name, filter.options.delete(:collection), filter.options)
end

#tableficate_text_field_tag(filter) ⇒ Object



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

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