Module: PolymorphicModel::Initializer

Included in:
ActiveRecord::Base
Defined in:
lib/polymorphic_model.rb

Instance Method Summary collapse

Instance Method Details

#polymorphic_model(options = {}) ⇒ Object

Initializes model to be a polymorphic one Example usage:

polymorphic_model :with_type_column => :page_type


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/polymorphic_model.rb', line 6

def polymorphic_model(options = {})
  if options[:with_type_column]
    @_polymorphic_column = options[:with_type_column].to_sym
    @_polymorphic_column.freeze
    @_model_types = []
    extend PolymorphicModel::ClassMethods
    include PolymorphicModel::InstanceMethods
    self.instance_eval do
      define_method :"#{@_polymorphic_column}=" do |value|
        super(value.to_s)
      end
    end
  end
end