mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1765 from bf4/serializer_cleanup_3
Remove unnecessary Serializer#cached_fields; Extracted cached_attributes
This commit is contained in:
commit
f2f747edda
@ -1,3 +1,5 @@
|
||||
# TODO(BF): refactor file to be smaller
|
||||
# rubocop:disable Metrics/ModuleLength
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
UndefinedCacheKey = Class.new(StandardError)
|
||||
@ -206,14 +208,20 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
# Get attributes from @cached_attributes
|
||||
# @return [Hash] cached attributes
|
||||
# def cached_attributes(fields, adapter_instance)
|
||||
def cached_fields(fields, adapter_instance)
|
||||
def cached_attributes(fields, 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(fields)
|
||||
end
|
||||
end
|
||||
else
|
||||
cache_check(adapter_instance) do
|
||||
attributes(fields)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def cache_check(adapter_instance)
|
||||
if self.class.cache_enabled?
|
||||
@ -331,3 +339,4 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
end
|
||||
# rubocop:enable Metrics/ModuleLength
|
||||
|
||||
@ -32,7 +32,8 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def serializable_hash_for_single_resource(options)
|
||||
resource = resource_object_for(options)
|
||||
cached_attributes = instance_options[:cached_attributes] || {}
|
||||
resource = serializer.cached_attributes(options[:fields], cached_attributes, self)
|
||||
relationships = resource_relationships(options)
|
||||
resource.merge(relationships)
|
||||
end
|
||||
@ -60,18 +61,6 @@ module ActiveModelSerializers
|
||||
|
||||
relationship_value
|
||||
end
|
||||
|
||||
def resource_object_for(options)
|
||||
if serializer.class.cache_enabled?
|
||||
cached_attributes = instance_options[:cached_attributes] || {}
|
||||
key = serializer.cache_key(self)
|
||||
cached_attributes.fetch(key) do
|
||||
serializer.cached_fields(options[:fields], self)
|
||||
end
|
||||
else
|
||||
serializer.cached_fields(options[:fields], self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user