Gem: dynamic_attributes

Link to source code: http://github.com/moiristo/dynamic_attributes

dynamic_attributes is a gem that lets you dynamically specify attributes on ActiveRecord models, which will be serialized and deserialized to a given text column. Think of it as having some document-database functionality in Rails while just using a relational database.

Example:

    >> dm = DynamicModel.new(:field_test => 'I am a dynamic attribute')
      +-------+-------------+--------------------------------------------+
      | title | description | dynamic_attributes                         |
      +-------+-------------+--------------------------------------------+
      |       |             | {"field_test"=>"I am a dynamic_attribute"} |
      +-------+-------------+--------------------------------------------+
    >> dm.field_test
      => "I am a dynamic_attribute"
    >> dm.field_test2
      NoMethodError: undefined method `field_test2'
    >> dm.field_test2 = 'I am too!'
      => 'I am too!'
    >> dm.field_test2
      => 'I am too!'
    >> dm.save
      +-------+-------------+------------------------------------------------------------------------+
      | title | description | dynamic_attributes                                                     |
      +-------+-------------+------------------------------------------------------------------------+
      |       |             | {"field_test2"=>"I am too!", "field_test"=>"I am a dynamic_attribute"} |
      +-------+-------------+------------------------------------------------------------------------+

I used this gem in our CMS to add non-standard attributes to upcoming events. Events usually have a start time, location and description, but some events needed some more attributes, like the name of the presenter or a case number.

Advertisements

~ by moiristo on September 30, 2010.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: