Module: SimpleTableFor::Helpers

Defined in:
lib/simple_table_for/helpers.rb

Instance Method Summary collapse

Instance Method Details

#field(content, options = {}) ⇒ Object Also known as: column

Generates a table field. See table_for for details.



5
6
7
# File 'lib/simple_table_for/helpers.rb', line 5

def field(content, options = {})
   :td, content, options
end

#table_for(collection, headers, options = {}) ⇒ Object Also known as: simple_table_for

Generates a table. Usage:

<%= table_for @users, [:name, :email, 'Registration Date', 'Comments count', '-'], id: 'table-id', class: 'table-class' do |user| %>
  <%= field user.name %>
  <%= field user.email %>
  <%= field user.created_at %>
  <%= field user.comments.count %>
  <%= field link_to('View', user) %>
<% end %>


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
# File 'lib/simple_table_for/helpers.rb', line 18

def table_for(collection, headers, options = {})
  options = Defaults.get.merge options

   :table, options do
    concat ( :thead do
       :tr do
        headers.map do |header|
          case header
          when String
            concat( :th, header)
          when Symbol
            concat( :th, collection.model.human_attribute_name(header))
          end
        end
      end
    end)

    concat ( :tbody do
      collection.map do |obj|
        concat ( :tr do
          capture{ yield obj }
        end)
      end
    end)
  end
end