Module: PolymorphicAsTable::ClassMethods

Defined in:
lib/polymorphic_as_table.rb

Instance Method Summary collapse

Instance Method Details

#has_polymorphic_as_tableObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/polymorphic_as_table.rb', line 10

def has_polymorphic_as_table
  ActiveRecord::Associations::AssociationProxy.send(
    :include, PolymorphicAsTable::AssociationProxy)
  ActiveRecord::Associations::HasOneAssociation.send(
    :include, PolymorphicAsTable::HasOneAssociation)
  ActiveRecord::Associations::HasManyAssociation.send(
    :include, PolymorphicAsTable::HasManyAssociation)
  ActiveRecord::Associations::ThroughAssociationScope.send(
    :include, PolymorphicAsTable::ThroughAssociationScope)
  ActiveRecord::Reflection::ThroughReflection.send(
    :include, PolymorphicAsTable::ThroughReflection)

  class << self
    def sti_name
      table_name
    end
  end
end

#is_polymorphic_as_tableObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/polymorphic_as_table.rb', line 29

def is_polymorphic_as_table
  ActiveRecord::Associations::AssociationProxy.send(
    :include, PolymorphicAsTable::AssociationProxy)
  ActiveRecord::Associations::BelongsToPolymorphicAssociation.send(
    :include, PolymorphicAsTable::BelongsToAssociation)

  class << self
    def define_attribute_methods
      if super && @@association
        define_method("#{@@association}_type=") do |value|
          debugger
          write_attribute("#{@@association}_type", value.tableize)
        end

        define_method("#{@@association}_type") do
          read_attribute("#{@@association}_type").classify
        end

        define_method("#{@@association}=") do |association|
          write_attribute("#{@@association}", association)
          write_attribute("#{@@association}_type", association.class.to_s.tableize)
        end
      end
    end

    def belongs_to(association_id, options={})
      @@association = association_id
      super
    end
  end
end