mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Ensuring read_multi works with fragment cache. (#1814)
* Ensuring read_multi works with fragment cache.
This commit is contained in:
parent
32a3b53892
commit
bcf3358524
@ -7,6 +7,7 @@ Breaking changes:
|
||||
Features:
|
||||
|
||||
Fixes:
|
||||
- [#1814] (https://github.com/rails-api/active_model_serializers/pull/1814) Ensuring read_multi works with fragment cache
|
||||
|
||||
Misc:
|
||||
|
||||
|
||||
@ -197,7 +197,7 @@ module ActiveModel
|
||||
def object_cache_key(serializer, adapter_instance)
|
||||
return unless serializer.present? && serializer.object.present?
|
||||
|
||||
serializer.class.cache_enabled? ? serializer.cache_key(adapter_instance) : nil
|
||||
(serializer.class.cache_enabled? || serializer.class.fragment_cache_enabled?) ? serializer.cache_key(adapter_instance) : nil
|
||||
end
|
||||
end
|
||||
|
||||
@ -211,7 +211,7 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
elsif serializer_class.fragment_cache_enabled?
|
||||
fetch_attributes_fragment(adapter_instance)
|
||||
fetch_attributes_fragment(adapter_instance, cached_attributes)
|
||||
else
|
||||
attributes(fields, true)
|
||||
end
|
||||
@ -230,7 +230,8 @@ module ActiveModel
|
||||
# 1. Determine cached fields from serializer class options
|
||||
# 2. Get non_cached_fields and fetch cache_fields
|
||||
# 3. Merge the two hashes using adapter_instance#fragment_cache
|
||||
def fetch_attributes_fragment(adapter_instance)
|
||||
# rubocop:disable Metrics/AbcSize
|
||||
def fetch_attributes_fragment(adapter_instance, cached_attributes = {})
|
||||
serializer_class._cache_options ||= {}
|
||||
serializer_class._cache_options[:key] = serializer_class._cache_key if serializer_class._cache_key
|
||||
fields = serializer_class.fragmented_attributes
|
||||
@ -243,15 +244,17 @@ module ActiveModel
|
||||
cached_fields = fields[:cached].dup
|
||||
key = cache_key(adapter_instance)
|
||||
cached_hash =
|
||||
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
||||
hash = attributes(cached_fields, true)
|
||||
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
||||
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
||||
cached_attributes.fetch(key) do
|
||||
serializer_class.cache_store.fetch(key, serializer_class._cache_options) do
|
||||
hash = attributes(cached_fields, true)
|
||||
include_directive = JSONAPI::IncludeDirective.new(cached_fields - hash.keys)
|
||||
hash.merge! resource_relationships({}, { include_directive: include_directive }, adapter_instance)
|
||||
end
|
||||
end
|
||||
|
||||
# Merge both results
|
||||
adapter_instance.fragment_cache(cached_hash, non_cached_hash)
|
||||
end
|
||||
# rubocop:enable Metrics/AbcSize
|
||||
|
||||
def cache_key(adapter_instance)
|
||||
return @cache_key if defined?(@cache_key)
|
||||
|
||||
@ -321,6 +321,34 @@ module ActiveModelSerializers
|
||||
end
|
||||
end
|
||||
|
||||
def test_cache_read_multi_with_fragment_cache_enabled
|
||||
post_serializer = Class.new(ActiveModel::Serializer) do
|
||||
cache except: [:body]
|
||||
end
|
||||
|
||||
serializers = ActiveModel::Serializer::CollectionSerializer.new([@post, @post], serializer: post_serializer)
|
||||
|
||||
Timecop.freeze(Time.current) do
|
||||
# Warming up.
|
||||
options = {}
|
||||
adapter_options = {}
|
||||
adapter_instance = ActiveModelSerializers::Adapter::Attributes.new(serializers, adapter_options)
|
||||
serializers.serializable_hash(adapter_options, options, adapter_instance)
|
||||
|
||||
# Should find something with read_multi now
|
||||
adapter_options = {}
|
||||
serializers.serializable_hash(adapter_options, options, adapter_instance)
|
||||
cached_attributes = adapter_options.fetch(:cached_attributes)
|
||||
|
||||
include_directive = ActiveModelSerializers.default_include_directive
|
||||
manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive)
|
||||
|
||||
refute_equal 0, cached_attributes.size
|
||||
refute_equal 0, manual_cached_attributes.size
|
||||
assert_equal manual_cached_attributes, cached_attributes
|
||||
end
|
||||
end
|
||||
|
||||
def test_serializer_file_path_on_nix
|
||||
path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
|
||||
caller_line = "#{path}:1:in `<top (required)>'"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user