Module: ActiveRecord::Inheritance::ClassMethods

Defined in:
activerecord/lib/active_record/inheritance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#abstract_classObject

Set this to true if this is an abstract class (see abstract_class?).


47
48
49
# File 'activerecord/lib/active_record/inheritance.rb', line 47

def abstract_class
  @abstract_class
end

Instance Method Details

#abstract_class?Boolean

Returns whether this class is an abstract class or not.


50
51
52
# File 'activerecord/lib/active_record/inheritance.rb', line 50

def abstract_class?
  defined?(@abstract_class) && @abstract_class == true
end

#base_classObject

Returns the base AR subclass that this class descends from. If A extends AR::Base, A.base_class will return A. If B descends from A through some arbitrarily deep hierarchy, B.base_class will return A.

If B < A and C < B and if A is an abstract_class then both B.base_class and C.base_class would return B as the answer since A is an abstract_class.


42
43
44
# File 'activerecord/lib/active_record/inheritance.rb', line 42

def base_class
  class_of_active_record_descendant(self)
end

#descends_from_active_record?Boolean

True if this isn't a concrete subclass needing a STI type condition.


15
16
17
18
19
20
21
# File 'activerecord/lib/active_record/inheritance.rb', line 15

def descends_from_active_record?
  if superclass.abstract_class?
    superclass.descends_from_active_record?
  else
    superclass == Base || !columns_hash.include?(inheritance_column)
  end
end

#finder_needs_type_condition?Boolean

:nodoc:


23
24
25
26
# File 'activerecord/lib/active_record/inheritance.rb', line 23

def finder_needs_type_condition? #:nodoc:
  # This is like this because benchmarking justifies the strange :false stuff
  :true == (@finder_needs_type_condition ||= descends_from_active_record? ? :false : :true)
end

#instantiate(record) ⇒ Object

Finder methods must instantiate through this method to work with the single-table inheritance model that makes it possible to create objects of different types from the same table.


61
62
63
64
65
66
67
68
69
70
71
72
# File 'activerecord/lib/active_record/inheritance.rb', line 61

def instantiate(record)
  sti_class = find_sti_class(record[inheritance_column])
  record_id = sti_class.primary_key && record[sti_class.primary_key]

  if ActiveRecord::IdentityMap.enabled? && record_id
    instance = use_identity_map(sti_class, record_id, record)
  else
    instance = sti_class.allocate.init_with('attributes' => record)
  end

  instance
end

#sti_nameObject


54
55
56
# File 'activerecord/lib/active_record/inheritance.rb', line 54

def sti_name
  store_full_sti_class ? name : name.demodulize
end

#symbolized_base_classObject


28
29
30
# File 'activerecord/lib/active_record/inheritance.rb', line 28

def symbolized_base_class
  @symbolized_base_class ||= base_class.to_s.to_sym
end

#symbolized_sti_nameObject


32
33
34
# File 'activerecord/lib/active_record/inheritance.rb', line 32

def symbolized_sti_name
  @symbolized_sti_name ||= sti_name.present? ? sti_name.to_sym : symbolized_base_class
end