A very simple helper for generating data tables.
Add this line to your application's Gemfile:
gem 'greedo'
And then execute:
$ bundle
Or install it yourself as:
$ gem install greedo
After installing greedo, you can use the helper like this:
%h1 My first table
= greedo(User.registered, per_page: 10) do |g|
= g.column :name
= g.column "Actions" do |user|
= link_to "Edit", edit_user_path(user)
This will create a data table with two columns, one labelled "Name" and the other "Actions". It will show 10 users from the given scope (which should either be an ActiveRecord::Relation or an Array). Pagination will be added if necessary.
%h1 My first table
= greedo(User.registered, per_page: 10) do |g|
= g.custom_empty_message "There are no users in the database."
= g.column :name
You can wrap records in a class instance:
%h1 Table with class presenters
= greedo(User.registered) do |g|
= g.presenter UserPresenter
= g.column :manager_name
or the same with a block:
%h1 Table with class presenters
= greedo(User.registered) do |g|
- g.presenter do |record|
UserPresenter.new(record)
= g.column :manager_name
This is a very simple helper for now - there's no sorting, or even any way to easily customize the generated HTML. This will change in time, but for now I'm open-sourcing this mainly to share this useful bit of code between projects.
Here's a couple of things I'm planning to add to this gem:
- sorting by clicking on a column name
- a generator to install the templates used by greedo in your project for ease of customization
- making the paginator library swappable (greedo uses will_paginate now).
- Fork it ( https://github.com/[my-github-username]/greedo/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request