test/fixtures/poro.rb in active_model_serializers-0.10.4 vs test/fixtures/poro.rb in active_model_serializers-0.10.5
- old
+ new
@@ -1,98 +1,93 @@
-verbose = $VERBOSE
-$VERBOSE = nil
class Model < ActiveModelSerializers::Model
- FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
+ rand(2).zero? && derive_attributes_from_names_and_fix_accessors
- ### Helper methods, not required to be serializable
+ attr_writer :id
- # Convenience when not adding @attributes readers and writers
- def method_missing(meth, *args)
- if meth.to_s =~ /^(.*)=$/
- attributes[Regexp.last_match(1).to_sym] = args[0]
- elsif attributes.key?(meth)
- attributes[meth]
- else
- super
+ # At this time, just for organization of intent
+ class_attribute :association_names
+ self.association_names = []
+
+ def self.associations(*names)
+ self.association_names |= names.map(&:to_sym)
+ # Silence redefinition of methods warnings
+ ActiveModelSerializers.silence_warnings do
+ attr_accessor(*names)
end
end
- # required for ActiveModel::AttributeAssignment#_assign_attribute
- # in Rails 5
- def respond_to_missing?(method_name, _include_private = false)
- attributes.key?(method_name.to_s.tr('=', '').to_sym) || super
+ def associations
+ association_names.each_with_object({}) do |association_name, result|
+ result[association_name] = public_send(association_name).freeze
+ end.with_indifferent_access.freeze
end
+
+ def attributes
+ super.except(*association_names)
+ end
end
# see
# https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/errors.rb
# The below allows you to do:
#
# model = ModelWithErrors.new
# model.validate! # => ["cannot be nil"]
# model.errors.full_messages # => ["name cannot be nil"]
-class ModelWithErrors < ::ActiveModelSerializers::Model
- attr_accessor :name
+class ModelWithErrors < Model
+ attributes :name
end
class Profile < Model
+ attributes :name, :description
+ associations :comments
end
-
class ProfileSerializer < ActiveModel::Serializer
attributes :name, :description
-
- # TODO: is this used anywhere?
- def arguments_passed_in?
- instance_options[:my_options] == :accessible
- end
end
-
class ProfilePreviewSerializer < ActiveModel::Serializer
attributes :name
end
-class Post < Model; end
-class Like < Model; end
-class Author < Model; end
-class Bio < Model; end
-class Blog < Model; end
-class Role < Model; end
-class User < Model; end
-class Location < Model; end
-class Place < Model; end
-class Tag < Model; end
-class VirtualValue < Model; end
-class Comment < Model
- # Uses a custom non-time-based cache key
- def cache_key
- "#{self.class.name.downcase}/#{id}"
- end
+class Author < Model
+ attributes :name
+ associations :posts, :bio, :roles, :comments
end
+class AuthorSerializer < ActiveModel::Serializer
+ cache key: 'writer', skip_digest: true
+ attribute :id
+ attribute :name
-class Employee < ActiveRecord::Base
- has_many :pictures, as: :imageable
- has_many :object_tags, as: :taggable
+ has_many :posts
+ has_many :roles
+ has_one :bio
end
-
-class ObjectTag < ActiveRecord::Base
- belongs_to :poly_tag
- belongs_to :taggable, polymorphic: true
+class AuthorPreviewSerializer < ActiveModel::Serializer
+ attributes :id
+ has_many :posts
end
-class Picture < ActiveRecord::Base
- belongs_to :imageable, polymorphic: true
- has_many :object_tags, as: :taggable
+class Comment < Model
+ attributes :body, :date
+ associations :post, :author, :likes
end
-
-class PolyTag < ActiveRecord::Base
- has_many :object_tags
+class CommentSerializer < ActiveModel::Serializer
+ cache expires_in: 1.day, skip_digest: true
+ attributes :id, :body
+ belongs_to :post
+ belongs_to :author
end
+class CommentPreviewSerializer < ActiveModel::Serializer
+ attributes :id
-module Spam
- class UnrelatedLink < Model; end
+ belongs_to :post
end
+class Post < Model
+ attributes :title, :body
+ associations :author, :comments, :blog, :tags, :related
+end
class PostSerializer < ActiveModel::Serializer
cache key: 'post', expires_in: 0.1, skip_digest: true
attributes :id, :title, :body
has_many :comments
@@ -100,62 +95,79 @@
belongs_to :author
def blog
Blog.new(id: 999, name: 'Custom blog')
end
-
- # TODO: is this used anywhere?
- def custom_options
- instance_options
- end
end
-
class SpammyPostSerializer < ActiveModel::Serializer
attributes :id
has_many :related
end
+class PostPreviewSerializer < ActiveModel::Serializer
+ attributes :title, :body, :id
-class CommentSerializer < ActiveModel::Serializer
- cache expires_in: 1.day, skip_digest: true
- attributes :id, :body
+ has_many :comments, serializer: ::CommentPreviewSerializer
+ belongs_to :author, serializer: ::AuthorPreviewSerializer
+end
+class PostWithCustomKeysSerializer < ActiveModel::Serializer
+ attributes :id
+ has_many :comments, key: :reviews
+ belongs_to :author, key: :writer
+ has_one :blog, key: :site
+end
- belongs_to :post
+class Bio < Model
+ attributes :content, :rating
+ associations :author
+end
+class BioSerializer < ActiveModel::Serializer
+ cache except: [:content], skip_digest: true
+ attributes :id, :content, :rating
+
belongs_to :author
+end
- def custom_options
- instance_options
- end
+class Blog < Model
+ attributes :name, :type, :special_attribute
+ associations :writer, :articles
end
+class BlogSerializer < ActiveModel::Serializer
+ cache key: 'blog'
+ attributes :id, :name
-class AuthorSerializer < ActiveModel::Serializer
- cache key: 'writer', skip_digest: true
+ belongs_to :writer
+ has_many :articles
+end
+class AlternateBlogSerializer < ActiveModel::Serializer
attribute :id
- attribute :name
-
- has_many :posts
- has_many :roles
- has_one :bio
+ attribute :name, key: :title
end
+class CustomBlogSerializer < ActiveModel::Serializer
+ attribute :id
+ attribute :special_attribute
+ has_many :articles
+end
+class Role < Model
+ attributes :name, :description, :special_attribute
+ associations :author
+end
class RoleSerializer < ActiveModel::Serializer
cache only: [:name, :slug], skip_digest: true
attributes :id, :name, :description
attribute :friendly_id, key: :slug
+ belongs_to :author
def friendly_id
"#{object.name}-#{object.id}"
end
-
- belongs_to :author
end
-class LikeSerializer < ActiveModel::Serializer
- attributes :id, :time
-
- belongs_to :likeable
+class Location < Model
+ attributes :lat, :lng
+ associations :place
end
-
class LocationSerializer < ActiveModel::Serializer
cache only: [:address], skip_digest: true
attributes :id, :lat, :lng
belongs_to :place, key: :address
@@ -163,85 +175,40 @@
def place
'Nowhere'
end
end
+class Place < Model
+ attributes :name
+ associations :locations
+end
class PlaceSerializer < ActiveModel::Serializer
attributes :id, :name
-
has_many :locations
end
-class BioSerializer < ActiveModel::Serializer
- cache except: [:content], skip_digest: true
- attributes :id, :content, :rating
-
- belongs_to :author
+class Like < Model
+ attributes :time
+ associations :likeable
end
-
-class BlogSerializer < ActiveModel::Serializer
- cache key: 'blog'
- attributes :id, :name
-
- belongs_to :writer
- has_many :articles
+class LikeSerializer < ActiveModel::Serializer
+ attributes :id, :time
+ belongs_to :likeable
end
-class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
- def json_key
- 'paginated'
+module Spam
+ class UnrelatedLink < Model
end
+ class UnrelatedLinkSerializer < ActiveModel::Serializer
+ cache only: [:id]
+ attributes :id
+ end
end
-class AlternateBlogSerializer < ActiveModel::Serializer
- attribute :id
- attribute :name, key: :title
-end
-
-class CustomBlogSerializer < ActiveModel::Serializer
- attribute :id
- attribute :special_attribute
-
- has_many :articles
-end
-
-class CommentPreviewSerializer < ActiveModel::Serializer
- attributes :id
-
- belongs_to :post
-end
-
-class AuthorPreviewSerializer < ActiveModel::Serializer
- attributes :id
-
- has_many :posts
-end
-
-class PostPreviewSerializer < ActiveModel::Serializer
- attributes :title, :body, :id
-
- has_many :comments, serializer: CommentPreviewSerializer
- belongs_to :author, serializer: AuthorPreviewSerializer
-end
-
-class PostWithTagsSerializer < ActiveModel::Serializer
- attributes :id
-
- has_many :tags
-end
-
-class PostWithCustomKeysSerializer < ActiveModel::Serializer
- attributes :id
-
- has_many :comments, key: :reviews
- belongs_to :author, key: :writer
- has_one :blog, key: :site
-end
-
+class VirtualValue < Model; end
class VirtualValueSerializer < ActiveModel::Serializer
attributes :id
-
has_many :reviews, virtual_value: [{ type: 'reviews', id: '1' },
{ type: 'reviews', id: '2' }]
has_one :maker, virtual_value: { type: 'makers', id: '1' }
def reviews
@@ -249,38 +216,10 @@
def maker
end
end
-class PolymorphicHasManySerializer < ActiveModel::Serializer
- attributes :id, :name
-end
-
-class PolymorphicBelongsToSerializer < ActiveModel::Serializer
- attributes :id, :title
-
- has_one :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true
-end
-
-class PolymorphicSimpleSerializer < ActiveModel::Serializer
- attributes :id
-end
-
-class PolymorphicObjectTagSerializer < ActiveModel::Serializer
- attributes :id
-
- has_many :taggable, serializer: PolymorphicSimpleSerializer, polymorphic: true
-end
-
-class PolymorphicTagSerializer < ActiveModel::Serializer
- attributes :id, :phrase
-
- has_many :object_tags, serializer: PolymorphicObjectTagSerializer
-end
-
-module Spam
- class UnrelatedLinkSerializer < ActiveModel::Serializer
- cache only: [:id]
- attributes :id
+class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
+ def json_key
+ 'paginated'
end
end
-$VERBOSE = verbose