mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Make bencharked model names reflect relationships; fix expected response
This commit is contained in:
parent
3d9ab37ffa
commit
af368af53e
@ -66,25 +66,25 @@ class ApiAssertion
|
||||
def expected
|
||||
@expected ||=
|
||||
{
|
||||
'post' => {
|
||||
'id' => 1337,
|
||||
'title' => 'New Post',
|
||||
'primary_resource' => {
|
||||
'id' => 1337,
|
||||
'title' => 'New PrimaryResource',
|
||||
'body' => 'Body',
|
||||
'comments' => [
|
||||
{
|
||||
'id' => 1,
|
||||
'body' => 'ZOMG A COMMENT'
|
||||
}
|
||||
],
|
||||
'blog' => {
|
||||
'id' => 999,
|
||||
'name' => 'Custom blog'
|
||||
'virtual_attribute' => {
|
||||
'id' => 999,
|
||||
'name' => 'Free-Range Virtual Attribute'
|
||||
},
|
||||
'author' => {
|
||||
'has_one_relationship' => {
|
||||
'id' => 42,
|
||||
'first_name' => 'Joao',
|
||||
'last_name' => 'Moura'
|
||||
}
|
||||
},
|
||||
'has_many_relationships' => [
|
||||
{
|
||||
'id' => 1,
|
||||
'body' => 'ZOMG A HAS MANY RELATIONSHIP'
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
@ -4,12 +4,23 @@ require_relative './app'
|
||||
time = 10
|
||||
disable_gc = true
|
||||
ActiveModelSerializers.config.key_transform = :unaltered
|
||||
comments = (0..50).map do |i|
|
||||
Comment.new(id: i, body: 'ZOMG A COMMENT')
|
||||
has_many_relationships = (0..50).map do |i|
|
||||
HasManyRelationship.new(id: i, body: 'ZOMG A HAS MANY RELATIONSHIP')
|
||||
end
|
||||
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
|
||||
post = Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
|
||||
serializer = PostSerializer.new(post)
|
||||
has_one_relationship = HasOneRelationship.new(
|
||||
id: 42,
|
||||
first_name: 'Joao',
|
||||
last_name: 'Moura'
|
||||
)
|
||||
primary_resource = PrimaryResource.new(
|
||||
id: 1337,
|
||||
title: 'New PrimaryResource',
|
||||
virtual_attribute: nil,
|
||||
body: 'Body',
|
||||
has_many_relationships: has_many_relationships,
|
||||
has_one_relationship: has_one_relationship
|
||||
)
|
||||
serializer = PrimaryResourceSerializer.new(primary_resource)
|
||||
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
|
||||
serialization = adapter.as_json
|
||||
|
||||
|
||||
@ -1,31 +1,30 @@
|
||||
class PostController < ActionController::Base
|
||||
POST =
|
||||
class PrimaryResourceController < ActionController::Base
|
||||
PRIMARY_RESOURCE =
|
||||
begin
|
||||
updated_at = Time.current
|
||||
if ENV['BENCH_STRESS']
|
||||
comments = (0..50).map do |i|
|
||||
Comment.new(id: i, body: 'ZOMG A COMMENT', updated_at: updated_at + i)
|
||||
has_many_relationships = (0..50).map do |i|
|
||||
HasManyRelationship.new(id: i, body: 'ZOMG A HAS MANY RELATIONSHIP')
|
||||
end
|
||||
else
|
||||
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT', updated_at: updated_at)]
|
||||
has_many_relationships = [HasManyRelationship.new(id: 1, body: 'ZOMG A HAS MANY RELATIONSHIP')]
|
||||
end
|
||||
author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
|
||||
Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
|
||||
has_one_relationship = HasOneRelationship.new(id: 42, first_name: 'Joao', last_name: 'Moura')
|
||||
PrimaryResource.new(id: 1337, title: 'New PrimaryResource', virtual_attribute: nil, body: 'Body', has_many_relationships: has_many_relationships, has_one_relationship: has_one_relationship)
|
||||
end
|
||||
|
||||
def render_with_caching_serializer
|
||||
toggle_cache_status
|
||||
render json: POST, serializer: CachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
|
||||
render json: PRIMARY_RESOURCE, serializer: CachingPrimaryResourceSerializer, adapter: :json, meta: { caching: perform_caching }
|
||||
end
|
||||
|
||||
def render_with_fragment_caching_serializer
|
||||
toggle_cache_status
|
||||
render json: POST, serializer: FragmentCachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
|
||||
render json: PRIMARY_RESOURCE, serializer: FragmentCachingPrimaryResourceSerializer, adapter: :json, meta: { caching: perform_caching }
|
||||
end
|
||||
|
||||
def render_with_non_caching_serializer
|
||||
toggle_cache_status
|
||||
render json: POST, adapter: :json, meta: { caching: perform_caching }
|
||||
render json: PRIMARY_RESOURCE, adapter: :json, meta: { caching: perform_caching }
|
||||
end
|
||||
|
||||
def render_cache_status
|
||||
@ -33,7 +32,7 @@ class PostController < ActionController::Base
|
||||
# Uncomment to debug
|
||||
# STDERR.puts cache_store.class
|
||||
# STDERR.puts cache_dependencies
|
||||
# ActiveSupport::Cache::Store.logger.debug [ActiveModelSerializers.config.cache_store, ActiveModelSerializers.config.perform_caching, CachingPostSerializer._cache, perform_caching, params].inspect
|
||||
# ActiveSupport::Cache::Store.logger.debug [ActiveModelSerializers.config.cache_store, ActiveModelSerializers.config.perform_caching, CachingPrimaryResourceSerializer._cache, perform_caching, params].inspect
|
||||
render json: { caching: perform_caching, meta: { cache_log: cache_messages, cache_status: cache_status } }.to_json
|
||||
end
|
||||
|
||||
@ -76,9 +75,9 @@ class PostController < ActionController::Base
|
||||
end
|
||||
|
||||
Rails.application.routes.draw do
|
||||
get '/status(/:on)' => 'post#render_cache_status'
|
||||
get '/clear' => 'post#clear'
|
||||
get '/caching(/:on)' => 'post#render_with_caching_serializer'
|
||||
get '/fragment_caching(/:on)' => 'post#render_with_fragment_caching_serializer'
|
||||
get '/non_caching(/:on)' => 'post#render_with_non_caching_serializer'
|
||||
get '/status(/:on)' => 'primary_resource#render_cache_status'
|
||||
get '/clear' => 'primary_resource#clear'
|
||||
get '/caching(/:on)' => 'primary_resource#render_with_caching_serializer'
|
||||
get '/fragment_caching(/:on)' => 'primary_resource#render_with_fragment_caching_serializer'
|
||||
get '/non_caching(/:on)' => 'primary_resource#render_with_non_caching_serializer'
|
||||
end
|
||||
|
||||
@ -1,33 +1,33 @@
|
||||
Rails.configuration.serializers = []
|
||||
class AuthorSerializer < ActiveModel::Serializer
|
||||
class HasOneRelationshipSerializer < ActiveModel::Serializer
|
||||
attributes :id, :first_name, :last_name
|
||||
|
||||
has_many :posts, embed: :ids
|
||||
has_many :primary_resources, embed: :ids
|
||||
has_one :bio
|
||||
end
|
||||
Rails.configuration.serializers << AuthorSerializer
|
||||
Rails.configuration.serializers << HasOneRelationshipSerializer
|
||||
|
||||
class BlogSerializer < ActiveModel::Serializer
|
||||
class VirtualAttributeSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name
|
||||
end
|
||||
Rails.configuration.serializers << BlogSerializer
|
||||
Rails.configuration.serializers << VirtualAttributeSerializer
|
||||
|
||||
class CommentSerializer < ActiveModel::Serializer
|
||||
attributes :id, :body, :updated_at
|
||||
class HasManyRelationshipSerializer < ActiveModel::Serializer
|
||||
attributes :id, :body
|
||||
|
||||
belongs_to :post
|
||||
belongs_to :author
|
||||
belongs_to :primary_resource
|
||||
belongs_to :has_one_relationship
|
||||
end
|
||||
Rails.configuration.serializers << CommentSerializer
|
||||
Rails.configuration.serializers << HasManyRelationshipSerializer
|
||||
|
||||
class PostSerializer < ActiveModel::Serializer
|
||||
class PrimaryResourceSerializer < ActiveModel::Serializer
|
||||
attributes :id, :title, :body
|
||||
|
||||
has_many :comments, serializer: CommentSerializer
|
||||
belongs_to :blog, serializer: BlogSerializer
|
||||
belongs_to :author, serializer: AuthorSerializer
|
||||
has_many :has_many_relationships, serializer: HasManyRelationshipSerializer
|
||||
belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
|
||||
belongs_to :has_one_relationship, serializer: HasOneRelationshipSerializer
|
||||
|
||||
link(:post_authors) { 'https://example.com/post_authors' }
|
||||
link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
|
||||
|
||||
meta do
|
||||
{
|
||||
@ -36,33 +36,33 @@ class PostSerializer < ActiveModel::Serializer
|
||||
}
|
||||
end
|
||||
|
||||
def blog
|
||||
Blog.new(id: 999, name: 'Custom blog')
|
||||
def virtual_attribute
|
||||
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
|
||||
end
|
||||
end
|
||||
Rails.configuration.serializers << PostSerializer
|
||||
Rails.configuration.serializers << PrimaryResourceSerializer
|
||||
|
||||
class CachingAuthorSerializer < AuthorSerializer
|
||||
class CachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
|
||||
cache key: 'writer', skip_digest: true
|
||||
end
|
||||
Rails.configuration.serializers << CachingAuthorSerializer
|
||||
Rails.configuration.serializers << CachingHasOneRelationshipSerializer
|
||||
|
||||
class CachingCommentSerializer < CommentSerializer
|
||||
class CachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
|
||||
cache expires_in: 1.day, skip_digest: true
|
||||
end
|
||||
Rails.configuration.serializers << CachingCommentSerializer
|
||||
Rails.configuration.serializers << CachingHasManyRelationshipSerializer
|
||||
|
||||
# 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
|
||||
class CachingPrimaryResourceSerializer < ActiveModel::Serializer
|
||||
cache key: 'primary_resource', 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
|
||||
belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
|
||||
belongs_to :has_one_relationship, serializer: CachingHasOneRelationshipSerializer
|
||||
has_many :has_many_relationships, serializer: CachingHasManyRelationshipSerializer
|
||||
|
||||
link(:post_authors) { 'https://example.com/post_authors' }
|
||||
link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
|
||||
|
||||
meta do
|
||||
{
|
||||
@ -71,33 +71,33 @@ class CachingPostSerializer < ActiveModel::Serializer
|
||||
}
|
||||
end
|
||||
|
||||
def blog
|
||||
Blog.new(id: 999, name: 'Custom blog')
|
||||
def virtual_attribute
|
||||
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
|
||||
end
|
||||
end
|
||||
Rails.configuration.serializers << CachingPostSerializer
|
||||
Rails.configuration.serializers << CachingPrimaryResourceSerializer
|
||||
|
||||
class FragmentCachingAuthorSerializer < AuthorSerializer
|
||||
class FragmentCachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
|
||||
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
|
||||
end
|
||||
Rails.configuration.serializers << FragmentCachingAuthorSerializer
|
||||
Rails.configuration.serializers << FragmentCachingHasOneRelationshipSerializer
|
||||
|
||||
class FragmentCachingCommentSerializer < CommentSerializer
|
||||
cache expires_in: 1.day, except: [:updated_at], skip_digest: true
|
||||
class FragmentCachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
|
||||
cache expires_in: 1.day, except: [:body], skip_digest: true
|
||||
end
|
||||
Rails.configuration.serializers << CachingCommentSerializer
|
||||
Rails.configuration.serializers << CachingHasManyRelationshipSerializer
|
||||
|
||||
# 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
|
||||
class FragmentCachingPrimaryResourceSerializer < ActiveModel::Serializer
|
||||
cache key: 'primary_resource', 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
|
||||
belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
|
||||
belongs_to :has_one_relationship, serializer: FragmentCachingHasOneRelationshipSerializer
|
||||
has_many :has_many_relationships, serializer: FragmentCachingHasManyRelationshipSerializer
|
||||
|
||||
link(:post_authors) { 'https://example.com/post_authors' }
|
||||
link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
|
||||
|
||||
meta do
|
||||
{
|
||||
@ -106,11 +106,11 @@ class FragmentCachingPostSerializer < ActiveModel::Serializer
|
||||
}
|
||||
end
|
||||
|
||||
def blog
|
||||
Blog.new(id: 999, name: 'Custom blog')
|
||||
def virtual_attribute
|
||||
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
|
||||
end
|
||||
end
|
||||
Rails.configuration.serializers << FragmentCachingPostSerializer
|
||||
Rails.configuration.serializers << FragmentCachingPrimaryResourceSerializer
|
||||
|
||||
if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
|
||||
require 'active_record'
|
||||
@ -119,48 +119,48 @@ if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
|
||||
ActiveRecord::Schema.define do
|
||||
self.verbose = false
|
||||
|
||||
create_table :blogs, force: true do |t|
|
||||
create_table :virtual_attributes, force: true do |t|
|
||||
t.string :name
|
||||
t.timestamps null: false
|
||||
end
|
||||
create_table :authors, force: true do |t|
|
||||
create_table :has_one_relationships, force: true do |t|
|
||||
t.string :first_name
|
||||
t.string :last_name
|
||||
t.timestamps null: false
|
||||
end
|
||||
create_table :posts, force: true do |t|
|
||||
create_table :primary_resources, force: true do |t|
|
||||
t.string :title
|
||||
t.text :body
|
||||
t.references :author
|
||||
t.references :blog
|
||||
t.references :has_one_relationship
|
||||
t.references :virtual_attribute
|
||||
t.timestamps null: false
|
||||
end
|
||||
create_table :comments, force: true do |t|
|
||||
create_table :has_many_relationships, force: true do |t|
|
||||
t.text :body
|
||||
t.references :author
|
||||
t.references :post
|
||||
t.references :has_one_relationship
|
||||
t.references :primary_resource
|
||||
t.timestamps null: false
|
||||
end
|
||||
end
|
||||
|
||||
class Comment < ActiveRecord::Base
|
||||
belongs_to :author
|
||||
belongs_to :post
|
||||
class HasManyRelationship < ActiveRecord::Base
|
||||
belongs_to :has_one_relationship
|
||||
belongs_to :primary_resource
|
||||
end
|
||||
|
||||
class Author < ActiveRecord::Base
|
||||
has_many :posts
|
||||
has_many :comments
|
||||
class HasOneRelationship < ActiveRecord::Base
|
||||
has_many :primary_resources
|
||||
has_many :has_many_relationships
|
||||
end
|
||||
|
||||
class Post < ActiveRecord::Base
|
||||
has_many :comments
|
||||
belongs_to :author
|
||||
belongs_to :blog
|
||||
class PrimaryResource < ActiveRecord::Base
|
||||
has_many :has_many_relationships
|
||||
belongs_to :has_one_relationship
|
||||
belongs_to :virtual_attribute
|
||||
end
|
||||
|
||||
class Blog < ActiveRecord::Base
|
||||
has_many :posts
|
||||
class VirtualAttribute < ActiveRecord::Base
|
||||
has_many :primary_resources
|
||||
end
|
||||
else
|
||||
# ActiveModelSerializers::Model is a convenient
|
||||
@ -201,19 +201,19 @@ else
|
||||
end
|
||||
end
|
||||
|
||||
class Comment < BenchmarkModel
|
||||
attr_accessor :id, :body, :updated_at
|
||||
class HasManyRelationship < BenchmarkModel
|
||||
attr_accessor :id, :body
|
||||
end
|
||||
|
||||
class Author < BenchmarkModel
|
||||
attr_accessor :id, :first_name, :last_name, :posts
|
||||
class HasOneRelationship < BenchmarkModel
|
||||
attr_accessor :id, :first_name, :last_name, :primary_resources
|
||||
end
|
||||
|
||||
class Post < BenchmarkModel
|
||||
attr_accessor :id, :title, :body, :comments, :blog, :author
|
||||
class PrimaryResource < BenchmarkModel
|
||||
attr_accessor :id, :title, :body, :has_many_relationships, :virtual_attribute, :has_one_relationship
|
||||
end
|
||||
|
||||
class Blog < BenchmarkModel
|
||||
class VirtualAttribute < BenchmarkModel
|
||||
attr_accessor :id, :name
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user