Skip to content

Commit

Permalink
Move serializer caching from adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Mar 14, 2016
1 parent 093d198 commit 3a91802
Show file tree
Hide file tree
Showing 19 changed files with 244 additions and 294 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Fixes:
- [#1488](https://github.com/rails-api/active_model_serializers/pull/1488) Require ActiveSupport's string inflections (@nate00)

Misc:
- [#1471](https://github.com/rails-api/active_model_serializers/pull/1471) [Cleanup] Serializer caching is its own concern. (@bf4)
- [#1482](https://github.com/rails-api/active_model_serializers/pull/1482) Document JSON API implementation defs and progress in class. (@bf4)
- [#1551](https://github.com/rails-api/active_model_serializers/pull/1551) Added codebeat badge (@korzonek)
- [#1527](https://github.com/rails-api/active_model_serializers/pull/1527) Refactor fragment cache class. (@groyoh)
Expand Down
5 changes: 4 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ def self.serializer_for(resource, options = {})
# @see ActiveModelSerializers::Adapter.lookup
# Deprecated
def self.adapter
warn 'Calling adapter method in Serializer, please use the ActiveModelSerializers::Adapter.configured_adapter'
ActiveModelSerializers::Adapter.lookup(config.adapter)
end
class << self
extend ActiveModelSerializers::Deprecate
deprecate :adapter, 'ActiveModelSerializers::Adapter.configured_adapter'
end

# @api private
def self.serializer_lookup_chain_for(klass)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _attributes
# @api private
# maps attribute value to explict key name
# @see Serializer::attribute
# @see Adapter::FragmentCache#fragment_serializer
# @see FragmentCache#fragment_serializer
def _attributes_keys
_attributes_data
.each_with_object({}) do |(key, attr), hash|
Expand Down
2 changes: 2 additions & 0 deletions lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module ActiveModelSerializers
extend ActiveSupport::Autoload
autoload :Model
autoload :CachedSerializer
autoload :FragmentCache
autoload :Callbacks
autoload :Deserialization
autoload :Logging
Expand Down
2 changes: 0 additions & 2 deletions lib/active_model_serializers/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module Adapter
UnknownAdapterError = Class.new(ArgumentError)
ADAPTER_MAP = {} # rubocop:disable Style/MutableConstant
private_constant :ADAPTER_MAP if defined?(private_constant)
require 'active_model_serializers/adapter/fragment_cache'
require 'active_model_serializers/adapter/cached_serializer'

class << self # All methods are class functions
def new(*args)
Expand Down
4 changes: 0 additions & 4 deletions lib/active_model_serializers/adapter/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ def serializable_hash(options = nil)
end
end

def fragment_cache(cached_hash, non_cached_hash)
Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
end

private

def serializable_hash_for_collection(options)
Expand Down
4 changes: 2 additions & 2 deletions lib/active_model_serializers/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def as_json(options = nil)
hash
end

def fragment_cache(*_args)
fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
def fragment_cache(cached_hash, non_cached_hash)
non_cached_hash.merge cached_hash
end

def cache_check(serializer)
Expand Down
77 changes: 0 additions & 77 deletions lib/active_model_serializers/adapter/cached_serializer.rb

This file was deleted.

118 changes: 0 additions & 118 deletions lib/active_model_serializers/adapter/fragment_cache.rb

This file was deleted.

9 changes: 0 additions & 9 deletions lib/active_model_serializers/adapter/json.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
module ActiveModelSerializers
module Adapter
class Json < Base
extend ActiveSupport::Autoload
autoload :FragmentCache

def serializable_hash(options = nil)
options ||= {}
{ root => Attributes.new(serializer, instance_options).serializable_hash(options) }
end

private

def fragment_cache(cached_hash, non_cached_hash)
ActiveModelSerializers::Adapter::Json::FragmentCache.new.fragment_cache(cached_hash, non_cached_hash)
end
end
end
end
11 changes: 0 additions & 11 deletions lib/active_model_serializers/adapter/json/fragment_cache.rb

This file was deleted.

10 changes: 8 additions & 2 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module ActiveModelSerializers
module Adapter
class JsonApi < Base
extend ActiveSupport::Autoload
autoload :FragmentCache
autoload :Jsonapi
autoload :ResourceIdentifier
autoload :Relationship
Expand Down Expand Up @@ -169,7 +168,14 @@ def failure_document

def fragment_cache(cached_hash, non_cached_hash)
root = false if instance_options.include?(:include)
FragmentCache.new.fragment_cache(root, cached_hash, non_cached_hash)
core_cached = cached_hash.first
core_non_cached = non_cached_hash.first
no_root_cache = cached_hash.delete_if { |key, _value| key == core_cached[0] }
no_root_non_cache = non_cached_hash.delete_if { |key, _value| key == core_non_cached[0] }
cached_resource = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1]
hash = root ? { root => cached_resource } : cached_resource

hash.deep_merge no_root_non_cache.deep_merge no_root_cache
end

protected
Expand Down
18 changes: 0 additions & 18 deletions lib/active_model_serializers/adapter/json_api/fragment_cache.rb

This file was deleted.

Loading

0 comments on commit 3a91802

Please sign in to comment.