test/benchmark/fixtures.rb in active_model_serializers-0.10.0.rc5 vs test/benchmark/fixtures.rb in active_model_serializers-0.10.0

- old
+ new

@@ -11,11 +11,11 @@ attributes :id, :name end Rails.configuration.serializers << BlogSerializer class CommentSerializer < ActiveModel::Serializer - attributes :id, :body + attributes :id, :body, :updated_at belongs_to :post belongs_to :author end Rails.configuration.serializers << CommentSerializer @@ -41,27 +41,79 @@ end end Rails.configuration.serializers << PostSerializer class CachingAuthorSerializer < AuthorSerializer - cache key: 'writer', only: [:first_name, :last_name], skip_digest: true + cache key: 'writer', skip_digest: true end Rails.configuration.serializers << CachingAuthorSerializer class CachingCommentSerializer < CommentSerializer cache expires_in: 1.day, skip_digest: true end Rails.configuration.serializers << CachingCommentSerializer -class CachingPostSerializer < PostSerializer +# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532 +class CachingPostSerializer < ActiveModel::Serializer cache key: 'post', expires_in: 0.1, skip_digest: true + + attributes :id, :title, :body + belongs_to :blog, serializer: BlogSerializer belongs_to :author, serializer: CachingAuthorSerializer has_many :comments, serializer: CachingCommentSerializer + + link(:post_authors) { 'https://example.com/post_authors' } + + meta do + { + rating: 5, + favorite_count: 10 + } + end + + def blog + Blog.new(id: 999, name: 'Custom blog') + end end Rails.configuration.serializers << CachingPostSerializer +class FragmentCachingAuthorSerializer < AuthorSerializer + cache key: 'writer', only: [:first_name, :last_name], skip_digest: true +end +Rails.configuration.serializers << FragmentCachingAuthorSerializer + +class FragmentCachingCommentSerializer < CommentSerializer + cache expires_in: 1.day, except: [:updated_at], skip_digest: true +end +Rails.configuration.serializers << CachingCommentSerializer + +# see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532 +class FragmentCachingPostSerializer < ActiveModel::Serializer + cache key: 'post', expires_in: 0.1, skip_digest: true + + attributes :id, :title, :body + + belongs_to :blog, serializer: BlogSerializer + belongs_to :author, serializer: FragmentCachingAuthorSerializer + has_many :comments, serializer: FragmentCachingCommentSerializer + + link(:post_authors) { 'https://example.com/post_authors' } + + meta do + { + rating: 5, + favorite_count: 10 + } + end + + def blog + Blog.new(id: 999, name: 'Custom blog') + end +end +Rails.configuration.serializers << FragmentCachingPostSerializer + if ENV['ENABLE_ACTIVE_RECORD'] == 'true' require 'active_record' ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') ActiveRecord::Schema.define do @@ -148,10 +200,10 @@ end end end class Comment < BenchmarkModel - attr_accessor :id, :body + attr_accessor :id, :body, :updated_at end class Author < BenchmarkModel attr_accessor :id, :first_name, :last_name, :posts end