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.
Advertisement
