Merge pull request #1781 from bf4/serializer_cleanup_6

Fix up caching, especially fragment_cache
This commit is contained in:
Benjamin Fleischer 2016-06-08 23:39:40 -05:00
commit 3d9ab37ffa
15 changed files with 136 additions and 160 deletions

View File

@ -167,7 +167,7 @@ module ActiveModel
adapter_options ||= {} adapter_options ||= {}
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options) options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
cached_attributes = adapter_options[:cached_attributes] ||= {} cached_attributes = adapter_options[:cached_attributes] ||= {}
resource = cached_attributes(options[:fields], cached_attributes, adapter_instance) resource = fetch_attributes(options[:fields], cached_attributes, adapter_instance)
relationships = resource_relationships(adapter_options, options, adapter_instance) relationships = resource_relationships(adapter_options, options, adapter_instance)
resource.merge(relationships) resource.merge(relationships)
end end
@ -194,8 +194,6 @@ module ActiveModel
def read_attribute_for_serialization(attr) def read_attribute_for_serialization(attr)
if respond_to?(attr) if respond_to?(attr)
send(attr) send(attr)
elsif self.class._fragmented
self.class._fragmented.read_attribute_for_serialization(attr)
else else
object.read_attribute_for_serialization(attr) object.read_attribute_for_serialization(attr)
end end

View File

@ -7,9 +7,11 @@ module ActiveModel
deprecate :inherited, 'ActiveModelSerializers::Adapter::Base.' deprecate :inherited, 'ActiveModelSerializers::Adapter::Base.'
end end
# :nocov:
def initialize(serializer, options = {}) def initialize(serializer, options = {})
super(ActiveModelSerializers::Adapter::Base.new(serializer, options)) super(ActiveModelSerializers::Adapter::Base.new(serializer, options))
end end
# :nocov:
end end
end end
end end

View File

@ -2,9 +2,6 @@ module ActiveModel
class Serializer class Serializer
# @api private # @api private
class BelongsToReflection < SingularReflection class BelongsToReflection < SingularReflection
def macro
:belongs_to
end
end end
end end
end end

View File

