Ruby on Rails Data Grid Control
As far as I can tell, Ruby on Rails doesn’t have a built in control for displaying tabular data in a grid. Not a rich one anyway, with paging/sorting/filtering capability built-in. I plan on attempting to create one. I’m actually almost there. I’m doing this instead of getting my actual projects done. 🙂
All parameters should have defaults to do the standard thing, with ability to further customize if so desired. Convention over configuration and all that. It should be able to do sorting/paging on columns from associated tables as well. Here’s the proposed syntax for how this might be called in a controller:
a simple example
class AgreementsController < ApplicationController
def list_simple
grid_view(Agreement,
:columns => ['title', 'active', 'amount', 'frequency']
)
end
end
a more complex example:
class AgreementsController < ApplicationController
def list_complex
grid_view(
Agreement,
:columns => [
"Status",
"Company Name" => {
:dbcolumn => "company_name",
:filter => true
},
"Category" => {
:dbcolumn => "agreement.category_id",
:filter => true,
:filter_type => :select,
:filter_options => Category.find_all.collect {|c| [ c.name, c.id ] }
},
"Active" => {
:dbcolumn => "agreement.active",
:filter => true,
:filter_type => :select,
:filter_options => [["Yes", 1], ["No", 0]]
}
],
:joins => ["LEFT OUTER JOIN categories ON agreements.category_id = categories.id",
"LEFT OUTER JOIN companies ON agreements.company_id = company.id"]
:alias_columns => [["companies.name", "company_name"],["categories.name", "category_name"]],
:order_by => "category_name, products.name",
:per_page => 25
)
end
end
The grid_view method will be at the application level, so you can call it from any controller. The presentation (view) will live in one /shared/_grid_view.rhtml
partial file and be 100% customizable. And because of that, it doesn’t even have to be a table or look like a grid! The MVC pattern of rails allows this, and would make this far superior and easier to customize than say – the ASP.NET data grid control, in my opinion. Here’s one possible way such a beast might look like: