Module: HtmlTables::DataTableHelper

Defined in:
lib/html_tables/data_table_helper.rb

Instance Method Summary collapse

Instance Method Details

#data_table_for(collection, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/html_tables/data_table_helper.rb', line 5

def data_table_for(collection, options = {})
  t = DataTable.new(self, collection, options)
  if block_given?
    yield t.object_to_yield
  else
    t.auto_generate_columns!
  end

  cls = %w(table table-striped table-bordered)
  cls << 'table-condensed' if options[:condensed]
  cls << options[:class] if options[:class]
  table_html_options = { class: cls }
  table_html_options.merge!(options[:html]) { |_, v1, v2| [v1, v2].flatten } if options[:html]
  (:table, table_html_options) do
    (:caption, options[:caption] || controller_name, class: ('hidden' unless options[:caption])) +
    (:colgroup) do
      b = ''.html_safe
      t.columns.each do |_, opts|
        col_opts = { }
        col_opts[:style] = "width: #{opts[:width]}" if opts[:width]
        b << (:col, col_opts) { }
      end
      b
    end +
    (:thead) do
      (:tr) do
        t.columns.map do |name, opts|
          header_opts = {}
          header_opts[:class] = 'check' if opts[:checkbox]
          header_opts[:header_title] = opts[:header_title] if opts[:header_title]
          (:th, t.header_for(name), header_opts)
        end.join.html_safe
      end
    end +
    (:tbody) do
      b = ''.html_safe
      rows = if options[:group]
        collection.group_by {|i| options[:group][:proc].call(i) }.each do |g, l|
          b << (:tr, class: 'group') do
            (:th, capture(g, &options[:group][:block]), colspan: t.columns.size)
          end
          render_data_rows(b, l, t)
        end
        collection
      else
        render_data_rows(b, collection, t)
      end
      if rows.size == 0 then
        b << (:tr, class: 'nodata') do
          (:td, colspan: t.columns.size) { t.nodata_message }
        end unless t.nodata_message.nil?
      end
      b
    end
  end
end

#extract_check_box_tag_options(opts) ⇒ Object



121
122
123
124
# File 'lib/html_tables/data_table_helper.rb', line 121

def extract_check_box_tag_options(opts)
  valid_options = [:disabled]
  opts.select { |k, _| valid_options.include?(k) }
end

#render_data_rows(b, collection, t) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/html_tables/data_table_helper.rb', line 62

def render_data_rows(b, collection, t)
  collection.each do |item|
    b << (:tr, t.row_options_for(item)) do
      t.columns.map do |name, opts|
        render_td(item, name, opts)
      end.join.html_safe
    end
  end
end

#render_td(item, column, opts) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/html_tables/data_table_helper.rb', line 72

def render_td(item, column, opts)
  td_options = {}

  if opts[:align] == :center
    td_options[:class] = 'c'
  end

  if opts[:title]
    td_options[:title] = if opts[:title].respond_to?(:call)
      opts[:title].call(item)
    else
      opts[:title].to_s
    end
  end

  v = if opts[:checkbox]
    checked = opts[:checked]
    checked = opts[:block].call(item) if opts[:block]
    check_box_tag "#{column}[]", item.public_send(opts.fetch(:value_method, :id)), checked, extract_check_box_tag_options(opts)
  elsif opts[:radio]
    radio_button_tag "#{column}[]", item.public_send(opts.fetch(:value_method, :id))
  elsif opts[:block]
    capture(item, &opts[:block])
  else
    tmp = item.public_send(column)
    tmp = item.public_send("#{column}_text") rescue tmp if tmp.is_a?(Symbol)
    tmp
  end

  if ::Rails.env.development? && v.is_a?(ActiveRecord::Base)
    btn = (:div, class: 'entity-shortcut') do
      link_to(url_for(v), class: 'btn btn-small') do
        (:i, nil, class: 'icon-share')
      end
    end rescue nil
  end

  v = if v.is_a?(Enumerable)
    v.inject(''.html_safe) { |b, i| b << ', ' unless b.blank?; b << i.format_for_output }
  else
    v.format_for_output
  end
  v = v.to_s unless v.nil? || v.is_a?(String)

  v = ''.html_safe << btn << v if btn

  (:td, v, td_options)
end