Module: SimpleTableFor

Defined in:
lib/simple_table_for.rb,
lib/simple_table_for/version.rb

Constant Summary collapse

VERSION =
'0.0.1'

Instance Method Summary collapse

Instance Method Details

#field(content, options = {}) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/simple_table_for.rb', line 2

def field(content, options = {})
  <<-TD.html_safe
    <td id='#{options[:id]}' class='#{options[:class]}'>
      #{content}
    </td>
  TD
end

#table_for(collection, heads, options = {}) ⇒ Object

Usage:

<%= table_for @posts, %w[Title Text Date Comments\ count -] do |post| %>
  <%= field post.title %>
  <%= field post.text %>
  <%= field post.date %>
  <%= field post.comments.count %>
  <%= field link_to('View', post) %>
<% end %>


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/simple_table_for.rb', line 18

def table_for(collection, heads, options = {})
  heads = heads.map{|h| "<th>#{h}</th>".html_safe }.join('')

  rows = collection.map do |obj|
    "<tr>#{capture{ yield obj }}</tr>".html_safe
  end.join('')

  <<-TABLE.html_safe
    <table id='#{options[:id]}' class='#{options[:class]}'>
      <thead>
        <tr>
          #{heads}
        </tr>
      </thead>

      <tbody>
        #{rows}
      </tbody>
    </table>
  TABLE
end