Adding Fragment Cache to AMS

It's an upgrade based on the new Cache implementation #693.
It allows to use the Rails conventions to cache
specific attributes or associations.
It's based on the Cache Composition implementation.
This commit is contained in:
João Moura
2015-02-03 21:57:02 -02:00
parent 48ed7cf9ba
commit 792fb8a905
19 changed files with 554 additions and 101 deletions

View File

@@ -0,0 +1,27 @@
require 'test_helper'
module ActiveModel
class Serializer
class Adapter
class FragmentCacheTest < Minitest::Test
def setup
@author = Author.new(name: 'Joao M. D. Moura')
@role = Role.new(name: 'Great Author', description:nil)
@role.author = [@author]
@role_serializer = RoleSerializer.new(@role)
@role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {}, nil)
end
def test_fragment_fetch_with_virtual_attributes
expected_result = {
id: @role.id,
description: @role.description,
slug: "#{@role.name}-#{@role.id}",
name: @role.name
}
assert_equal(@role_hash.fetch, expected_result)
end
end
end
end
end

View File

@@ -6,6 +6,7 @@ module ActiveModel
class Json
class HasManyTestTest < Minitest::Test
def setup
ActionController::Base.cache_store.clear
@author = Author.new(id: 1, name: 'Steve K.')
@post = Post.new(title: 'New Post', body: 'Body')
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')

View File

@@ -41,6 +41,7 @@ module ActiveModel
expected = [
{
id: "43",
rating: nil,
type: "bios",
content:"AMS Contributor",
links: {

View File

@@ -1,11 +1,11 @@
require 'test_helper'
module ActiveModel
class Serializer
class Adapter
class JsonApi
class LinkedTest < Minitest::Test
def setup
ActionController::Base.cache_store.clear
@author1 = Author.new(id: 1, name: 'Steve K.')
@author2 = Author.new(id: 2, name: 'Tenderlove')
@bio1 = Bio.new(id: 1, content: 'AMS Contributor')
@@ -103,8 +103,9 @@ module ActiveModel
}
}, {
id: "1",
content: "AMS Contributor",
rating: nil,
type: "bios",
content: "AMS Contributor",
links: {
author: { linkage: { type: "authors", id: "1" } }
}
@@ -119,8 +120,9 @@ module ActiveModel
}
}, {
id: "2",
content: "Rails Contributor",
rating: nil,
type: "bios",
content: "Rails Contributor",
links: {
author: { linkage: { type: "authors", id: "2" } }
}

View File

@@ -5,6 +5,7 @@ module ActiveModel
class Adapter
class JsonTest < Minitest::Test
def setup
ActionController::Base.cache_store.clear
@author = Author.new(id: 1, name: 'Steve K.')
@post = Post.new(title: 'New Post', body: 'Body')
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')