aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/inheritance.rb
diff options
context:
space:
mode:
authorCody Cutrer <cody@cutrer.us>2015-03-24 09:09:46 -0600
committerCody Cutrer <cody@cutrer.us>2015-03-24 12:23:00 -0600
commit7c0f8c64fa1e8e055e2077255843f149e600024b (patch)
treef903ef2d256be508349a00b6e2ffb88c5666e4a1 /activerecord/lib/active_record/inheritance.rb
parentd024bad4d1f8307a66fd6684dc658fddee37147e (diff)
downloadrails-7c0f8c64fa1e8e055e2077255843f149e600024b.tar.gz
rails-7c0f8c64fa1e8e055e2077255843f149e600024b.tar.bz2
rails-7c0f8c64fa1e8e055e2077255843f149e600024b.zip
DRY up STI subclass logic
the newer method used for discriminating new records did not use the older and more robust method used for instantiating existing records, but did have a better post-check to ensure the sublass was in the hierarchy. so move the descendants check to find_sti_class, and then simply call find_sti_class from subclass_from_attributes now with fixed specs
Diffstat (limited to 'activerecord/lib/active_record/inheritance.rb')
-rw-r--r--activerecord/lib/active_record/inheritance.rb38
1 files changed, 19 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb
index 24098f72dc..d3ef88288c 100644
--- a/activerecord/lib/active_record/inheritance.rb
+++ b/activerecord/lib/active_record/inheritance.rb
@@ -55,7 +55,7 @@ module ActiveRecord
subclass = subclass_from_attributes(attrs)
end
- if subclass
+ if subclass && subclass != self
subclass.new(*args, &block)
else
super
@@ -167,17 +167,23 @@ module ActiveRecord
end
def find_sti_class(type_name)
- if store_full_sti_class
- ActiveSupport::Dependencies.constantize(type_name)
- else
- compute_type(type_name)
+ subclass = begin
+ 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)
+ raise SubclassNotFound, "Invalid single-table inheritance type: #{type_name} is not a subclass of #{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."
+ subclass
end
def type_condition(table = arel_table)
@@ -198,14 +204,8 @@ module ActiveRecord
def subclass_from_attributes(attrs)
subclass_name = attrs.with_indifferent_access[inheritance_column]
- if subclass_name.present? && subclass_name != self.name
- subclass = subclass_name.safe_constantize
-
- unless descendants.include?(subclass)
- raise ActiveRecord::SubclassNotFound.new("Invalid single-table inheritance type: #{subclass_name} is not a subclass of #{name}")
- end
-
- subclass
+ if subclass_name.present?
+ find_sti_class(subclass_name)
end
end
end