Hi!
I’m thinking of implementing datagrid like functionality for showing
data (models) in grids so that I don’t have to re-invent the
view/controller for each table I need. There are number of parameters
that need to be customizable: column titles, row style depending on the
data (i.e. showing all rows with balance<0 in red), is column sortable,
sort-algorithm etc.
To me, putting this parametrization in model like i.e. scaffold
extension does (fields like @scaffold_fields, @scaffold_*) isn’t the
right choice, as these parameters have more to do with “visual model”
than with data model, so I was thinking of creating app/decorators/ and
putting these visual models into separate classes (decorators) there
i.e.
class OrdersDecorator < ActiveRecord::Decorator
The base class (AR::Decorator), each decorator would becom instance
variable @model, which could be used to refer to original model, and
this could happen automagically depending on the class name. There would
be some default functions, like field_title that would return humanized
column descriptions (i.e. order_status -> Order status)
This is how one such decorator could look like
class OrdersDecorator < ActiveRecord::Decorator
@datagrid_fields=%w(id, name, country)
def datagrid_field_title_id
“Order ID”
end
def datagrid_row_style(m)
‘big-order’ if m.total>100
end
def sort_country(a,b,direction)
return a.continent<b.continet #sort countries by continent, not by
name
end
end
What do you think?