ModelTableizer
Given a collection of objects where within the object instance variables are accessable, as method calls, produce an html table layout for the collection. Since this generates raw html you can use in any ruby web application server(Sinatra, Rails, Camping, etc...)
Example:
users = [
User.new(:name => 'Some Name 1', :email => 'some email 1',
:created_at => Time.now, :beta_request => '/shome_url_1'),
User.new(:name => 'Some Name 2', :email => 'some email 2',
:created_at => Time.now, :beta_request => ''),
User.new(:name => 'Some Name 3', :email => 'some email 3',
:created_at => Time.now, :beta_request => '/shome_url_3')
]
table users, :id => 'list_of_users' do column :name, :id => 'blah_id' column :email column :created_at column "CRM Status", :id => 'from_crm' do "#{user.beta_request}" if user.beta_request end end
Results in:
Name | Created At | Crm Status | |
---|---|---|---|
Some Name 1 | some email 1 | 2012-04-06 15:37:49 -0700 | /shome_url_1 |
Some Name 2 | some email 2 | 2012-04-06 15:37:49 -0700 | |
Some Name 3 | some email 3 | 2012-04-06 15:37:49 -0700 | /shome_url_3 |
Installation
Add this line to your application's Gemfile:
gem 'model_tableizer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install model_tableizer
Usage
include ModelTableizer
If you are going to be using this with Rails, in your application controller add the above include line. Then in a controller method make a call such as:
@user_table = table users, :id => 'list_of_users' do
column :name, :id => 'blah_id'
column :email
column :created_at
column "CRM Status", :id => 'from_crm' do
"<a href='#{user.beta_request}'>#{user.beta_request}</a>" if user.beta_request
end
end
Then in your view you can access
<% raw(@user_table) %>
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request