Remove Adapter autoloads in favor of require

Adapters must be eager loaded to ensure they are defined
before they are used as namespacing.

cf6a074a1c (diff-41f2b3509d33e1c65bb70ee0ec7a2eea)
This commit is contained in:
Benjamin Fleischer 2015-09-18 10:19:40 -05:00
parent a30913229f
commit ad2ca3b45c
13 changed files with 75 additions and 58 deletions

View File

@ -8,39 +8,6 @@ AllCops:
DisplayCopNames: true DisplayCopNames: true
DisplayStyleGuide: true DisplayStyleGuide: true
Style/IndentationConsistency:
Exclude:
- lib/active_model/serializer/adapter/attributes.rb
- lib/active_model/serializer/adapter/fragment_cache.rb
- lib/active_model/serializer/adapter/json.rb
- lib/active_model/serializer/adapter/json/fragment_cache.rb
- lib/active_model/serializer/adapter/json_api.rb
- lib/active_model/serializer/adapter/json_api/fragment_cache.rb
- lib/active_model/serializer/adapter/json_api/pagination_links.rb
- lib/active_model/serializer/adapter/null.rb
Style/IndentationWidth:
Exclude:
- lib/active_model/serializer/adapter/attributes.rb
- lib/active_model/serializer/adapter/fragment_cache.rb
- lib/active_model/serializer/adapter/json.rb
- lib/active_model/serializer/adapter/json/fragment_cache.rb
- lib/active_model/serializer/adapter/json_api.rb
- lib/active_model/serializer/adapter/json_api/fragment_cache.rb
- lib/active_model/serializer/adapter/json_api/pagination_links.rb
- lib/active_model/serializer/adapter/null.rb
Style/AccessModifierIndentation:
Exclude:
- lib/active_model/serializer/adapter/attributes.rb
- lib/active_model/serializer/adapter/fragment_cache.rb
- lib/active_model/serializer/adapter/json.rb
- lib/active_model/serializer/adapter/json/fragment_cache.rb
- lib/active_model/serializer/adapter/json_api.rb
- lib/active_model/serializer/adapter/json_api/fragment_cache.rb
- lib/active_model/serializer/adapter/json_api/pagination_links.rb
- lib/active_model/serializer/adapter/null.rb
Lint/NestedMethodDefinition: Lint/NestedMethodDefinition:
Enabled: false Enabled: false
Exclude: Exclude:

View File

@ -9,8 +9,6 @@ require 'active_model/serializer/utils'
module ActiveModel module ActiveModel
class Serializer class Serializer
extend ActiveSupport::Autoload
include Configuration include Configuration
include Associations include Associations

View File

@ -4,13 +4,8 @@ module ActiveModel
UnknownAdapterError = Class.new(ArgumentError) UnknownAdapterError = Class.new(ArgumentError)
ADAPTER_MAP = {} ADAPTER_MAP = {}
private_constant :ADAPTER_MAP if defined?(private_constant) private_constant :ADAPTER_MAP if defined?(private_constant)
extend ActiveSupport::Autoload require 'active_model/serializer/adapter/fragment_cache'
autoload :Attributes require 'active_model/serializer/adapter/cached_serializer'
autoload :Null
autoload :FragmentCache
autoload :Json
autoload :JsonApi
autoload :CachedSerializer
def self.create(resource, options = {}) def self.create(resource, options = {})
override = options.delete(:adapter) override = options.delete(:adapter)
@ -131,6 +126,12 @@ module ActiveModel
json[meta_key] = meta if meta json[meta_key] = meta if meta
json json
end end
# Gotta be at the bottom to use the code above it :(
require 'active_model/serializer/adapter/null'
require 'active_model/serializer/adapter/attributes'
require 'active_model/serializer/adapter/json'
require 'active_model/serializer/adapter/json_api'
end end
end end
end end

View File

@ -1,4 +1,7 @@
class ActiveModel::Serializer::Adapter::Attributes < ActiveModel::Serializer::Adapter module ActiveModel
class Serializer
class Adapter
class Attributes < Adapter
def serializable_hash(options = nil) def serializable_hash(options = nil)
options ||= {} options ||= {}
if serializer.respond_to?(:each) if serializer.respond_to?(:each)
@ -48,3 +51,6 @@ class ActiveModel::Serializer::Adapter::Attributes < ActiveModel::Serializer::Ad
json json
end end
end end
end
end
end

View File

@ -1,4 +1,7 @@
class ActiveModel::Serializer::Adapter::FragmentCache module ActiveModel
class Serializer
class Adapter
class FragmentCache
attr_reader :serializer attr_reader :serializer
def initialize(adapter, serializer, options) def initialize(adapter, serializer, options)
@ -77,3 +80,6 @@ class ActiveModel::Serializer::Adapter::FragmentCache
name.gsub('::', '_') name.gsub('::', '_')
end end
end end
end
end
end

