44
55module ActiveModelSerializers
66 class CacheTest < ActiveSupport ::TestCase
7+ class Article < ::Model
8+ attributes :title
9+ # To confirm error is raised when cache_key is not set and cache_key option not passed to cache
10+ undef_method :cache_key
11+ end
12+ class ArticleSerializer < ActiveModel ::Serializer
13+ cache only : [ :place ] , skip_digest : true
14+ attributes :title
15+ end
16+
17+ class Author < ::Model
18+ attributes :id , :name
19+ associations :posts , :bio , :roles
20+ end
721 # Instead of a primitive cache key (i.e. a string), this class
822 # returns a list of objects that require to be expanded themselves.
923 class AuthorWithExpandableCacheElements < Author
@@ -27,45 +41,85 @@ def cache_key
2741 ]
2842 end
2943 end
30-
3144 class UncachedAuthor < Author
3245 # To confirm cache_key is set using updated_at and cache_key option passed to cache
3346 undef_method :cache_key
3447 end
48+ class AuthorSerializer < ActiveModel ::Serializer
49+ cache key : 'writer' , skip_digest : true
50+ attributes :id , :name
3551
36- class Article < ::Model
37- attributes :title
38- # To confirm error is raised when cache_key is not set and cache_key option not passed to cache
39- undef_method :cache_key
52+ has_many :posts
53+ has_many :roles
54+ has_one :bio
4055 end
4156
42- class ArticleSerializer < ActiveModel :: Serializer
43- cache only : [ :place ] , skip_digest : true
44- attributes :title
57+ class Blog < :: Model
58+ attributes :name
59+ associations :writer
4560 end
61+ class BlogSerializer < ActiveModel ::Serializer
62+ cache key : 'blog'
63+ attributes :id , :name
4664
47- class InheritedRoleSerializer < RoleSerializer
48- cache key : 'inherited_role' , only : [ :name , :special_attribute ]
49- attribute :special_attribute
65+ belongs_to :writer
5066 end
5167
5268 class Comment < ::Model
53- attributes :body
69+ attributes :id , : body
5470 associations :post , :author
5571
5672 # Uses a custom non-time-based cache key
5773 def cache_key
5874 "comment/#{ id } "
5975 end
6076 end
77+ class CommentSerializer < ActiveModel ::Serializer
78+ cache expires_in : 1 . day , skip_digest : true
79+ attributes :id , :body
80+ belongs_to :post
81+ belongs_to :author
82+ end
83+
84+ class Post < ::Model
85+ attributes :id , :title , :body
86+ associations :author , :comments , :blog
87+ end
88+ class PostSerializer < ActiveModel ::Serializer
89+ cache key : 'post' , expires_in : 0.1 , skip_digest : true
90+ attributes :id , :title , :body
91+
92+ has_many :comments
93+ belongs_to :blog
94+ belongs_to :author
95+ end
96+
97+ class Role < ::Model
98+ attributes :name , :description , :special_attribute
99+ associations :author
100+ end
101+ class RoleSerializer < ActiveModel ::Serializer
102+ cache only : [ :name , :slug ] , skip_digest : true
103+ attributes :id , :name , :description
104+ attribute :friendly_id , key : :slug
105+ belongs_to :author
106+
107+ def friendly_id
108+ "#{ object . name } -#{ object . id } "
109+ end
110+ end
111+ class InheritedRoleSerializer < RoleSerializer
112+ cache key : 'inherited_role' , only : [ :name , :special_attribute ]
113+ attribute :special_attribute
114+ end
61115
62116 setup do
63117 cache_store . clear
64118 @comment = Comment . new ( id : 1 , body : 'ZOMG A COMMENT' )
65- @post = Post . new ( title : 'New Post' , body : 'Body' )
119+ @post = Post . new ( id : 'post' , title : 'New Post' , body : 'Body' )
66120 @bio = Bio . new ( id : 1 , content : 'AMS Contributor' )
67- @author = Author . new ( name : 'Joao M. D. Moura' )
68- @blog = Blog . new ( id : 999 , name : 'Custom blog' , writer : @author , articles : [ ] )
121+ @author = Author . new ( id : 'author' , name : 'Joao M. D. Moura' )
122+ @blog = Blog . new ( id : 999 , name : 'Custom blog' , writer : @author )
69123 @role = Role . new ( name : 'Great Author' )
70124 @location = Location . new ( lat : '-23.550520' , lng : '-46.633309' )
71125 @place = Place . new ( name : 'Amazing Place' )
@@ -325,12 +379,14 @@ def test_a_serializer_rendered_by_two_adapter_returns_differently_fetch_attribut
325379
326380 def test_uses_file_digest_in_cache_key
327381 render_object_with_cache ( @blog )
328- key = "#{ @blog . cache_key } /#{ adapter . cache_key } /#{ ::Model ::FILE_DIGEST } "
382+ file_digest = Digest ::MD5 . hexdigest ( File . open ( __FILE__ ) . read )
383+ key = "#{ @blog . cache_key } /#{ adapter . cache_key } /#{ file_digest } "
329384 assert_equal ( @blog_serializer . attributes , cache_store . fetch ( key ) )
330385 end
331386
332387 def test_cache_digest_definition
333- assert_equal ( ::Model ::FILE_DIGEST , @post_serializer . class . _cache_digest )
388+ file_digest = Digest ::MD5 . hexdigest ( File . open ( __FILE__ ) . read )
389+ assert_equal ( file_digest , @post_serializer . class . _cache_digest )
334390 end
335391
336392 def test_object_cache_keys
@@ -532,7 +588,7 @@ def test_fragment_fetch_with_virtual_attributes
532588 role_hash = role_serializer . fetch_attributes_fragment ( adapter_instance )
533589 assert_equal ( role_hash , expected_result )
534590
535- role . attributes [ :id ] = 'this has been updated'
591+ role . id = 'this has been updated'
536592 role . name = 'this was cached'
537593
538594 role_hash = role_serializer . fetch_attributes_fragment ( adapter_instance )
0 commit comments