Files

Class Index [+]

Quicksearch

ActiveRecord::Inheritance::ClassMethods

Attributes

abstract_class[RW]

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

Public Instance Methods

abstract_class?() click to toggle source

Returns whether this class is an abstract class or not.

    # File lib/active_record/inheritance.rb, line 50
50:       def abstract_class?
51:         defined?(@abstract_class) && @abstract_class == true
52:       end
base_class() click to toggle source

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.

    # File lib/active_record/inheritance.rb, line 42
42:       def base_class
43:         class_of_active_record_descendant(self)
44:       end
descends_from_active_record?() click to toggle source

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

    # File lib/active_record/inheritance.rb, line 15
15:       def descends_from_active_record?
16:         if superclass.abstract_class?
17:           superclass.descends_from_active_record?
18:         else
19:           superclass == Base || !columns_hash.include?(inheritance_column)
20:         end
21:       end
instantiate(record) click to toggle source

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.

    # File lib/active_record/inheritance.rb, line 61
61:       def instantiate(record)
62:         sti_class = find_sti_class(record[inheritance_column])
63:         record_id = sti_class.primary_key && record[sti_class.primary_key]
64: 
65:         if ActiveRecord::IdentityMap.enabled? && record_id
66:           instance = use_identity_map(sti_class, record_id, record)
67:         else
68:           instance = sti_class.allocate.init_with('attributes' => record)
69:         end
70: 
71:         instance
72:       end
sti_name() click to toggle source
    # File lib/active_record/inheritance.rb, line 54
54:       def sti_name
55:         store_full_sti_class ? name : name.demodulize
56:       end
symbolized_base_class() click to toggle source
    # File lib/active_record/inheritance.rb, line 28
28:       def symbolized_base_class
29:         @symbolized_base_class ||= base_class.to_s.to_sym
30:       end
symbolized_sti_name() click to toggle source
    # File lib/active_record/inheritance.rb, line 32
32:       def symbolized_sti_name
33:         @symbolized_sti_name ||= sti_name.present? ? sti_name.to_sym : symbolized_base_class
34:       end

Protected Instance Methods

class_of_active_record_descendant(klass) click to toggle source

Returns the class descending directly from ActiveRecord::Base or an abstract class, if any, in the inheritance hierarchy.

    # File lib/active_record/inheritance.rb, line 78
78:       def class_of_active_record_descendant(klass)
79:         if klass == Base || klass.superclass == Base || klass.superclass.abstract_class?
80:           klass
81:         elsif klass.superclass.nil?
82:           raise ActiveRecordError, "#{name} doesn't belong in a hierarchy descending from ActiveRecord"
83:         else
84:           class_of_active_record_descendant(klass.superclass)
85:         end
86:       end
compute_type(type_name) click to toggle source

Returns the class type of the record using the current module as a prefix. So descendants of MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.

     # File lib/active_record/inheritance.rb, line 90
 90:       def compute_type(type_name)
 91:         if type_name.match(/^::/)
 92:           # If the type is prefixed with a scope operator then we assume that
 93:           # the type_name is an absolute reference.
 94:           ActiveSupport::Dependencies.constantize(type_name)
 95:         else
 96:           # Build a list of candidates to search for
 97:           candidates = []
 98:           name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
 99:           candidates << type_name
100: 
101:           candidates.each do |candidate|
102:             begin
103:               constant = ActiveSupport::Dependencies.constantize(candidate)
104:               return constant if candidate == constant.to_s
105:             rescue NameError => e
106:               # We don't want to swallow NoMethodError < NameError errors
107:               raise e unless e.instance_of?(NameError)
108:             end
109:           end
110: 
111:           raise NameError, "uninitialized constant #{candidates.first}"
112:         end
113:       end

Private Instance Methods

find_sti_class(type_name) click to toggle source
     # File lib/active_record/inheritance.rb, line 132
132:       def find_sti_class(type_name)
133:         if type_name.blank? || !columns_hash.include?(inheritance_column)
134:           self
135:         else
136:           begin
137:             if store_full_sti_class
138:               ActiveSupport::Dependencies.constantize(type_name)
139:             else
140:               compute_type(type_name)
141:             end
142:           rescue NameError
143:             raise SubclassNotFound,
144:               "The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " +
145:               "This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " +
146:               "Please rename this column if you didn't intend it to be used for storing the inheritance class " +
147:               "or overwrite #{name}.inheritance_column to use another column for that information."
148:           end
149:         end
150:       end
type_condition(table = arel_table) click to toggle source
     # File lib/active_record/inheritance.rb, line 152
152:       def type_condition(table = arel_table)
153:         sti_column = table[inheritance_column.to_sym]
154:         sti_names  = ([self] + descendants).map { |model| model.sti_name }
155: 
156:         sti_column.in(sti_names)
157:       end
use_identity_map(sti_class, record_id, record) click to toggle source
     # File lib/active_record/inheritance.rb, line 117
117:       def use_identity_map(sti_class, record_id, record)
118:         if (column = sti_class.columns_hash[sti_class.primary_key]) && column.number?
119:           record_id = record_id.to_i
120:         end
121: 
122:         if instance = IdentityMap.get(sti_class, record_id)
123:           instance.reinit_with('attributes' => record)
124:         else
125:           instance = sti_class.allocate.init_with('attributes' => record)
126:           IdentityMap.add(instance)
127:         end
128: 
129:         instance
130:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.