Sha256: 04a84f93ff53eaa55f822ae7986966cffdc49a4f31ab517d4627c7d250e00258

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

module SimpleTableFor
  module Helpers
    # Generates a table field.
    # See table_for for details.
    def field(content, options = {})
      content_tag :td, content, options
    end

    # Generates a table.
    # Usage:
    #   <%= table_for @users, [:name, :email, 'Registration Date', 'Comments count', '-'], model: User, 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 %>
    def table_for(collection, headers, options = {})
      options = Defaults.get.merge options

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_table_for-0.3.1 lib/simple_table_for/helpers.rb
simple_table_for-0.3.0 lib/simple_table_for/helpers.rb