mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Complete extracting to Serializer#cached_attributes
This commit is contained in:
parent
96750b2f9a
commit
ba23de686d
@ -1,3 +1,5 @@
|
|||||||
|
# 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)
|
||||||
@ -206,6 +208,21 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cached_attributes(options, cached_attributes, adapter_instance)
|
||||||
|
if self.class.cache_enabled?
|
||||||
|
key = cache_key(adapter_instance)
|
||||||
|
cached_attributes.fetch(key) do
|
||||||
|
cache_check(adapter_instance) do
|
||||||
|
attributes(options[:fields])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
cache_check(adapter_instance) do
|
||||||
|
attributes(options[:fields])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def cache_check(adapter_instance)
|
def cache_check(adapter_instance)
|
||||||
if self.class.cache_enabled?
|
if self.class.cache_enabled?
|
||||||
self.class.cache_store.fetch(cache_key(adapter_instance), self.class._cache_options) do
|
self.class.cache_store.fetch(cache_key(adapter_instance), self.class._cache_options) do
|
||||||
@ -322,3 +339,4 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
# rubocop:enable Metrics/ModuleLength
|
||||||
|
|||||||
@ -32,20 +32,8 @@ module ActiveModelSerializers
|
|||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash_for_single_resource(options)
|
def serializable_hash_for_single_resource(options)
|
||||||
resource =
|
cached_attributes = instance_options[:cached_attributes] || {}
|
||||||
if serializer.class.cache_enabled?
|
resource = serializer.cached_attributes(options, cached_attributes, self)
|
||||||
cached_attributes = instance_options[:cached_attributes] || {}
|
|
||||||
key = serializer.cache_key(self)
|
|
||||||
cached_attributes.fetch(key) do
|
|
||||||
serializer.cache_check(self) do
|
|
||||||
serializer.attributes(options[:fields])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
serializer.cache_check(self) do
|
|
||||||
serializer.attributes(options[:fields])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
relationships = resource_relationships(options)
|
relationships = resource_relationships(options)
|
||||||
resource.merge(relationships)
|
resource.merge(relationships)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user