From eb8666339390dcf1608b2b384451739eb0b7866b Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 14 Apr 2016 21:59:18 -0500 Subject: [PATCH 1/4] Remove unnecessary Adapter::Base#resource_object_for --- .../adapter/attributes.rb | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/active_model_serializers/adapter/attributes.rb b/lib/active_model_serializers/adapter/attributes.rb index 6c903afe..3b6acab6 100644 --- a/lib/active_model_serializers/adapter/attributes.rb +++ b/lib/active_model_serializers/adapter/attributes.rb @@ -32,7 +32,16 @@ module ActiveModelSerializers end def serializable_hash_for_single_resource(options) - resource = resource_object_for(options) + resource = + 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 relationships = resource_relationships(options) resource.merge(relationships) end @@ -60,18 +69,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 From 96750b2f9a633460780597f1c96ca6f1ab2b2e8f Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 14 Apr 2016 22:01:56 -0500 Subject: [PATCH 2/4] Remove unnecessary Serializer#cached_fields --- lib/active_model/serializer/caching.rb | 9 --------- lib/active_model_serializers/adapter/attributes.rb | 8 ++++++-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/active_model/serializer/caching.rb b/lib/active_model/serializer/caching.rb index 850c346b..6444f9d9 100644 --- a/lib/active_model/serializer/caching.rb +++ b/lib/active_model/serializer/caching.rb @@ -206,15 +206,6 @@ 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) - cache_check(adapter_instance) do - attributes(fields) - end - end - def cache_check(adapter_instance) if self.class.cache_enabled? self.class.cache_store.fetch(cache_key(adapter_instance), self.class._cache_options) do diff --git a/lib/active_model_serializers/adapter/attributes.rb b/lib/active_model_serializers/adapter/attributes.rb index 3b6acab6..ba7caa37 100644 --- a/lib/active_model_serializers/adapter/attributes.rb +++ b/lib/active_model_serializers/adapter/attributes.rb @@ -37,10 +37,14 @@ module ActiveModelSerializers cached_attributes = instance_options[:cached_attributes] || {} key = serializer.cache_key(self) cached_attributes.fetch(key) do - serializer.cached_fields(options[:fields], self) + serializer.cache_check(self) do + serializer.attributes(options[:fields]) + end end else - serializer.cached_fields(options[:fields], self) + serializer.cache_check(self) do + serializer.attributes(options[:fields]) + end end relationships = resource_relationships(options) resource.merge(relationships) From ba23de686de741539b4d8ec0ad964078132da963 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 14 Apr 2016 22:24:30 -0500 Subject: [PATCH 3/4] Complete extracting to Serializer#cached_attributes --- lib/active_model/serializer/caching.rb | 18 ++++++++++++++++++ .../adapter/attributes.rb | 16 ++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/active_model/serializer/caching.rb b/lib/active_model/serializer/caching.rb index 6444f9d9..f7f2904b 100644 --- a/lib/active_model/serializer/caching.rb +++ b/lib/active_model/serializer/caching.rb @@ -1,3 +1,5 @@ +# TODO(BF): refactor file to be smaller +# rubocop:disable Metrics/ModuleLength module ActiveModel class Serializer UndefinedCacheKey = Class.new(StandardError) @@ -206,6 +208,21 @@ module ActiveModel 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) if self.class.cache_enabled? self.class.cache_store.fetch(cache_key(adapter_instance), self.class._cache_options) do @@ -322,3 +339,4 @@ module ActiveModel end end end +# rubocop:enable Metrics/ModuleLength diff --git a/lib/active_model_serializers/adapter/attributes.rb b/lib/active_model_serializers/adapter/attributes.rb index ba7caa37..abcc5cba 100644 --- a/lib/active_model_serializers/adapter/attributes.rb +++ b/lib/active_model_serializers/adapter/attributes.rb @@ -32,20 +32,8 @@ module ActiveModelSerializers end def serializable_hash_for_single_resource(options) - resource = - if serializer.class.cache_enabled? - 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 + cached_attributes = instance_options[:cached_attributes] || {} + resource = serializer.cached_attributes(options, cached_attributes, self) relationships = resource_relationships(options) resource.merge(relationships) end From 385abb4ba03764ad24e36a4eb55ef542766e980e Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Thu, 14 Apr 2016 22:53:06 -0500 Subject: [PATCH 4/4] Simplify Serializer#cached_attributes to take a fields argument --- lib/active_model/serializer/caching.rb | 6 +++--- lib/active_model_serializers/adapter/attributes.rb | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/active_model/serializer/caching.rb b/lib/active_model/serializer/caching.rb index f7f2904b..0f1fffb2 100644 --- a/lib/active_model/serializer/caching.rb +++ b/lib/active_model/serializer/caching.rb @@ -208,17 +208,17 @@ module ActiveModel end end - def cached_attributes(options, cached_attributes, 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(options[:fields]) + attributes(fields) end end else cache_check(adapter_instance) do - attributes(options[:fields]) + attributes(fields) end end end diff --git a/lib/active_model_serializers/adapter/attributes.rb b/lib/active_model_serializers/adapter/attributes.rb index abcc5cba..56a155c3 100644 --- a/lib/active_model_serializers/adapter/attributes.rb +++ b/lib/active_model_serializers/adapter/attributes.rb @@ -33,7 +33,7 @@ module ActiveModelSerializers def serializable_hash_for_single_resource(options) cached_attributes = instance_options[:cached_attributes] || {} - resource = serializer.cached_attributes(options, cached_attributes, self) + resource = serializer.cached_attributes(options[:fields], cached_attributes, self) relationships = resource_relationships(options) resource.merge(relationships) end