aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorAndrew White <pixeltrix@users.noreply.github.com>2017-01-03 15:50:06 +0000
committerGitHub <noreply@github.com>2017-01-03 15:50:06 +0000
commitec7255a2d6f49f7f7b305c10a50f65bfd2a1511e (patch)
tree6f8c50e8bf9e628720b90b6c5cea16f7f5b7531d /activerecord/lib
parent33e60514aed85b3076f2636d5f1ccfb513aace1c (diff)
parentcdf8a2b49307953fbcd93df9fdec3c23063740b1 (diff)
downloadrails-ec7255a2d6f49f7f7b305c10a50f65bfd2a1511e.tar.gz
rails-ec7255a2d6f49f7f7b305c10a50f65bfd2a1511e.tar.bz2
rails-ec7255a2d6f49f7f7b305c10a50f65bfd2a1511e.zip
Merge pull request #27537 from Le6ow5k1/sti
Cache results of computing model type
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/inheritance.rb17
1 files changed, 15 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb
index a1d4f47372..fbdaeaae51 100644
--- a/activerecord/lib/active_record/inheritance.rb
+++ b/activerecord/lib/active_record/inheritance.rb
@@ -130,16 +130,26 @@ module ActiveRecord
store_full_sti_class ? name : name.demodulize
end
+ def inherited(subclass)
+ subclass.instance_variable_set(:@_type_candidates_cache, Concurrent::Map.new)
+ super
+ end
+
protected
# 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.
def compute_type(type_name)
- if type_name.match(/^::/)
+ if type_name.start_with?("::".freeze)
# If the type is prefixed with a scope operator then we assume that
# the type_name is an absolute reference.
ActiveSupport::Dependencies.constantize(type_name)
else
+ type_candidate = @_type_candidates_cache[type_name]
+ if type_candidate && type_constant = ActiveSupport::Dependencies.safe_constantize(type_candidate)
+ return type_constant
+ end
+
# Build a list of candidates to search for
candidates = []
name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
@@ -147,7 +157,10 @@ module ActiveRecord
candidates.each do |candidate|
constant = ActiveSupport::Dependencies.safe_constantize(candidate)
- return constant if candidate == constant.to_s
+ if candidate == constant.to_s
+ @_type_candidates_cache[type_name] = candidate
+ return constant
+ end
end
raise NameError.new("uninitialized constant #{candidates.first}", candidates.first)