Merge pull request #1765 from bf4/serializer_cleanup_3

Remove unnecessary Serializer#cached_fields; Extracted cached_attributes
This commit is contained in:
Benjamin Fleischer 2016-06-01 01:41:42 -05:00
commit f2f747edda
2 changed files with 17 additions and 19 deletions

View File

@ -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,12 +208,18 @@ module ActiveModel
end end
end end
# Get attributes from @cached_attributes def cached_attributes(fields, cached_attributes, adapter_instance)
# @return [Hash] cached attributes if self.class.cache_enabled?
# def cached_attributes(fields, adapter_instance) key = cache_key(adapter_instance)
def cached_fields(fields, adapter_instance) cached_attributes.fetch(key) do
cache_check(adapter_instance) do cache_check(adapter_instance) do
attributes(fields) attributes(fields)
end
end
else
cache_check(adapter_instance) do
attributes(fields)
end
end end
end end
@ -331,3 +339,4 @@ module ActiveModel
end end
end end
end end
# rubocop:enable Metrics/ModuleLength

View File

@ -32,7 +32,8 @@ module ActiveModelSerializers
end end
def serializable_hash_for_single_resource(options) 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) relationships = resource_relationships(options)
resource.merge(relationships) resource.merge(relationships)
end end
@ -60,18 +61,6 @@ module ActiveModelSerializers
relationship_value relationship_value
end 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 end
end end