Module: MultipleTableInheritance::Child::Base::ClassMethods

Defined in:
lib/multiple_table_inheritance/child/base.rb

Instance Method Summary collapse

Instance Method Details

#inherits_from(association_name, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/multiple_table_inheritance/child/base.rb', line 14

def inherits_from(association_name, options={})
  # Standardize options, and remove those that should not affect the belongs_to relationship
  options = Base::default_options.merge(options.to_options)
  inherit_methods = options.delete(:methods)
  
  @inherited_attribute_methods_mutex = Mutex.new
  
  extend AttributeMethods, FinderMethods, SharedMethods
  include InstanceMethods, SharedMethods
  include DelegateMethods if inherit_methods
  
  # Set association references.
  self.parent_association_name = association_name.to_sym
  self.primary_key = "#{parent_association_name}_id"
  
  # Ensure parent association is always returned.
  define_method("#{parent_association_name}_with_autobuild") do
    send("#{parent_association_name}_without_autobuild") || send("build_#{parent_association_name}")
  end
  
  # Bind relationship, handle validation, and save properly.
  belongs_to parent_association_name, options
  alias_method_chain parent_association_name, :autobuild
  before_validation :set_association_subtype
  validate :parent_association_must_be_valid
  before_save :parent_association_must_be_saved
  
  # denote that the association methods have not been built
  @loaded = false
end

#parent_association_classObject



45
46
47
48
49
50
# File 'lib/multiple_table_inheritance/child/base.rb', line 45

def parent_association_class
  @parent_association_class ||= begin
    reflection = create_reflection(:belongs_to, parent_association_name, {}, self)
    reflection.klass
  end
end