mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #37500 from rafaelfranca/rm-sti-config
Add methods to the public API of ActiveRecord::Inheritance
This commit is contained in:
commit
b305f0e206
2 changed files with 30 additions and 14 deletions
|
@ -6,7 +6,7 @@ module ActiveRecord
|
||||||
class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
|
class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
|
||||||
def klass
|
def klass
|
||||||
type = owner[reflection.foreign_type]
|
type = owner[reflection.foreign_type]
|
||||||
type.presence && type.constantize
|
type.presence && owner.class.polymorphic_class_for(type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def target_changed?
|
def target_changed?
|
||||||
|
|
|
@ -162,14 +162,40 @@ module ActiveRecord
|
||||||
defined?(@abstract_class) && @abstract_class == true
|
defined?(@abstract_class) && @abstract_class == true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the value to be stored in the inheritance column for STI.
|
||||||
def sti_name
|
def sti_name
|
||||||
store_full_sti_class ? name : name.demodulize
|
store_full_sti_class ? name : name.demodulize
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the class for the provided +type_name+.
|
||||||
|
#
|
||||||
|
# It is used to find the class correspondent to the value stored in the inheritance column.
|
||||||
|
def sti_class_for(type_name)
|
||||||
|
if store_full_sti_class
|
||||||
|
ActiveSupport::Dependencies.constantize(type_name)
|
||||||
|
else
|
||||||
|
compute_type(type_name)
|
||||||
|
end
|
||||||
|
rescue NameError
|
||||||
|
raise SubclassNotFound,
|
||||||
|
"The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " \
|
||||||
|
"This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " \
|
||||||
|
"Please rename this column if you didn't intend it to be used for storing the inheritance class " \
|
||||||
|
"or overwrite #{name}.inheritance_column to use another column for that information."
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the value to be stored in the polymorphic type column for Polymorphic Associations.
|
||||||
def polymorphic_name
|
def polymorphic_name
|
||||||
base_class.name
|
base_class.name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the class for the provided +name+.
|
||||||
|
#
|
||||||
|
# It is used to find the class correspondent to the value stored in the polymorphic type column.
|
||||||
|
def polymorphic_class_for(name)
|
||||||
|
name.constantize
|
||||||
|
end
|
||||||
|
|
||||||
def inherited(subclass)
|
def inherited(subclass)
|
||||||
subclass.instance_variable_set(:@_type_candidates_cache, Concurrent::Map.new)
|
subclass.instance_variable_set(:@_type_candidates_cache, Concurrent::Map.new)
|
||||||
super
|
super
|
||||||
|
@ -224,22 +250,12 @@ module ActiveRecord
|
||||||
|
|
||||||
def find_sti_class(type_name)
|
def find_sti_class(type_name)
|
||||||
type_name = base_class.type_for_attribute(inheritance_column).cast(type_name)
|
type_name = base_class.type_for_attribute(inheritance_column).cast(type_name)
|
||||||
subclass = begin
|
subclass = sti_class_for(type_name)
|
||||||
if store_full_sti_class
|
|
||||||
ActiveSupport::Dependencies.constantize(type_name)
|
|
||||||
else
|
|
||||||
compute_type(type_name)
|
|
||||||
end
|
|
||||||
rescue NameError
|
|
||||||
raise SubclassNotFound,
|
|
||||||
"The single-table inheritance mechanism failed to locate the subclass: '#{type_name}'. " \
|
|
||||||
"This error is raised because the column '#{inheritance_column}' is reserved for storing the class in case of inheritance. " \
|
|
||||||
"Please rename this column if you didn't intend it to be used for storing the inheritance class " \
|
|
||||||
"or overwrite #{name}.inheritance_column to use another column for that information."
|
|
||||||
end
|
|
||||||
unless subclass == self || descendants.include?(subclass)
|
unless subclass == self || descendants.include?(subclass)
|
||||||
raise SubclassNotFound, "Invalid single-table inheritance type: #{subclass.name} is not a subclass of #{name}"
|
raise SubclassNotFound, "Invalid single-table inheritance type: #{subclass.name} is not a subclass of #{name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
subclass
|
subclass
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue