Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheritance of serializer inheriting the cache configuration #1249

Merged
merged 1 commit into from
Oct 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ class Serializer
class << self
attr_accessor :_attributes
attr_accessor :_attributes_keys
attr_accessor :_cache
attr_accessor :_fragmented
attr_accessor :_cache_key
attr_accessor :_cache_only
attr_accessor :_cache_except
attr_accessor :_cache_options
attr_accessor :_cache_digest
end

with_options instance_writer: false, instance_reader: false do |serializer|
serializer.class_attribute :_cache
serializer.class_attribute :_fragmented
serializer.class_attribute :_cache_key
serializer.class_attribute :_cache_only
serializer.class_attribute :_cache_except
serializer.class_attribute :_cache_options
serializer.class_attribute :_cache_digest
end

def self.inherited(base)
Expand Down
16 changes: 16 additions & 0 deletions test/serializers/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ def setup
@blog_serializer = BlogSerializer.new(@blog)
end

def test_inherited_cache_configuration
inherited_serializer = Class.new(PostSerializer)

assert_equal PostSerializer._cache_key, inherited_serializer._cache_key
assert_equal PostSerializer._cache_options, inherited_serializer._cache_options
end

def test_override_cache_configuration
inherited_serializer = Class.new(PostSerializer) do
cache key: 'new-key'
end

assert PostSerializer._cache_key == 'post'
assert inherited_serializer._cache_key == 'new-key'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Please change to assert_equal and I think this is good to go.

Do you think there's any value in testing all the class_attributes? I think there would be

     serializer.class_attribute :_cache
      serializer.class_attribute :_fragmented
      serializer.class_attribute :_cache_key
      serializer.class_attribute :_cache_only
      serializer.class_attribute :_cache_except
      serializer.class_attribute :_cache_options
      serializer.class_attribute :_cache_digest

I have some similar changes in bf4@a60ba9d#diff-5624f9cf35be4dc254412d979fd293c4R31 that could go along with or follow this:

+    class_attribute :_attributes, instance_writer: false
+    self._attributes ||= []
+    class_attribute :_attributes_keys, instance_writer: false, instance_reader: false
+    self._attributes_keys ||= {}
+    class_attribute :_type, instance_writer: false
#
   def self.inherited(base)
+      base._attributes = _attributes.dup
+      base._attributes_keys = _attributes_keys.dup
#
    def attributes
+      attributes = _attributes.dup

      attributes.each_with_object({}) do |name, hash|
+        if _fragmented
+          hash[name] = _fragmented.public_send(name)
+        else
+          hash[name] = send(name)
        end
      end
    end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end

def test_cache_definition
assert_equal(ActionController::Base.cache_store, @post_serializer.class._cache)
assert_equal(ActionController::Base.cache_store, @author_serializer.class._cache)
Expand Down