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
# 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
  include InstanceMethods
  include DelegateMethods if inherit_methods
  
  self.parent_association_name = association_name.to_sym
  self.primary_key = "#{parent_association_name}_id"
  
  define_parent_association_builder
  
  # Bind relationship, handle validation, and save properly.
  belongs_to parent_association_name, options
  alias_method_chain parent_association_name, :autobuild
  validate :parent_association_must_be_valid
  before_save :parent_association_must_be_saved
end

#parent_association_classObject



37
38
39
40
41
42
# File 'lib/multiple_table_inheritance/child/base.rb', line 37

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