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
44
45
46
47
48
# 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)
  
  extend 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
  
  # Allow getting and setting of parent attributes and relationships.
  inherited_columns_and_associations.each do |name|
    delegate name, "#{name}=", :to => parent_association_name
  end
  
  # Ensure parent's accessible attributes are accessible in child.
  parent_association_class.accessible_attributes.each do |attr|
    attr_accessible attr.to_sym
  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
end

#parent_association_classObject



50
51
52
53
# File 'lib/multiple_table_inheritance/child/base.rb', line 50

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