remove dynamic class creation where not needed (#1850)

* remove dynamic class creation where not needed
This commit is contained in:
L. Preston Sego III 2016-07-18 15:11:09 -04:00 committed by Benjamin Fleischer
parent 3ad2457aaf
commit aa4d89ab47
14 changed files with 90 additions and 85 deletions

View File

@ -5,7 +5,9 @@ module ActionController
class JsonApi class JsonApi
class KeyTransformTest < ActionController::TestCase class KeyTransformTest < ActionController::TestCase
class KeyTransformTestController < ActionController::Base class KeyTransformTestController < ActionController::Base
Post = Class.new(::Model) class Post < ::Model; end
class Author < ::Model; end
class TopComment < ::Model; end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
type 'posts' type 'posts'
attributes :title, :body, :publish_at attributes :title, :body, :publish_at
@ -22,13 +24,11 @@ module ActionController
end end
end end
Author = Class.new(::Model)
class AuthorSerializer < ActiveModel::Serializer class AuthorSerializer < ActiveModel::Serializer
type 'authors' type 'authors'
attributes :first_name, :last_name attributes :first_name, :last_name
end end
TopComment = Class.new(::Model)
class TopCommentSerializer < ActiveModel::Serializer class TopCommentSerializer < ActiveModel::Serializer
type 'top_comments' type 'top_comments'
attributes :body attributes :body

View File

@ -15,7 +15,7 @@ module ActiveModelSerializers
@adapter = ActiveModelSerializers::Adapter::Json.new(serializer, options) @adapter = ActiveModelSerializers::Adapter::Json.new(serializer, options)
end end
Post = Class.new(::Model) class Post < ::Model; end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body, :publish_at attributes :id, :title, :body, :publish_at
end end

View File

@ -4,7 +4,10 @@ module ActiveModelSerializers
module Adapter module Adapter
class JsonApi class JsonApi
class FieldsTest < ActiveSupport::TestCase class FieldsTest < ActiveSupport::TestCase
Post = Class.new(::Model) class Post < ::Model; end
class Author < ::Model; end
class Comment < ::Model; end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
type 'posts' type 'posts'
attributes :title, :body attributes :title, :body
@ -12,13 +15,11 @@ module ActiveModelSerializers
has_many :comments has_many :comments
end end
Author = Class.new(::Model)
class AuthorSerializer < ActiveModel::Serializer class AuthorSerializer < ActiveModel::Serializer
type 'authors' type 'authors'
attributes :name, :birthday attributes :name, :birthday
end end
Comment = Class.new(::Model)
class CommentSerializer < ActiveModel::Serializer class CommentSerializer < ActiveModel::Serializer
type 'comments' type 'comments'
attributes :body attributes :body

View File

@ -1,10 +1,9 @@
require 'test_helper' require 'test_helper'
NestedPost = Class.new(Model) class NestedPost < ::Model; end
class NestedPostSerializer < ActiveModel::Serializer class NestedPostSerializer < ActiveModel::Serializer
has_many :nested_posts has_many :nested_posts
end end
module ActiveModelSerializers module ActiveModelSerializers
module Adapter module Adapter
class JsonApi class JsonApi
@ -283,8 +282,8 @@ module ActiveModelSerializers
end end
class NoDuplicatesTest < ActiveSupport::TestCase class NoDuplicatesTest < ActiveSupport::TestCase
Post = Class.new(::Model) class Post < ::Model; end
Author = Class.new(::Model) class Author < ::Model; end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
type 'posts' type 'posts'
@ -303,8 +302,8 @@ module ActiveModelSerializers
@author.posts << @post1 @author.posts << @post1
@author.posts << @post2 @author.posts << @post2
@nestedpost1 = ::NestedPost.new(id: 1, nested_posts: []) @nestedpost1 = NestedPost.new(id: 1, nested_posts: [])
@nestedpost2 = ::NestedPost.new(id: 2, nested_posts: []) @nestedpost2 = NestedPost.new(id: 2, nested_posts: [])
@nestedpost1.nested_posts << @nestedpost1 @nestedpost1.nested_posts << @nestedpost1
@nestedpost1.nested_posts << @nestedpost2 @nestedpost1.nested_posts << @nestedpost2
@nestedpost2.nested_posts << @nestedpost1 @nestedpost2.nested_posts << @nestedpost1

View File

@ -4,7 +4,7 @@ module ActiveModelSerializers
module Adapter module Adapter
class JsonApi class JsonApi
class LinksTest < ActiveSupport::TestCase class LinksTest < ActiveSupport::TestCase
LinkAuthor = Class.new(::Model) class LinkAuthor < ::Model; end
class LinkAuthorSerializer < ActiveModel::Serializer class LinkAuthorSerializer < ActiveModel::Serializer
link :self do link :self do
href "http://example.com/link_author/#{object.id}" href "http://example.com/link_author/#{object.id}"

View File

@ -4,7 +4,10 @@ module ActiveModelSerializers
module Adapter module Adapter
class JsonApi class JsonApi
class KeyCaseTest < ActiveSupport::TestCase class KeyCaseTest < ActiveSupport::TestCase
Post = Class.new(::Model) class Post < ::Model; end
class Author < ::Model; end
class Comment < ::Model; end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
type 'posts' type 'posts'
attributes :title, :body, :publish_at attributes :title, :body, :publish_at
@ -23,13 +26,11 @@ module ActiveModelSerializers
end end
end end
Author = Class.new(::Model)
class AuthorSerializer < ActiveModel::Serializer class AuthorSerializer < ActiveModel::Serializer
type 'authors' type 'authors'
attributes :first_name, :last_name attributes :first_name, :last_name
end end
Comment = Class.new(::Model)
class CommentSerializer < ActiveModel::Serializer class CommentSerializer < ActiveModel::Serializer
type 'comments' type 'comments'
attributes :body attributes :body

View File

@ -4,7 +4,7 @@ require_relative './app'
# https://github.com/ruby-bench/ruby-bench-suite/blob/8ad567f7e43a044ae48c36833218423bb1e2bd9d/rails/benchmarks/actionpack_router.rb # https://github.com/ruby-bench/ruby-bench-suite/blob/8ad567f7e43a044ae48c36833218423bb1e2bd9d/rails/benchmarks/actionpack_router.rb
class ApiAssertion class ApiAssertion
include Benchmark::ActiveModelSerializers::TestMethods include Benchmark::ActiveModelSerializers::TestMethods
BadRevisionError = Class.new(StandardError) class BadRevisionError < StandardError; end
def valid? def valid?
caching = get_caching caching = get_caching

View File

@ -4,22 +4,22 @@ require 'tempfile'
module ActiveModelSerializers module ActiveModelSerializers
class CacheTest < ActiveSupport::TestCase class CacheTest < ActiveSupport::TestCase
UncachedAuthor = Class.new(Author) do class UncachedAuthor < Author
# To confirm cache_key is set using updated_at and cache_key option passed to cache # To confirm cache_key is set using updated_at and cache_key option passed to cache
undef_method :cache_key undef_method :cache_key
end end
Article = Class.new(::Model) do class Article < ::Model
# To confirm error is raised when cache_key is not set and cache_key option not passed to cache # To confirm error is raised when cache_key is not set and cache_key option not passed to cache
undef_method :cache_key undef_method :cache_key
end end
ArticleSerializer = Class.new(ActiveModel::Serializer) do class ArticleSerializer < ActiveModel::Serializer
cache only: [:place], skip_digest: true cache only: [:place], skip_digest: true
attributes :title attributes :title
end end
InheritedRoleSerializer = Class.new(RoleSerializer) do class InheritedRoleSerializer < RoleSerializer
cache key: 'inherited_role', only: [:name, :special_attribute] cache key: 'inherited_role', only: [:name, :special_attribute]
attribute :special_attribute attribute :special_attribute
end end

View File

@ -3,7 +3,7 @@ require 'test_helper'
module ActiveModel module ActiveModel
class Serializer class Serializer
class CollectionSerializerTest < ActiveSupport::TestCase class CollectionSerializerTest < ActiveSupport::TestCase
MessagesSerializer = Class.new(ActiveModel::Serializer) do class MessagesSerializer < ActiveModel::Serializer
type 'messages' type 'messages'
end end

85
test/fixtures/poro.rb vendored
View File

@ -50,18 +50,18 @@ class ProfilePreviewSerializer < ActiveModel::Serializer
attributes :name attributes :name
end end
Post = Class.new(Model) class Post < Model; end
Like = Class.new(Model) class Like < Model; end
Author = Class.new(Model) class Author < Model; end
Bio = Class.new(Model) class Bio < Model; end
Blog = Class.new(Model) class Blog < Model; end
Role = Class.new(Model) class Role < Model; end
User = Class.new(Model) class User < Model; end
Location = Class.new(Model) class Location < Model; end
Place = Class.new(Model) class Place < Model; end
Tag = Class.new(Model) class Tag < Model; end
VirtualValue = Class.new(Model) class VirtualValue < Model; end
Comment = Class.new(Model) do class Comment < Model
# Uses a custom non-time-based cache key # Uses a custom non-time-based cache key
def cache_key def cache_key
"#{self.class.name.downcase}/#{id}" "#{self.class.name.downcase}/#{id}"
@ -87,10 +87,11 @@ class PolyTag < ActiveRecord::Base
has_many :object_tags has_many :object_tags
end end
module Spam; end module Spam
Spam::UnrelatedLink = Class.new(Model) class UnrelatedLink < Model; end
end
PostSerializer = Class.new(ActiveModel::Serializer) do class PostSerializer < ActiveModel::Serializer
cache key: 'post', expires_in: 0.1, skip_digest: true cache key: 'post', expires_in: 0.1, skip_digest: true
attributes :id, :title, :body attributes :id, :title, :body
@ -108,12 +109,12 @@ PostSerializer = Class.new(ActiveModel::Serializer) do
end end
end end
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do class SpammyPostSerializer < ActiveModel::Serializer
attributes :id attributes :id
has_many :related has_many :related
end end
CommentSerializer = Class.new(ActiveModel::Serializer) do class CommentSerializer < ActiveModel::Serializer
cache expires_in: 1.day, skip_digest: true cache expires_in: 1.day, skip_digest: true
attributes :id, :body attributes :id, :body
@ -125,7 +126,7 @@ CommentSerializer = Class.new(ActiveModel::Serializer) do
end end
end end
AuthorSerializer = Class.new(ActiveModel::Serializer) do class AuthorSerializer < ActiveModel::Serializer
cache key: 'writer', skip_digest: true cache key: 'writer', skip_digest: true
attribute :id attribute :id
attribute :name attribute :name
@ -135,7 +136,7 @@ AuthorSerializer = Class.new(ActiveModel::Serializer) do
has_one :bio has_one :bio
end end
RoleSerializer = Class.new(ActiveModel::Serializer) do class RoleSerializer < ActiveModel::Serializer
cache only: [:name, :slug], skip_digest: true cache only: [:name, :slug], skip_digest: true
attributes :id, :name, :description attributes :id, :name, :description
attribute :friendly_id, key: :slug attribute :friendly_id, key: :slug
@ -147,13 +148,13 @@ RoleSerializer = Class.new(ActiveModel::Serializer) do
belongs_to :author belongs_to :author
end end
LikeSerializer = Class.new(ActiveModel::Serializer) do class LikeSerializer < ActiveModel::Serializer
attributes :id, :time attributes :id, :time
belongs_to :likeable belongs_to :likeable
end end
LocationSerializer = Class.new(ActiveModel::Serializer) do class LocationSerializer < ActiveModel::Serializer
cache only: [:address], skip_digest: true cache only: [:address], skip_digest: true
attributes :id, :lat, :lng attributes :id, :lat, :lng
@ -164,20 +165,20 @@ LocationSerializer = Class.new(ActiveModel::Serializer) do
end end
end end
PlaceSerializer = Class.new(ActiveModel::Serializer) do class PlaceSerializer < ActiveModel::Serializer
attributes :id, :name attributes :id, :name
has_many :locations has_many :locations
end end
BioSerializer = Class.new(ActiveModel::Serializer) do class BioSerializer < ActiveModel::Serializer
cache except: [:content], skip_digest: true cache except: [:content], skip_digest: true
attributes :id, :content, :rating attributes :id, :content, :rating
belongs_to :author belongs_to :author
end end
BlogSerializer = Class.new(ActiveModel::Serializer) do class BlogSerializer < ActiveModel::Serializer
cache key: 'blog' cache key: 'blog'
attributes :id, :name attributes :id, :name
@ -185,50 +186,50 @@ BlogSerializer = Class.new(ActiveModel::Serializer) do
has_many :articles has_many :articles
end end
PaginatedSerializer = Class.new(ActiveModel::Serializer::CollectionSerializer) do class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
def json_key def json_key
'paginated' 'paginated'
end end
end end
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do class AlternateBlogSerializer < ActiveModel::Serializer
attribute :id attribute :id
attribute :name, key: :title attribute :name, key: :title
end end
CustomBlogSerializer = Class.new(ActiveModel::Serializer) do class CustomBlogSerializer < ActiveModel::Serializer
attribute :id attribute :id
attribute :special_attribute attribute :special_attribute
has_many :articles has_many :articles
end end
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do class CommentPreviewSerializer < ActiveModel::Serializer
attributes :id attributes :id
belongs_to :post belongs_to :post
end end
AuthorPreviewSerializer = Class.new(ActiveModel::Serializer) do class AuthorPreviewSerializer < ActiveModel::Serializer
attributes :id attributes :id
has_many :posts has_many :posts
end end
PostPreviewSerializer = Class.new(ActiveModel::Serializer) do class PostPreviewSerializer < ActiveModel::Serializer
attributes :title, :body, :id attributes :title, :body, :id
has_many :comments, serializer: CommentPreviewSerializer has_many :comments, serializer: CommentPreviewSerializer
belongs_to :author, serializer: AuthorPreviewSerializer belongs_to :author, serializer: AuthorPreviewSerializer
end end
PostWithTagsSerializer = Class.new(ActiveModel::Serializer) do class PostWithTagsSerializer < ActiveModel::Serializer
attributes :id attributes :id
has_many :tags has_many :tags
end end
PostWithCustomKeysSerializer = Class.new(ActiveModel::Serializer) do class PostWithCustomKeysSerializer < ActiveModel::Serializer
attributes :id attributes :id
has_many :comments, key: :reviews has_many :comments, key: :reviews
@ -236,7 +237,7 @@ PostWithCustomKeysSerializer = Class.new(ActiveModel::Serializer) do
has_one :blog, key: :site has_one :blog, key: :site
end end
VirtualValueSerializer = Class.new(ActiveModel::Serializer) do class VirtualValueSerializer < ActiveModel::Serializer
attributes :id attributes :id
has_many :reviews, virtual_value: [{ type: 'reviews', id: '1' }, has_many :reviews, virtual_value: [{ type: 'reviews', id: '1' },
@ -250,34 +251,36 @@ VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
end end
end end
PolymorphicHasManySerializer = Class.new(ActiveModel::Serializer) do class PolymorphicHasManySerializer < ActiveModel::Serializer
attributes :id, :name attributes :id, :name
end end
PolymorphicBelongsToSerializer = Class.new(ActiveModel::Serializer) do class PolymorphicBelongsToSerializer < ActiveModel::Serializer
attributes :id, :title attributes :id, :title
has_one :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true has_one :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true
end end
PolymorphicSimpleSerializer = Class.new(ActiveModel::Serializer) do class PolymorphicSimpleSerializer < ActiveModel::Serializer
attributes :id attributes :id
end end
PolymorphicObjectTagSerializer = Class.new(ActiveModel::Serializer) do class PolymorphicObjectTagSerializer < ActiveModel::Serializer
attributes :id attributes :id
has_many :taggable, serializer: PolymorphicSimpleSerializer, polymorphic: true has_many :taggable, serializer: PolymorphicSimpleSerializer, polymorphic: true
end end
PolymorphicTagSerializer = Class.new(ActiveModel::Serializer) do class PolymorphicTagSerializer < ActiveModel::Serializer
attributes :id, :phrase attributes :id, :phrase
has_many :object_tags, serializer: PolymorphicObjectTagSerializer has_many :object_tags, serializer: PolymorphicObjectTagSerializer
end end
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do module Spam
cache only: [:id] class UnrelatedLinkSerializer < ActiveModel::Serializer
attributes :id cache only: [:id]
attributes :id
end
end end
$VERBOSE = verbose $VERBOSE = verbose

View File

@ -3,7 +3,8 @@ require 'test_helper'
module ActiveModel module ActiveModel
class Serializer class Serializer
class AssociationMacrosTest < ActiveSupport::TestCase class AssociationMacrosTest < ActiveSupport::TestCase
AuthorSummarySerializer = Class.new class AuthorSummarySerializer < ActiveModel::Serializer; end
class AssociationsTestSerializer < Serializer class AssociationsTestSerializer < Serializer
belongs_to :author, serializer: AuthorSummarySerializer belongs_to :author, serializer: AuthorSummarySerializer
has_many :comments has_many :comments

View File

@ -159,18 +159,18 @@ module ActiveModel
class NamespacedResourcesTest < ActiveSupport::TestCase class NamespacedResourcesTest < ActiveSupport::TestCase
class ResourceNamespace class ResourceNamespace
Post = Class.new(::Model) class Post < ::Model; end
Comment = Class.new(::Model) class Comment < ::Model; end
Author = Class.new(::Model) class Author < ::Model; end
Description = Class.new(::Model) class Description < ::Model; end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
has_many :comments has_many :comments
belongs_to :author belongs_to :author
has_one :description has_one :description
end end
CommentSerializer = Class.new(ActiveModel::Serializer) class CommentSerializer < ActiveModel::Serializer; end
AuthorSerializer = Class.new(ActiveModel::Serializer) class AuthorSerializer < ActiveModel::Serializer; end
DescriptionSerializer = Class.new(ActiveModel::Serializer) class DescriptionSerializer < ActiveModel::Serializer; end
end end
def setup def setup
@ -200,17 +200,17 @@ module ActiveModel
end end
class NestedSerializersTest < ActiveSupport::TestCase class NestedSerializersTest < ActiveSupport::TestCase
Post = Class.new(::Model) class Post < ::Model; end
Comment = Class.new(::Model) class Comment < ::Model; end
Author = Class.new(::Model) class Author < ::Model; end
Description = Class.new(::Model) class Description < ::Model; end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
has_many :comments has_many :comments
CommentSerializer = Class.new(ActiveModel::Serializer) class CommentSerializer < ActiveModel::Serializer; end
belongs_to :author belongs_to :author
AuthorSerializer = Class.new(ActiveModel::Serializer) class AuthorSerializer < ActiveModel::Serializer; end
has_one :description has_one :description
DescriptionSerializer = Class.new(ActiveModel::Serializer) class DescriptionSerializer < ActiveModel::Serializer; end
end end
def setup def setup
@ -291,12 +291,12 @@ module ActiveModel
end end
class InheritedSerializerTest < ActiveSupport::TestCase class InheritedSerializerTest < ActiveSupport::TestCase
InheritedPostSerializer = Class.new(PostSerializer) do class InheritedPostSerializer < PostSerializer
belongs_to :author, polymorphic: true belongs_to :author, polymorphic: true
has_many :comments, key: :reviews has_many :comments, key: :reviews
end end
InheritedAuthorSerializer = Class.new(AuthorSerializer) do class InheritedAuthorSerializer < AuthorSerializer
has_many :roles, polymorphic: true has_many :roles, polymorphic: true
has_one :bio, polymorphic: true has_one :bio, polymorphic: true
end end

View File

@ -81,7 +81,7 @@ module ActiveModel
assert_equal('custom', hash[:blog][:id]) assert_equal('custom', hash[:blog][:id])
end end
PostWithVirtualAttribute = Class.new(::Model) class PostWithVirtualAttribute < ::Model; end
class PostWithVirtualAttributeSerializer < ActiveModel::Serializer class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
attribute :name do attribute :name do
"#{object.first_name} #{object.last_name}" "#{object.first_name} #{object.last_name}"

View File

@ -28,8 +28,8 @@ module ActiveModel
class SerializerTest < ActiveSupport::TestCase class SerializerTest < ActiveSupport::TestCase
module ResourceNamespace module ResourceNamespace
Post = Class.new(::Model) class Post < ::Model; end
Comment = Class.new(::Model) class Comment < ::Model; end
class PostSerializer < ActiveModel::Serializer class PostSerializer < ActiveModel::Serializer
class CommentSerializer < ActiveModel::Serializer class CommentSerializer < ActiveModel::Serializer
@ -46,7 +46,7 @@ module ActiveModel
end end
end end
Tweet = Class.new(::Model) class Tweet < ::Model; end
TweetSerializer = Class.new TweetSerializer = Class.new
def setup def setup