@ -1,5 +1,3 @@
# TODO(BF): refactor file to be smaller
# rubocop:disable Metrics/ModuleLength
module ActiveModel module ActiveModel
class Serializer class Serializer
UndefinedCacheKey = Class.new(StandardError) UndefinedCacheKey = Class.new(StandardError)
@ -9,10 +7,9 @@ module ActiveModel
included do included do
with_options instance_writer: false, instance_reader: false do |serializer| with_options instance_writer: false, instance_reader: false do |serializer|
serializer.class_attribute :_cache # @api private : the cache store serializer.class_attribute :_cache # @api private : the cache store
serializer.class_attribute :_fragmented # @api private : @see ::fragmented
serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key. Ignored if the serializable object defines #cache_key. serializer.class_attribute :_cache_key # @api private : when present, is first item in cache_key. Ignored if the serializable object defines #cache_key.
serializer.class_attribute :_cache_only # @api private : when fragment caching, whitelists cached_attributes. Cannot combine with except serializer.class_attribute :_cache_only # @api private : when fragment caching, whitelists fetch_attributes. Cannot combine with except
serializer.class_attribute :_cache_except # @api private : when fragment caching, blacklists cached_attributes. Cannot combine with only serializer.class_attribute :_cache_except # @api private : when fragment caching, blacklists fetch_attributes. Cannot combine with only
serializer.class_attribute :_cache_options # @api private : used by CachedSerializer, passed to _cache.fetch serializer.class_attribute :_cache_options # @api private : used by CachedSerializer, passed to _cache.fetch
# _cache_options include: # _cache_options include:
# expires_in # expires_in
@ -21,7 +18,7 @@ module ActiveModel
# race_condition_ttl # race_condition_ttl
# Passed to ::_cache as # Passed to ::_cache as
# serializer.cache_store.fetch(cache_key, @klass._cache_options) # serializer.cache_store.fetch(cache_key, @klass._cache_options)
# Passed as second argument to serializer.cache_store.fetch(cache_key, self.class._cache_options) # Passed as second argument to serializer.cache_store.fetch(cache_key, serializer_class._cache_options)
serializer.class_attribute :_cache_digest_file_path # @api private : Derived at inheritance serializer.class_attribute :_cache_digest_file_path # @api private : Derived at inheritance
end end
end end
@ -71,19 +68,15 @@ module ActiveModel
_cache_options && _cache_options[:skip_digest] _cache_options && _cache_options[:skip_digest]
end end
def cached_attributes def fragmented_attributes
_cache_only ? _cache_only : _attributes - _cache_except cached = _cache_only ? _cache_only : _attributes - _cache_except
end cached = cached.map! { |field| _attributes_keys.fetch(field, field) }
non_cached = _attributes - cached
def non_cached_attributes non_cached = non_cached.map! { |field| _attributes_keys.fetch(field, field) }
_attributes - cached_attributes {
end cached: cached,
non_cached: non_cached
# @api private }
# Used by FragmentCache on the CachedSerializer
# to call attribute methods on the fragmented cached serializer.
def fragmented(serializer)
self._fragmented = serializer
end end
# Enables a serializer to be automatically cached # Enables a serializer to be automatically cached
@ -208,118 +201,65 @@ module ActiveModel
end end
end end
def cached_attributes(fields, cached_attributes, adapter_instance) ### INSTANCE METHODS
if self.class.cache_enabled? def fetch_attributes(fields, cached_attributes, adapter_instance)
if serializer_class.cache_enabled?
key = cache_key(adapter_instance) key = cache_key(adapter_instance)
cached_attributes.fetch(key) do cached_attributes.fetch(key) do
cache_check(adapter_instance) do serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
attributes(fields) attributes(fields, true)
end end
end end
elsif serializer_class.fragment_cache_enabled?
fetch_attributes_fragment(adapter_instance)
else else
cache_check(adapter_instance) do attributes(fields, true)
attributes(fields)
end
end end
end end
def cache_check(adapter_instance) def fetch(adapter_instance, cache_options = serializer_class._cache_options)
if self.class.cache_enabled? if serializer_class.cache_store
self.class.cache_store.fetch(cache_key(adapter_instance), self.class._cache_options) do serializer_class.cache_store.fetch(cache_key(adapter_instance), cache_options) do
yield yield
end end
elsif self.class.fragment_cache_enabled?
fetch_fragment_cache(adapter_instance)
else else
yield yield
end end
end end
# 1. Create a CachedSerializer and NonCachedSerializer from the serializer class # 1. Determine cached fields from serializer class options
# 2. Serialize the above two with the given adapter # 2. Get non_cached_fields and fetch cache_fields
# 3. Pass their serializations to the adapter +::fragment_cache+ # 3. Merge the two hashes using adapter_instance#fragment_cache
# def fetch_attributes_fragment(adapter_instance)
# It will split the serializer into two, one that will be cached and one that will not serializer_class._cache_options ||= {}
# serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key
# Given a resource name fields = serializer_class.fragmented_attributes
# 1. Dynamically creates a CachedSerializer and NonCachedSerializer
# for a given class 'name'
# 2. Call
# CachedSerializer.cache(serializer._cache_options)
# CachedSerializer.fragmented(serializer)
# NonCachedSerializer.cache(serializer._cache_options)
# 3. Build a hash keyed to the +cached+ and +non_cached+ serializers
# 4. Call +cached_attributes+ on the serializer class and the above hash
# 5. Return the hash
#
# @example
# When +name+ is <tt>User::Admin</tt>
# creates the Serializer classes (if they don't exist).
# CachedUser_AdminSerializer
# NonCachedUser_AdminSerializer
#
# Given a hash of its cached and non-cached serializers
# 1. Determine cached attributes from serializer class options
# 2. Add cached attributes to cached Serializer
# 3. Add non-cached attributes to non-cached Serializer
def fetch_fragment_cache(adapter_instance)
serializer_class_name = self.class.name.gsub('::'.freeze, '_'.freeze)
self.class._cache_options ||= {}
self.class._cache_options[:key] = self.class._cache_key if self.class._cache_key
cached_serializer = _get_or_create_fragment_cached_serializer(serializer_class_name) non_cached_fields = fields[:non_cached].dup
cached_hash = ActiveModelSerializers::SerializableResource.new( non_cached_hash = attributes(non_cached_fields, true)
object, include_directive = JSONAPI::IncludeDirective.new(non_cached_fields - non_cached_hash.keys)
serializer: cached_serializer, non_cached_hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
adapter: adapter_instance.class
).serializable_hash
non_cached_serializer = _get_or_create_fragment_non_cached_serializer(serializer_class_name) cached_fields = fields[:cached].dup
non_cached_hash = ActiveModelSerializers::SerializableResource.new( key = cache_key(adapter_instance)
object, cached_hash =
serializer: non_cached_serializer, serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
adapter: adapter_instance.class hash = attributes(cached_fields, true)
).serializable_hash include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
end
# Merge both results # Merge both results
adapter_instance.fragment_cache(cached_hash, non_cached_hash) adapter_instance.fragment_cache(cached_hash, non_cached_hash)
end end
def _get_or_create_fragment_cached_serializer(serializer_class_name)
cached_serializer = _get_or_create_fragment_serializer "Cached#{serializer_class_name}"
cached_serializer.cache(self.class._cache_options)
cached_serializer.type(self.class._type)
cached_serializer.fragmented(self)
self.class.cached_attributes.each do |attribute|
options = self.class._attributes_keys[attribute] || {}
cached_serializer.attribute(attribute, options)
end
cached_serializer
end
def _get_or_create_fragment_non_cached_serializer(serializer_class_name)
non_cached_serializer = _get_or_create_fragment_serializer "NonCached#{serializer_class_name}"
non_cached_serializer.type(self.class._type)
non_cached_serializer.fragmented(self)
self.class.non_cached_attributes.each do |attribute|
options = self.class._attributes_keys[attribute] || {}
non_cached_serializer.attribute(attribute, options)
end
non_cached_serializer
end
def _get_or_create_fragment_serializer(name)
return Object.const_get(name) if Object.const_defined?(name)
Object.const_set(name, Class.new(ActiveModel::Serializer))
end
def cache_key(adapter_instance) def cache_key(adapter_instance)
return @cache_key if defined?(@cache_key) return @cache_key if defined?(@cache_key)
parts = [] parts = []
parts << object_cache_key parts << object_cache_key
parts << adapter_instance.cache_key parts << adapter_instance.cache_key
parts << self.class._cache_digest unless self.class._skip_digest? parts << serializer_class._cache_digest unless serializer_class._skip_digest?
@cache_key = parts.join('/') @cache_key = parts.join('/')
end end
@ -328,15 +268,18 @@ module ActiveModel
def object_cache_key def object_cache_key
if object.respond_to?(:cache_key) if object.respond_to?(:cache_key)
object.cache_key object.cache_key
elsif (serializer_cache_key = (self.class._cache_key || self.class._cache_options[:key])) elsif (serializer_cache_key = (serializer_class._cache_key || serializer_class._cache_options[:key]))
object_time_safe = object.updated_at object_time_safe = object.updated_at
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime) object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
"#{serializer_cache_key}/#{object.id}-#{object_time_safe}" "#{serializer_cache_key}/#{object.id}-#{object_time_safe}"
else else
fail UndefinedCacheKey, "#{object.class} must define #cache_key, or the 'key:' option must be passed into '#{self.class}.cache'" fail UndefinedCacheKey, "#{object.class} must define #cache_key, or the 'key:' option must be passed into '#{serializer_class}.cache'"
end end
end end
def serializer_class
@serializer_class ||= self.class
end
end end
end end
end end
# rubocop:enable Metrics/ModuleLength

