mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
It's a new implementation of cache based on ActiveSupport::Cache.
The implementation abstracts the cache in Adapter class on a
private method called cached_object, this method is intended
to be used on Adapters inside serializable_hash method in order
to cache each instance of the object that will be returned by
the serializer.
Some of its features are:
- A different syntax. (no longer need the cache_key method).
- An options argument that have the same arguments of ActiveSupport::Cache::Store, plus a key option that will be the prefix of the object cache on a pattern "#{key}-#{object.id}".
- It cache the objects individually and not the whole Serializer return, re-using it in different requests (as a show and a index method for example.)
57 lines
1.6 KiB
Ruby
57 lines
1.6 KiB
Ruby
require 'test_helper'
|
|
|
|
module ActiveModel
|
|
class Serializer
|
|
class Adapter
|
|
class Json
|
|
class Collection < Minitest::Test
|
|
def setup
|
|
@author = Author.new(id: 1, name: 'Steve K.')
|
|
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
|
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
|
@first_post.comments = []
|
|
@second_post.comments = []
|
|
@first_post.author = @author
|
|
@second_post.author = @author
|
|
@blog = Blog.new(id: 1, name: "My Blog!!")
|
|
@first_post.blog = @blog
|
|
@second_post.blog = nil
|
|
|
|
@serializer = ArraySerializer.new([@first_post, @second_post])
|
|
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
|
ActionController::Base.cache_store.clear
|
|
end
|
|
|
|
def test_include_multiple_posts
|
|
expected = [{
|
|
title: "Hello!!",
|
|
body: "Hello, world!!",
|
|
id: 1,
|
|
comments: [],
|
|
author: {
|
|
id: 1,
|
|
name: "Steve K."
|
|
},
|
|
blog: {
|
|
id: 999,
|
|
name: "Custom blog"
|
|
}
|
|
}, {
|
|
title: "New Post",
|
|
body: "Body",
|
|
id: 2,
|
|
comments: [],
|
|
author: {
|
|
id: 1,
|
|
name: "Steve K."
|
|
},
|
|
blog: nil
|
|
}]
|
|
assert_equal expected, @adapter.serializable_hash
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|