define_columns
Define Columns allows you to manipulate the content columns of your ActiveRecord model.
Features
-
Add virtual attributes as columns
-
Hiding existing content columns
-
Changes the output of the column
Benefits
-
Allows you to place your logic in the model instead of the view.
-
Does not assume your table markup. You have full flexibility to change things
-
No need to hand write the table columns for virtual attributes
Installation
$ gem install define_columns
Usage
# Person has content_columns [ "name", "address", "gender", "hobbies" ]
# Note that the names must be an exact match
class Person < ActiveRecord::Base
define_columns do |columns|
columns.hide %w(name) # takes in an array of column names
columns.add %w(favourite_color) # can add virtual attributes as columns
columns.show(:gender) do |gender| # returns "-Male-" for column.show(person)
"-#{gender}-"
end
end
def favourite_color
red
end
end
# Person.table_columns returns [ "address", "gender", "hobbies", "favourite_color" ]
# In your view(in haml)
%table
%thead
%tr
- Person.table_columns.each do |column|
%th= column.header
- @persons.each do |person|
%tbody
- Person.table_columns.each do |column|
%td= column.show(person)
To Note
The show block takes in an argument and a block.
columns.show(:gender) do |gender|
"-#{gender}-"
end
The argument :gender must match the column name. The block parameter ‘gender’ is equivalent to the result of @person.gender.
Not having a show block renders @person.gender
Copyright
Copyright © 2010 Wong Liang Zan. See LICENSE for details.