Module: HtmlTables::DataTableHelper
- Defined in:
- lib/html_tables/data_table_helper.rb
Instance Method Summary collapse
- #data_table_for(collection, options = {}) ⇒ Object
- #extract_check_box_tag_options(opts) ⇒ Object
- #render_data_rows(b, collection, t) ⇒ Object
- #render_td(item, column, opts) ⇒ Object
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, = {}) t = DataTable.new(self, collection, ) 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 [:condensed] cls << [:class] if [:class] = { class: cls } .merge!([:html]) { |_, v1, v2| [v1, v2].flatten } if [:html] content_tag(:table, ) do content_tag(:caption, [:caption] || controller_name, class: ('hidden' unless [:caption])) + content_tag(:colgroup) do b = ''.html_safe t.columns.each do |_, opts| col_opts = { } col_opts[:style] = "width: #{opts[:width]}" if opts[:width] b << content_tag(:col, col_opts) { } end b end + content_tag(:thead) do content_tag(: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] content_tag(:th, t.header_for(name), header_opts) end.join.html_safe end end + content_tag(:tbody) do b = ''.html_safe rows = if [:group] collection.group_by {|i| [:group][:proc].call(i) }.each do |g, l| b << content_tag(:tr, class: 'group') do content_tag(:th, capture(g, &[: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 << content_tag(:tr, class: 'nodata') do content_tag(:td, colspan: t.columns.size) { t. } end unless t..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 (opts) = [:disabled] opts.select { |k, _| .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 << content_tag(:tr, t.(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) = {} if opts[:align] == :center [:class] = 'c' end if opts[:title] [: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, (opts) elsif opts[:radio] "#{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 = content_tag(:div, class: 'entity-shortcut') do link_to(url_for(v), class: 'btn btn-small') do content_tag(: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 content_tag(:td, v, ) end |