View File

@ -1,4 +1,7 @@
class ActiveModel::Serializer::Adapter::Json < ActiveModel::Serializer::Adapter module ActiveModel
class Serializer
class Adapter
class Json < Adapter
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
autoload :FragmentCache autoload :FragmentCache
@ -13,3 +16,6 @@ class ActiveModel::Serializer::Adapter::Json < ActiveModel::Serializer::Adapter
ActiveModel::Serializer::Adapter::Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash) ActiveModel::Serializer::Adapter::Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
end end
end end
end
end
end

View File

@ -1,5 +1,13 @@
class ActiveModel::Serializer::Adapter::Json::FragmentCache module ActiveModel
class Serializer
class Adapter
class Json
class FragmentCache
def fragment_cache(cached_hash, non_cached_hash) def fragment_cache(cached_hash, non_cached_hash)
non_cached_hash.merge cached_hash non_cached_hash.merge cached_hash
end end
end end
end
end
end
end

View File

@ -1,4 +1,7 @@
class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapter module ActiveModel
class Serializer
class Adapter
class JsonApi < Adapter
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
autoload :PaginationLinks autoload :PaginationLinks
autoload :FragmentCache autoload :FragmentCache
@ -158,3 +161,6 @@ class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapt
JsonApi::PaginationLinks.new(serializer.object, options[:context]).serializable_hash(options) JsonApi::PaginationLinks.new(serializer.object, options[:context]).serializable_hash(options)
end end
end end
end
end
end

View File

@ -1,4 +1,8 @@
class ActiveModel::Serializer::Adapter::JsonApi::FragmentCache module ActiveModel
class Serializer
class Adapter
class JsonApi
class FragmentCache
def fragment_cache(root, cached_hash, non_cached_hash) def fragment_cache(root, cached_hash, non_cached_hash)
hash = {} hash = {}
core_cached = cached_hash.first core_cached = cached_hash.first
@ -11,3 +15,7 @@ class ActiveModel::Serializer::Adapter::JsonApi::FragmentCache
hash.deep_merge no_root_non_cache.deep_merge no_root_cache hash.deep_merge no_root_non_cache.deep_merge no_root_cache
end end
end end
end
end
end
end

View File

@ -1,4 +1,8 @@
class ActiveModel::Serializer::Adapter::JsonApi::PaginationLinks module ActiveModel
class Serializer
class Adapter
class JsonApi < Adapter
class PaginationLinks
FIRST_PAGE = 1 FIRST_PAGE = 1
attr_reader :collection, :context attr_reader :collection, :context
@ -48,3 +52,7 @@ class ActiveModel::Serializer::Adapter::JsonApi::PaginationLinks
@query_parameters ||= context.query_parameters @query_parameters ||= context.query_parameters
end end
end end
end
end
end
end

View File

@ -1,5 +1,11 @@
class ActiveModel::Serializer::Adapter::Null < ActiveModel::Serializer::Adapter module ActiveModel
class Serializer
class Adapter
class Null < Adapter
def serializable_hash(options = nil) def serializable_hash(options = nil)
{} {}
end end
end end
end
end
end

View File

@ -15,6 +15,7 @@ module ActiveModel
attr_accessor :_reflections attr_accessor :_reflections
end end
extend ActiveSupport::Autoload
autoload :Association autoload :Association
autoload :Reflection autoload :Reflection
autoload :SingularReflection autoload :SingularReflection

View File

@ -5,11 +5,6 @@ module ActiveModel
def setup def setup
@previous_adapter = ActiveModel::Serializer.config.adapter @previous_adapter = ActiveModel::Serializer.config.adapter
# Eager load adapters
ActiveModel::Serializer::Adapter.eager_load!
[:json_api, :attributes, :null, :json].each do |adapter_name|
ActiveModel::Serializer::Adapter.lookup(adapter_name)
end
end end
def teardown def teardown
@ -66,12 +61,13 @@ module ActiveModel
def test_adapter_map def test_adapter_map
expected_adapter_map = { expected_adapter_map = {
'null'.freeze => ActiveModel::Serializer::Adapter::Null,
'json'.freeze => ActiveModel::Serializer::Adapter::Json, 'json'.freeze => ActiveModel::Serializer::Adapter::Json,
'json_api'.freeze => ActiveModel::Serializer::Adapter::JsonApi,
'attributes'.freeze => ActiveModel::Serializer::Adapter::Attributes, 'attributes'.freeze => ActiveModel::Serializer::Adapter::Attributes,
'null'.freeze => ActiveModel::Serializer::Adapter::Null 'json_api'.freeze => ActiveModel::Serializer::Adapter::JsonApi
} }
assert_equal ActiveModel::Serializer::Adapter.adapter_map, expected_adapter_map actual = ActiveModel::Serializer::Adapter.adapter_map
assert_equal actual, expected_adapter_map
end end
def test_adapters def test_adapters