Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

table_for_collection

Package Overview
Dependencies
Maintainers
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

table_for_collection

  • 1.0.7
  • Rubygems
  • Socket score

Version published
Maintainers
3
Created
Source

{}[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:

NameEmailAddress
John Smithjohn.smith@example.com100 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:

...
NameEmailHomeCreated 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

Package last updated on 30 Nov 2013

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc