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