mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Backport caching of the constant lookup
This commit is contained in:
parent
c2565ed726
commit
039e6eac34
@ -4,11 +4,13 @@ module ActiveModel
|
||||
extend self
|
||||
|
||||
def _const_get(const)
|
||||
begin
|
||||
method = RUBY_VERSION >= '2.0' ? :const_get : :qualified_const_get
|
||||
Object.send method, const
|
||||
rescue NameError
|
||||
const.safe_constantize
|
||||
Serializer.serializers_cache.fetch_or_store(const) do
|
||||
begin
|
||||
method = RUBY_VERSION >= '2.0' ? :const_get : :qualified_const_get
|
||||
Object.send method, const
|
||||
rescue NameError
|
||||
const.safe_constantize
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -4,6 +4,7 @@ require 'active_model/serializer/association'
|
||||
require 'active_model/serializer/config'
|
||||
|
||||
require 'thread'
|
||||
require 'concurrent/map'
|
||||
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
@ -100,6 +101,10 @@ end
|
||||
associate(Association::HasMany, *attrs)
|
||||
end
|
||||
|
||||
def serializers_cache
|
||||
@serializers_cache ||= Concurrent::Map.new
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def strip_attribute(attr)
|
||||
|
||||
@ -14,6 +14,9 @@ begin
|
||||
ActionController::Base.send(:include, ::ActionController::Serialization)
|
||||
ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
|
||||
end
|
||||
ActionDispatch::Reloader.to_prepare do
|
||||
ActiveModel::Serializer.serializers_cache.clear
|
||||
end
|
||||
end
|
||||
rescue LoadError
|
||||
# rails not installed, continuing
|
||||
|
||||
Loading…
Reference in New Issue
Block a user