View File

@ -2,9 +2,6 @@ module ActiveModel
class Serializer class Serializer
# @api private # @api private
class HasManyReflection < CollectionReflection class HasManyReflection < CollectionReflection
def macro
:has_many
end
end end
end end
end end

View File

@ -2,9 +2,6 @@ module ActiveModel
class Serializer class Serializer
# @api private # @api private
class HasOneReflection < SingularReflection class HasOneReflection < SingularReflection
def macro
:has_one
end
end end
end end
end end

View File

@ -5,11 +5,13 @@ module ActiveModelSerializers
private_constant :ADAPTER_MAP if defined?(private_constant) private_constant :ADAPTER_MAP if defined?(private_constant)
class << self # All methods are class functions class << self # All methods are class functions
# :nocov:
def new(*args) def new(*args)
fail ArgumentError, 'Adapters inherit from Adapter::Base.' \ fail ArgumentError, 'Adapters inherit from Adapter::Base.' \
"Adapter.new called with args: '#{args.inspect}', from" \ "Adapter.new called with args: '#{args.inspect}', from" \
"'caller[0]'." "'caller[0]'."
end end
# :nocov:
def configured_adapter def configured_adapter
lookup(ActiveModelSerializers.config.adapter) lookup(ActiveModelSerializers.config.adapter)

