Skip to content

Commit

Permalink
Remove Adapter autoloads in favor of require
Browse files Browse the repository at this point in the history
Adapters must be eager loaded to ensure they are defined
before they are used as namespacing.

cf6a074#diff-41f2b3509d33e1c65bb70ee0ec7a2eea
  • Loading branch information
bf4 committed Sep 18, 2015
1 parent 24a5f38 commit 46104e4
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 58 deletions.
33 changes: 0 additions & 33 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,6 @@ AllCops:
DisplayCopNames: true
DisplayStyleGuide: true

Style/IndentationConsistency:
Exclude:
- lib/active_model/serializer/adapter/flatten_json.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/flatten_json.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/flatten_json.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:
Enabled: false
Exclude:
Expand Down
2 changes: 0 additions & 2 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

module ActiveModel
class Serializer
extend ActiveSupport::Autoload

include Configuration
include Associations

Expand Down
15 changes: 8 additions & 7 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ class Adapter
UnknownAdapterError = Class.new(ArgumentError)
ADAPTER_MAP = {}
private_constant :ADAPTER_MAP if defined?(private_constant)
extend ActiveSupport::Autoload
autoload :FragmentCache
autoload :Json
autoload :JsonApi
autoload :Null
autoload :FlattenJson
autoload :CachedSerializer
require 'active_model/serializer/adapter/fragment_cache'
require 'active_model/serializer/adapter/cached_serializer'

def self.create(resource, options = {})
override = options.delete(:adapter)
Expand Down Expand Up @@ -128,6 +123,12 @@ def include_meta(json)
json[meta_key] = meta if meta
json
end

# Gotta be at the bottom to use the code above it :(
require 'active_model/serializer/adapter/null'
require 'active_model/serializer/adapter/json'
require 'active_model/serializer/adapter/flatten_json'
require 'active_model/serializer/adapter/json_api'
end
end
end
8 changes: 7 additions & 1 deletion lib/active_model/serializer/adapter/flatten_json.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class ActiveModel::Serializer::Adapter::FlattenJson < ActiveModel::Serializer::Adapter::Json
module ActiveModel
class Serializer
class Adapter
class FlattenJson < Json
def serializable_hash(options = {})
super.each_value.first
end
Expand All @@ -9,4 +12,7 @@ def serializable_hash(options = {})
def include_meta(json)
json
end
end
end
end
end
8 changes: 7 additions & 1 deletion lib/active_model/serializer/adapter/fragment_cache.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class ActiveModel::Serializer::Adapter::FragmentCache
module ActiveModel
class Serializer
class Adapter
class FragmentCache
attr_reader :serializer

def initialize(adapter, serializer, options)
Expand Down Expand Up @@ -76,4 +79,7 @@ def fragment_serializer(name, klass)
def to_valid_const_name(name)
name.gsub('::', '_')
end
end
end
end
end
8 changes: 7 additions & 1 deletion lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class ActiveModel::Serializer::Adapter::Json < ActiveModel::Serializer::Adapter
module ActiveModel
class Serializer
class Adapter
class Json < Adapter
extend ActiveSupport::Autoload
autoload :FragmentCache

Expand Down Expand Up @@ -44,4 +47,7 @@ def serializable_hash(options = nil)
def 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
10 changes: 9 additions & 1 deletion lib/active_model/serializer/adapter/json/fragment_cache.rb
Original file line number Diff line number Diff line change
@@ -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)
non_cached_hash.merge cached_hash
end
end
end
end
end
end
8 changes: 7 additions & 1 deletion lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class ActiveModel::Serializer::Adapter::JsonApi < ActiveModel::Serializer::Adapter
module ActiveModel
class Serializer
class Adapter
class JsonApi < Adapter
extend ActiveSupport::Autoload
autoload :PaginationLinks
autoload :FragmentCache
Expand Down Expand Up @@ -157,4 +160,7 @@ def _included_for(serializer, includes)
def links_for(serializer, options)
JsonApi::PaginationLinks.new(serializer.object, options[:context]).serializable_hash(options)
end
end
end
end
end
10 changes: 9 additions & 1 deletion lib/active_model/serializer/adapter/json_api/fragment_cache.rb
Original file line number Diff line number Diff line change
@@ -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)
hash = {}
core_cached = cached_hash.first
Expand All @@ -10,4 +14,8 @@ def fragment_cache(root, cached_hash, non_cached_hash)

hash.deep_merge no_root_non_cache.deep_merge no_root_cache
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class ActiveModel::Serializer::Adapter::JsonApi::PaginationLinks
module ActiveModel
class Serializer
class Adapter
class JsonApi < Adapter
class PaginationLinks
FIRST_PAGE = 1

attr_reader :collection, :context
Expand Down Expand Up @@ -47,4 +51,8 @@ def original_url
def query_parameters
@query_parameters ||= context.query_parameters
end
end
end
end
end
end
8 changes: 7 additions & 1 deletion lib/active_model/serializer/adapter/null.rb
Original file line number Diff line number Diff line change
@@ -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)
{}
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/active_model/serializer/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class << base
attr_accessor :_reflections
end

extend ActiveSupport::Autoload
autoload :Association
autoload :Reflection
autoload :SingularReflection
Expand Down
12 changes: 4 additions & 8 deletions test/serializers/adapter_for_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ class AdapterForTest < Minitest::Test

def setup
@previous_adapter = ActiveModel::Serializer.config.adapter
# Eager load adapters
ActiveModel::Serializer::Adapter.eager_load!
[:json_api, :flatten_json, :null, :json].each do |adapter_name|
ActiveModel::Serializer::Adapter.lookup(adapter_name)
end
end

def teardown
Expand Down Expand Up @@ -66,12 +61,13 @@ def test_adapter_class_for_unknown_adapter

def test_adapter_map
expected_adapter_map = {
'null'.freeze => ActiveModel::Serializer::Adapter::Null,
'json'.freeze => ActiveModel::Serializer::Adapter::Json,
'json_api'.freeze => ActiveModel::Serializer::Adapter::JsonApi,
'flatten_json'.freeze => ActiveModel::Serializer::Adapter::FlattenJson,
'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

def test_adapters
Expand Down

0 comments on commit 46104e4

Please sign in to comment.