Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Installation

ramontayag edited this page · 2 revisions

Create a migration:

class CreateDynamicAttributes < ActiveRecord::Migration
  def self.up
    create_table(:dynamic_attributes) do |t|
      t.string :name, :null => false
      t.string :attr_key, :null => false
      t.string :object_type, :null => false
      t.string :attributable_type, :null => false
      t.integer :attributable_id, :null => false
      %w(integer string boolean text float).each do |type|
        t.send(type, "#{type}_value".to_sym)
      end
    end

    add_index "dynamic_attributes", ["attributable_id"], :name => "index_dynamic_attributes_on_attributable_id"
    add_index "dynamic_attributes", ["attributable_type"], :name => "index_dynamic_attributes_on_attributable_type"
    add_index "dynamic_attributes", ["attr_key"], :name => "index_dynamic_attributes_on_attr_key"

    create_table(:dynamic_attribute_definitions) do |t|
      t.text :definition
      t.string :attribute_defineable_type, :null => false
      t.integer :attribute_defineable_id, :null => false
    end

    add_index "dynamic_attribute_definitions", ["attribute_defineable_id"], :name => "index_dynamic_attribute_definitions_on_attribute_defineable_id"
    add_index "dynamic_attribute_definitions", ["attribute_defineable_type"], :name => "index_dynamic_attribute_definitions_on_attribute_defineable_type"
  end

  def self.down
    remove_index "dynamic_attribute_definitions", :name => "index_dynamic_attribute_definitions_on_attribute_defineable_id"
    remove_index "dynamic_attribute_definitions", :name => "index_dynamic_attribute_definitions_on_attribute_defineable_type"

    drop_table(:dynamic_attribute_definitions)

    remove_index "dynamic_attributes", :name => "index_dynamic_attributes_on_attr_key"
    remove_index "dynamic_attributes", :name => "index_dynamic_attributes_on_attributable_type"
    remove_index "dynamic_attributes", :name => "index_dynamic_attributes_on_attributable_id"

    drop_table(:dynamic_attributes)
  end
end
  • attr_key the name of the dynamic attribute
  • object_type what type of value is saved. You have the following choices:
    • “string”
    • “integer”
    • “boolean”
    • “float”
    • “text”
  • attributable_id and attributable_type are for the polymorphic association
  • integer_value, string_value, boolean_value, text_value, float_value are used to store the data. See object_type.

Add this to your Gemfile:

gem 'datts_right'

Add this to the model that you want to have dynamic attributes:

has_dynamic_attributes or has_datts

Something went wrong with that request. Please try again.