View File

@ -294,7 +294,7 @@ module ActiveModelSerializers
# {http://jsonapi.org/format/#document-resource-objects Document Resource Objects} # {http://jsonapi.org/format/#document-resource-objects Document Resource Objects}
def resource_object_for(serializer) def resource_object_for(serializer)
resource_object = serializer.cache_check(self) do resource_object = serializer.fetch(self) do
resource_object = ResourceIdentifier.new(serializer, instance_options).as_json resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
requested_fields = fieldset && fieldset.fields_for(resource_object[:type]) requested_fields = fieldset && fieldset.fields_for(resource_object[:type])

View File

@ -6,8 +6,10 @@ module ActiveModelSerializers
Adapter::JsonApi::Deserialization.parse(*args) Adapter::JsonApi::Deserialization.parse(*args)
end end
# :nocov:
def jsonapi_parse!(*args) def jsonapi_parse!(*args)
Adapter::JsonApi::Deserialization.parse!(*args) Adapter::JsonApi::Deserialization.parse!(*args)
end end
# :nocov:
end end
end end

View File

@ -38,6 +38,7 @@ module ActiveModelSerializers
end end
# The following methods are needed to be minimally implemented for ActiveModel::Errors # The following methods are needed to be minimally implemented for ActiveModel::Errors
# :nocov:
def self.human_attribute_name(attr, _options = {}) def self.human_attribute_name(attr, _options = {})
attr attr
end end
@ -45,5 +46,6 @@ module ActiveModelSerializers
def self.lookup_ancestors def self.lookup_ancestors
[self] [self]
end end
# :nocov:
end end
end end

View File

@ -32,11 +32,13 @@ module ActiveModelSerializers
end end
end end
# :nocov:
generators do |app| generators do |app|
Rails::Generators.configure!(app.config.generators) Rails::Generators.configure!(app.config.generators)
Rails::Generators.hidden_namespaces.uniq! Rails::Generators.hidden_namespaces.uniq!
require 'generators/rails/resource_override' require 'generators/rails/resource_override'
end end
# :nocov:
if Rails.env.test? if Rails.env.test?
ActionController::TestCase.send(:include, ActiveModelSerializers::Test::Schema) ActionController::TestCase.send(:include, ActiveModelSerializers::Test::Schema)

