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. Dynamic attributes can be defined by simply setting an attribute or by passing them on create or update.
MIT License
= 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. 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"} |
+-------+-------------+------------------------------------------------------------------------+
== Requirements
== Installation
== Usage
To add dynamic_attributes to an AR model, take the following steps:
Create a migration to add a column to serialize the dynamic attributes to:
class AddDynamicAttributesToDynamicModels < ActiveRecord::Migration def self.up add_column :dynamic_models, :dynamic_attributes, :text end
def self.down
remove_column :dynamic_models, :dynamic_attributes
end
end
Add dynamic_attributes to your AR model:
class DynamicModel < ActiveRecord::Base has_dynamic_attributes end
Now you can add dynamic attributes in several ways. Examples:
Note that a dynamic attribute should be prefixed (by default with 'field_'), see the Options section for more info.
== Options
The has_dynamic_attribute call takes three different options:
By default, the has_dynamic_attributes call without options equals to calling:
has_dynamic_attributes :dynamic_attribute_field => :dynamic_attributes, :dynamic_attribute_prefix => 'field_', :destroy_dynamic_attribute_for_nil => false
Take a look at the code Rdoc for more information!
== Validations
The validations provided by AR can be used for dynamic attributes as if it is a normal attribute.
== Contributors
== Note on Patches/Pull Requests
== Copyright
Copyright (c) 2010 Reinier de Lange. See LICENSE for details.