diff --git a/.rubocop.yml b/.rubocop.yml index 43ccb9f56..e7d729cc0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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: diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index e77ddfd5b..62f427517 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -9,8 +9,6 @@ module ActiveModel class Serializer - extend ActiveSupport::Autoload - include Configuration include Associations diff --git a/lib/active_model/serializer/adapter.rb b/lib/active_model/serializer/adapter.rb index 6b5686b57..4a681291f 100644 --- a/lib/active_model/serializer/adapter.rb +++ b/lib/active_model/serializer/adapter.rb @@ -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) @@ -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 diff --git a/lib/active_model/serializer/adapter/flatten_json.rb b/lib/active_model/serializer/adapter/flatten_json.rb index bf0a690e7..e7bda1743 100644 --- a/lib/active_model/serializer/adapter/flatten_json.rb +++ b/lib/active_model/serializer/adapter/flatten_json.rb @@ -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 @@ -9,4 +12,7 @@ def serializable_hash(options = {}) def include_meta(json) json end + end + end + end end diff --git a/lib/active_model/serializer/adapter/fragment_cache.rb b/lib/active_model/serializer/adapter/fragment_cache.rb index 8b1380f26..7b47c1d49 100644 --- a/lib/active_model/serializer/adapter/fragment_cache.rb +++ b/lib/active_model/serializer/adapter/fragment_cache.rb @@ -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) @@ -76,4 +79,7 @@ def fragment_serializer(name, klass) def to_valid_const_name(name) name.gsub('::', '_') end + end + end + end end diff --git a/lib/active_model/serializer/adapter/json.rb b/lib/active_model/serializer/adapter/json.rb index aec8fc4ef..5bc133361 100644 --- a/lib/active_model/serializer/adapter/json.rb +++ b/lib/active_model/serializer/adapter/json.rb @@ -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 @@ -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 diff --git a/lib/active_model/serializer/adapter/json/fragment_cache.rb b/lib/active_model/serializer/adapter/json/fragment_cache.rb index 5e687241d..76ba1ee6d 100644 --- a/lib/active_model/serializer/adapter/json/fragment_cache.rb +++ b/lib/active_model/serializer/adapter/json/fragment_cache.rb @@ -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 diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 89d196e83..87b40999e 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -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 @@ -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 diff --git a/lib/active_model/serializer/adapter/json_api/fragment_cache.rb b/lib/active_model/serializer/adapter/json_api/fragment_cache.rb index ab3481307..edadb4ec7 100644 --- a/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +++ b/lib/active_model/serializer/adapter/json_api/fragment_cache.rb @@ -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 @@ -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 diff --git a/lib/active_model/serializer/adapter/json_api/pagination_links.rb b/lib/active_model/serializer/adapter/json_api/pagination_links.rb index 8661f3b14..55e3280b8 100644 --- a/lib/active_model/serializer/adapter/json_api/pagination_links.rb +++ b/lib/active_model/serializer/adapter/json_api/pagination_links.rb @@ -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 @@ -47,4 +51,8 @@ def original_url def query_parameters @query_parameters ||= context.query_parameters end + end + end + end + end end diff --git a/lib/active_model/serializer/adapter/null.rb b/lib/active_model/serializer/adapter/null.rb index 78f9b8e3b..1728f88ed 100644 --- a/lib/active_model/serializer/adapter/null.rb +++ b/lib/active_model/serializer/adapter/null.rb @@ -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 diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index c8628435a..660ff0d16 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -15,6 +15,7 @@ class << base attr_accessor :_reflections end + extend ActiveSupport::Autoload autoload :Association autoload :Reflection autoload :SingularReflection diff --git a/test/serializers/adapter_for_test.rb b/test/serializers/adapter_for_test.rb index ae7c95bb8..e0d39c921 100644 --- a/test/serializers/adapter_for_test.rb +++ b/test/serializers/adapter_for_test.rb @@ -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 @@ -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