View File

@ -123,7 +123,7 @@ module ActionController
id: 42, id: 42,
lat: '-23.550520', lat: '-23.550520',
lng: '-46.633309', lng: '-46.633309',
place: 'Nowhere' # is a virtual attribute on LocationSerializer address: 'Nowhere' # is a virtual attribute on LocationSerializer
} }
] ]
} }

View File

@ -14,7 +14,13 @@ module ActiveModelSerializers
end end
end end
class FragmentedSerializer < ActiveModel::Serializer; end class FragmentedSerializer < ActiveModel::Serializer
cache only: :id
def id
'special_id'
end
end
setup do setup do
@model = Author.new(id: 1, name: 'Steve K.') @model = Author.new(id: 1, name: 'Steve K.')
@ -42,7 +48,6 @@ module ActiveModelSerializers
end end
def test_id_defined_on_fragmented def test_id_defined_on_fragmented
FragmentedSerializer.fragmented(WithDefinedIdSerializer.new(@model))
test_id(FragmentedSerializer, 'special_id') test_id(FragmentedSerializer, 'special_id')
end end

View File

@ -117,7 +117,7 @@ module ActiveModelSerializers
e = assert_raises ActiveModel::Serializer::UndefinedCacheKey do e = assert_raises ActiveModel::Serializer::UndefinedCacheKey do
render_object_with_cache(article) render_object_with_cache(article)
end end
assert_match(/ActiveModelSerializers::CacheTest::Article must define #cache_key, or the 'key:' option must be passed into 'CachedActiveModelSerializers_CacheTest_ArticleSerializer.cache'/, e.message) assert_match(/ActiveModelSerializers::CacheTest::Article must define #cache_key, or the 'key:' option must be passed into 'ActiveModelSerializers::CacheTest::ArticleSerializer.cache'/, e.message)
end end
def test_cache_options_definition def test_cache_options_definition
@ -127,7 +127,7 @@ module ActiveModelSerializers
end end
def test_fragment_cache_definition def test_fragment_cache_definition
assert_equal([:name], @role_serializer.class._cache_only) assert_equal([:name, :slug], @role_serializer.class._cache_only)
assert_equal([:content], @bio_serializer.class._cache_except) assert_equal([:content], @bio_serializer.class._cache_except)
end end
@ -178,14 +178,14 @@ module ActiveModelSerializers
id: @location.id, id: @location.id,
lat: @location.lat, lat: @location.lat,
lng: @location.lng, lng: @location.lng,
place: 'Nowhere' address: 'Nowhere'
} }
hash = render_object_with_cache(@location) hash = render_object_with_cache(@location)
assert_equal(hash, expected_result) assert_equal(hash, expected_result)
key = "#{@location.cache_key}/#{adapter.cache_key}" key = "#{@location.cache_key}/#{adapter.cache_key}"
assert_equal({ place: 'Nowhere' }, cache_store.fetch(key)) assert_equal({ address: 'Nowhere' }, cache_store.fetch(key))
end end
def test_fragment_cache_with_inheritance def test_fragment_cache_with_inheritance
@ -204,7 +204,7 @@ module ActiveModelSerializers
# Based on original failing test by @kevintyll # Based on original failing test by @kevintyll
# rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/AbcSize
def test_a_serializer_rendered_by_two_adapter_returns_differently_cached_attributes def test_a_serializer_rendered_by_two_adapter_returns_differently_fetch_attributes
Object.const_set(:Alert, Class.new(ActiveModelSerializers::Model) do Object.const_set(:Alert, Class.new(ActiveModelSerializers::Model) do
attr_accessor :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at attr_accessor :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at
end) end)
@ -225,7 +225,7 @@ module ActiveModelSerializers
created_at: Time.new(2016, 3, 31, 21, 37, 35, 0) created_at: Time.new(2016, 3, 31, 21, 37, 35, 0)
) )
expected_cached_attributes = { expected_fetch_attributes = {
id: 1, id: 1,
status: 'fail', status: 'fail',
resource: 'resource-1', resource: 'resource-1',
@ -250,7 +250,7 @@ module ActiveModelSerializers
# Assert attributes are serialized correctly # Assert attributes are serialized correctly
serializable_alert = serializable(alert, serializer: AlertSerializer, adapter: :attributes) serializable_alert = serializable(alert, serializer: AlertSerializer, adapter: :attributes)
attributes_serialization = serializable_alert.as_json attributes_serialization = serializable_alert.as_json
assert_equal expected_cached_attributes, alert.attributes assert_equal expected_fetch_attributes, alert.attributes
assert_equal alert.attributes, attributes_serialization assert_equal alert.attributes, attributes_serialization
attributes_cache_key = serializable_alert.adapter.serializer.cache_key(serializable_alert.adapter) attributes_cache_key = serializable_alert.adapter.serializer.cache_key(serializable_alert.adapter)
assert_equal attributes_serialization, cache_store.fetch(attributes_cache_key) assert_equal attributes_serialization, cache_store.fetch(attributes_cache_key)
@ -296,23 +296,28 @@ module ActiveModelSerializers
assert actual.any? { |key| key =~ %r{author/author-\d+} } assert actual.any? { |key| key =~ %r{author/author-\d+} }
end end
def test_cached_attributes def test_fetch_attributes_from_cache
serializer = ActiveModel::Serializer::CollectionSerializer.new([@comment, @comment]) serializers = ActiveModel::Serializer::CollectionSerializer.new([@comment, @comment])
Timecop.freeze(Time.current) do Timecop.freeze(Time.current) do
render_object_with_cache(@comment) render_object_with_cache(@comment)
attributes = Adapter::Attributes.new(serializer) options = {}
include_directive = ActiveModelSerializers.default_include_directive adapter_options = {}
cached_attributes = ActiveModel::Serializer.cache_read_multi(serializer, attributes, include_directive) adapter_instance = ActiveModelSerializers::Adapter::Attributes.new(serializers, adapter_options)
serializers.serializable_hash(adapter_options, options, adapter_instance)
cached_attributes = adapter_options.fetch(:cached_attributes)
assert_equal cached_attributes["#{@comment.cache_key}/#{attributes.cache_key}"], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes include_directive = ActiveModelSerializers.default_include_directive
assert_equal cached_attributes["#{@comment.post.cache_key}/#{attributes.cache_key}"], Post.new(id: 'post', title: 'New Post', body: 'Body').attributes manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive)
assert_equal manual_cached_attributes, cached_attributes
assert_equal cached_attributes["#{@comment.cache_key}/#{adapter_instance.cache_key}"], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes
assert_equal cached_attributes["#{@comment.post.cache_key}/#{adapter_instance.cache_key}"], Post.new(id: 'post', title: 'New Post', body: 'Body').attributes
writer = @comment.post.blog.writer writer = @comment.post.blog.writer
writer_cache_key = writer.cache_key writer_cache_key = writer.cache_key
assert_equal cached_attributes["#{writer_cache_key}/#{adapter_instance.cache_key}"], Author.new(id: 'author', name: 'Joao M. D. Moura').attributes
assert_equal cached_attributes["#{writer_cache_key}/#{attributes.cache_key}"], Author.new(id: 'author', name: 'Joao M. D. Moura').attributes
end end
end end
@ -429,25 +434,53 @@ module ActiveModelSerializers
end end
def test_fragment_fetch_with_virtual_attributes def test_fragment_fetch_with_virtual_attributes
@author = Author.new(name: 'Joao M. D. Moura') author = Author.new(name: 'Joao M. D. Moura')
@role = Role.new(name: 'Great Author', description: nil) role = Role.new(name: 'Great Author', description: nil)
@role.author = [@author] role.author = [author]
@role_serializer = RoleSerializer.new(@role) role_serializer = RoleSerializer.new(role)
@role_hash = @role_serializer.fetch_fragment_cache(ActiveModelSerializers::Adapter.configured_adapter.new(@role_serializer)) adapter_instance = ActiveModelSerializers::Adapter.configured_adapter.new(role_serializer)
expected_result = { expected_result = {
id: @role.id, id: role.id,
description: @role.description, description: role.description,
slug: "#{@role.name}-#{@role.id}", slug: "#{role.name}-#{role.id}",
name: @role.name name: role.name
} }
assert_equal(@role_hash, expected_result) cache_store.clear
role_hash = role_serializer.fetch_attributes_fragment(adapter_instance)
assert_equal(role_hash, expected_result)
role.attributes[:id] = 'this has been updated'
role.name = 'this was cached'
role_hash = role_serializer.fetch_attributes_fragment(adapter_instance)
assert_equal(expected_result.merge(id: role.id), role_hash)
end
def test_fragment_fetch_with_except
adapter_instance = ActiveModelSerializers::Adapter.configured_adapter.new(@bio_serializer)
expected_result = {
id: @bio.id,
rating: nil,
content: @bio.content
}
cache_store.clear
bio_hash = @bio_serializer.fetch_attributes_fragment(adapter_instance)
assert_equal(expected_result, bio_hash)
@bio.content = 'this has been updated'
@bio.rating = 'this was cached'
bio_hash = @bio_serializer.fetch_attributes_fragment(adapter_instance)
assert_equal(expected_result.merge(content: @bio.content), bio_hash)
end end
def test_fragment_fetch_with_namespaced_object def test_fragment_fetch_with_namespaced_object
@spam = Spam::UnrelatedLink.new(id: 'spam-id-1') @spam = Spam::UnrelatedLink.new(id: 'spam-id-1')
@spam_serializer = Spam::UnrelatedLinkSerializer.new(@spam) @spam_serializer = Spam::UnrelatedLinkSerializer.new(@spam)
@spam_hash = @spam_serializer.fetch_fragment_cache(ActiveModelSerializers::Adapter.configured_adapter.new(@spam_serializer)) adapter_instance = ActiveModelSerializers::Adapter.configured_adapter.new(@spam_serializer)
@spam_hash = @spam_serializer.fetch_attributes_fragment(adapter_instance)
expected_result = { expected_result = {
id: @spam.id id: @spam.id
} }
@ -476,10 +509,5 @@ module ActiveModelSerializers
def adapter def adapter
@serializable_resource.adapter @serializable_resource.adapter
end end
def cached_serialization(serializer)
cache_key = serializer.cache_key(adapter)
cache_store.fetch(cache_key)
end
end end
end end

11
test/fixtures/poro.rb vendored
View File

@ -136,10 +136,11 @@ AuthorSerializer = Class.new(ActiveModel::Serializer) do
end end
RoleSerializer = Class.new(ActiveModel::Serializer) do RoleSerializer = Class.new(ActiveModel::Serializer) do
cache only: [:name], skip_digest: true cache only: [:name, :slug], skip_digest: true
attributes :id, :name, :description, :slug attributes :id, :name, :description
attribute :friendly_id, key: :slug
def slug def friendly_id
"#{object.name}-#{object.id}" "#{object.name}-#{object.id}"
end end
@ -153,10 +154,10 @@ LikeSerializer = Class.new(ActiveModel::Serializer) do
end end
LocationSerializer = Class.new(ActiveModel::Serializer) do LocationSerializer = Class.new(ActiveModel::Serializer) do
cache only: [:place], skip_digest: true cache only: [:address], skip_digest: true
attributes :id, :lat, :lng attributes :id, :lat, :lng
belongs_to :place belongs_to :place, key: :address
def place def place
'Nowhere' 'Nowhere'