Skip to content

Commit fcfbc0d

Browse files
committed
Merge pull request #1764 from bf4/serializer_cleanup_2
Remove unnecessary Adapter::Base#cache_attributes
2 parents 3f38a60 + ee518e1 commit fcfbc0d

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

lib/active_model_serializers/adapter/attributes.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module Adapter
33
class Attributes < Base
44
def initialize(serializer, options = {})
55
super
6-
@cached_attributes = options[:cache_attributes] || {}
76
@include_directive =
87
if options[:include_directive]
98
options[:include_directive]
@@ -27,7 +26,7 @@ def serializable_hash(options = nil)
2726
private
2827

2928
def serializable_hash_for_collection(options)
30-
cache_attributes
29+
instance_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(serializer, self, @include_directive)
3130
opts = instance_options.merge(include_directive: @include_directive)
3231
serializer.map { |s| Attributes.new(s, opts).serializable_hash(options) }
3332
end
@@ -62,16 +61,11 @@ def relationship_value_for(association, options)
6261
relationship_value
6362
end
6463

65-
# Set @cached_attributes
66-
def cache_attributes
67-
return if @cached_attributes.present?
68-
69-
@cached_attributes = ActiveModel::Serializer.cache_read_multi(serializer, self, @include_directive)
70-
end
71-
7264
def resource_object_for(options)
7365
if serializer.class.cache_enabled?
74-
@cached_attributes.fetch(serializer.cache_key(self)) do
66+
cached_attributes = instance_options[:cached_attributes] || {}
67+
key = serializer.cache_key(self)
68+
cached_attributes.fetch(key) do
7569
serializer.cached_fields(options[:fields], self)
7670
end
7771
else

test/cache_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ def test_cached_attributes
303303
render_object_with_cache(@comment)
304304

305305
attributes = Adapter::Attributes.new(serializer)
306-
attributes.send(:cache_attributes)
307-
cached_attributes = attributes.instance_variable_get(:@cached_attributes)
306+
include_directive = ActiveModelSerializers.default_include_directive
307+
cached_attributes = ActiveModel::Serializer.cache_read_multi(serializer, attributes, include_directive)
308308

309309
assert_equal cached_attributes["#{@comment.cache_key}/#{attributes.cached_name}"], Comment.new(id: 1, body: 'ZOMG A COMMENT').attributes
310310
assert_equal cached_attributes["#{@comment.post.cache_key}/#{attributes.cached_name}"], Post.new(id: 'post', title: 'New Post', body: 'Body').attributes

0 commit comments

Comments
 (0)