Sha256: 34672e3138e9507c6f11f99223a6e456df81c93a75b98ffb18d896f128d5a01c

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 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', '-'], 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, collection.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

    alias simple_table_for table_for
    alias column field
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_table_for-0.4.0 lib/simple_table_for/helpers.rb