
Security News
New Website “Is It Really FOSS?” Tracks Transparency in Open Source Distribution Models
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
table_for_collection
Advanced tools
{}[http://badge.fury.io/rb/table_for_collection]
{
}[http://travis-ci.org/lunich/table_for]
{
}[https://codeclimate.com/github/lunich/table_for]
== Description
table_for_collection is a simple gem used to render tables based on the given collection.
=== Install
Just add to your Gemfile
gem 'table_for_collection'
=== Simplest examle
<%= table_for @users do -%> <% columns :name, :email, :address %> <% end %>
In this case you just put fields list to the :columns method. This ruby code will produce following HTML:
Name | Address | |
---|---|---|
John Smith | john.smith@example.com | 100 Spear St., NY, USA |
=== Simple examle
<%= table_for @users, :html => { :class => "simple-table", :id => "users", :width => "100%", :tr => { :class => lambda { |user| "simple-row #{user.role}" }, :id => lambda { |user| "user-row-#{user.id}" } } } do -%> <% column :name, :html => { :th => { :width => "25%" }, :td => { :class => "user-name" }} %> <% column :email, :width => "20%" %> <% column :address, :title => "Home" %> <% column :created_at, :title_callback => lambda { |title| link_to(title, users_path(params.merge(:order => "desc"))) } %> <% end %>
You can put :html hash to the options list (inside the :column method too). In this case table will be created with given html attributes. To assign :tr options use :tr key under :html - :class value will be added to the classes list, :id can be instance of the Proc class, in this it will be called for each collection element. For column width you can use shortcut :width, remember that :width shortcut has higher priority. Also :title value in the :column method will be used as column's title. You can pass Proc object to the :title_callback param. In this case given proc will be called with title value. It will produce HTML similar to:
...Name | Home | Created At | |
---|---|---|---|
... | ... | ... | ... |
=== More complex example
<%= table_for @users do -%> <% column :login, :title => "User name" %> <% column :email do |email| %> <% mail_to email %> <% end %> <% column :title => "Full name" do |user| %> <% [user.first_name, user.last_name].join(" ") %> <% end %> <% column :company %> <% column :title => "Actions" do |user| %> <% [link_to("Show", user), link_to("Delete", user, :method => :delete)].join(" | ") %> <% end %> <% column :role, :default => "guest" %> <% column :created_at, :time_format => "%Y-%m-%d" %> <% end %>
=== Example with colorized rows
<%= table_for @users, :html => { :tr => { :class => "row" } }, :stripes => %w{even odd} do %> <% column :name %> <% column :email %> <% column :address %> <% end %>
Also class given by :stripe option will be merged with options[:html][:tr] so this code will produce following HTML:
... ... ... ...== Relations
If you need a column for relation or apply method for simple column you can use :attr option:
<%= table_for @users do -%> <% column :login %> <% column :company, :attr => :name %> <% column :name, :attr => :underscore %> <% end %>
== I18n
When column name is set, but :title is not, helper tries to find translation in standard mechanism of the ActiveModel. For more information visit: http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models
== Known issues
Unfortunately it works incorrect if there is no "-" before %> in this code:
<%= table_for @users do -%>
We hope it will be fixed soon.
Also we need to check if all our samples are correct.
FAQs
Unknown package
We found that table_for_collection demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.