Forum: Ruby on Rails decorators for models?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Esad H. (Guest)
on 2006-03-07 03:09

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

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"

  def datagrid_row_style(m)
    'big-order' if>100

  def sort_country(a,b,direction)
   return a.continent<b.continet #sort countries by continent, not by

What do you think?
Douglas T. (Guest)
on 2006-03-07 08:19
(Received via mailing list)
instead of using a decorator, why don't you implement it as a module and
then mix it into ActiveRecord::Base? That way, any existing model
you have will obtain the datagrid functionality.
This topic is locked and can not be replied to.