Eager load modules on boot

Using ActiveModelSerializers with a threaded web server eg. Puma
uninitialized constant errors are thrown. Leaving this article for
reference:
http://blog.plataformatec.com.br/2012/08/eager-loading-for-greater-good/.
This commit is contained in:
Fabian Mersch 2018-05-22 15:06:06 +02:00
parent fce8be0dc0
commit 2a8b9f4105
4 changed files with 42 additions and 28 deletions

View File

@ -18,16 +18,18 @@ module ActiveModel
# @see #serializable_hash for more details on these valid keys. # @see #serializable_hash for more details on these valid keys.
SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include, :root].freeze
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
autoload :Adapter eager_autoload do
autoload :Null autoload :Adapter
autoload :Attribute autoload :Null
autoload :Association autoload :Attribute
autoload :Reflection autoload :Association
autoload :SingularReflection autoload :Reflection
autoload :CollectionReflection autoload :SingularReflection
autoload :BelongsToReflection autoload :CollectionReflection
autoload :HasOneReflection autoload :BelongsToReflection
autoload :HasManyReflection autoload :HasOneReflection
autoload :HasManyReflection
end
include ActiveSupport::Configurable include ActiveSupport::Configurable
include Caching include Caching

View File

@ -5,16 +5,19 @@ require 'active_support/core_ext/string/inflections'
require 'active_support/json' require 'active_support/json'
module ActiveModelSerializers module ActiveModelSerializers
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
autoload :Model eager_autoload do
autoload :Callbacks autoload :Model
autoload :Deserialization autoload :Callbacks
autoload :SerializableResource autoload :SerializableResource
autoload :Logging autoload :SerializationContext
autoload :Test autoload :Logging
autoload :Adapter autoload :Test
autoload :JsonPointer autoload :Adapter
autoload :Deprecate autoload :JsonPointer
autoload :LookupChain autoload :Deprecate
autoload :LookupChain
autoload :Deserialization
end
class << self; attr_accessor :logger; end class << self; attr_accessor :logger; end
self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
@ -46,6 +49,11 @@ module ActiveModelSerializers
$VERBOSE = original_verbose $VERBOSE = original_verbose
end end
def self.eager_load!
super
ActiveModel::Serializer.eager_load!
end
require 'active_model/serializer/version' require 'active_model/serializer/version'
require 'active_model/serializer' require 'active_model/serializer'
require 'active_model/serializable_resource' require 'active_model/serializable_resource'

View File

@ -22,14 +22,16 @@ module ActiveModelSerializers
module Adapter module Adapter
class JsonApi < Base class JsonApi < Base
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
autoload :Jsonapi eager_autoload do
autoload :ResourceIdentifier autoload :Jsonapi
autoload :Relationship autoload :ResourceIdentifier
autoload :Link autoload :Link
autoload :PaginationLinks autoload :PaginationLinks
autoload :Meta autoload :Meta
autoload :Error autoload :Error
autoload :Deserialization autoload :Deserialization
autoload :Relationship
end
def self.default_key_transform def self.default_key_transform
:dash :dash

View File

@ -5,6 +5,8 @@ require 'action_controller/serialization'
module ActiveModelSerializers module ActiveModelSerializers
class Railtie < Rails::Railtie class Railtie < Rails::Railtie
config.eager_load_namespaces << ActiveModelSerializers
config.to_prepare do config.to_prepare do
ActiveModel::Serializer.serializers_cache.clear ActiveModel::Serializer.serializers_cache.clear
end end