DeclarativeGrid
DeclarativeGrid is a simple, flexible and extensible datagrid/table generator.
- Configurable: Described in DSL
- Extensible Renderers: You can build a custom renderer to generate XLSX, json, and so on
- Select and sort columns: You can assign array of column names to renderer
- Any Enumerable or Enumerator can be source data
- It is independent from ActiveRecord and other ORMs
Deprecation
In Version 0.1.0, block-style instantization is formally deprecated, it will be removed on the next release (0.2.0)
grid = MemberGrid.new { records }
Please pass records as an argument instead
grid = MemberGrid.new(records)
Example
in models/member_grid.rb
class MemberGrid < DeclarativeGrid::Base
# Columns
#
column(:name)
column(:birthday) {|r| (r.birthdate) }
column(:gender, header: 'Sex')
# Renderers
#
# with builtin render class: DeclarativeGrid::Renderers::Csv
renderer :csv
#
# with builtin render class: DeclarativeGrid::Renderers::Html
renderer :html
#
# with selected columns
renderer :csv_selected, class: :csv, columns: [:gender, :name]
#
# with custom renderer class
renderer :xls, class: XlsRenderer
#
end
in member.rb
class Member < Struct.new(:name, :gender, :birthdate)
end
example code:
members = [
Member.new('Adam Jensen', 'Male', Date.new(1993,3,9)),
Member.new('Altaïr Ibn-La\'Ahad', 'Male', Date.new(1165,1,11)),
Member.new('Ezio Auditore da Firenze', 'Male', Date.new(1459,6,24)),
Member.new('Desmond Miles', 'Male', Date.new(1987,3,13))
]
@grid = MemberGrid.new(members)
puts "== sample 1 =="
puts @grid.renderer(:csv).to_string
puts "== sample 2 =="
puts @grid.renderer(:csv_selected).to_string
puts "== sample 3 =="
io = StringIO.new
@grid.renderer(:csv_selected).perform(io)
puts io.string
result:
== sample 1 ==
Name,Birthday,Sex
Adam Jensen,1993-03-09,Male
Altaïr Ibn-La'Ahad,1165-01-11,Male
Ezio Auditore da Firenze,1459-06-24,Male
Desmond Miles,1987-03-13,Male
== sample 2 ==
Sex,Name
Male,Adam Jensen
Male,Altaïr Ibn-La'Ahad
Male,Ezio Auditore da Firenze
Male,Desmond Miles
== sample 3 ==
Sex,Name
Male,Adam Jensen
Male,Altaïr Ibn-La'Ahad
Male,Ezio Auditore da Firenze
Male,Desmond Miles
Installation
Add this line to your application's Gemfile:
gem 'declarative_grid'
And then execute:
$ bundle
Or install it yourself as:
$ gem install declarative_grid
Usage
If you want to work with ActiveRecord, like this:
grid = MemberGrid.new(Enumerator.new(Member.where(is_good: true), :find_each))
If you want shorter code:
grid = MemberGrid.new Member
or
grid = MemberGrid.new Member.where(is_good: true)
The library will automatically wrap the records with Enumerator, preventing from massive flooding.
Note that ActiveRecord#all (prior to 4.0) returns an Array instead of ActiveRecord::Relation. Please avoid using it.
How to Test
$ rake spec
Contributing
- Fork it
- 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 new Pull Request