mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
This will prevent objects PORO objects that don't have updated_at defined, from throwing an error. Not as big a deal now that PORO objects can inherit ActiveModelSerializers::Model, but still necessary if it's not inherited for whatever reason. Add the Adapter type to the cache key. This prevents incorrect results when the same object is serialized with different adapters. BF: Cherry-pick of040a97b9e9which was a squash off89ed71058from pr 1346
259 lines
5.5 KiB
Ruby
259 lines
5.5 KiB
Ruby
verbose = $VERBOSE
|
|
$VERBOSE = nil
|
|
class Model < ActiveModelSerializers::Model
|
|
### Helper methods, not required to be serializable
|
|
|
|
# Convenience when not adding @attributes readers and writers
|
|
def method_missing(meth, *args)
|
|
if meth.to_s =~ /^(.*)=$/
|
|
attributes[$1.to_sym] = args[0]
|
|
elsif attributes.key?(meth)
|
|
attributes[meth]
|
|
else
|
|
super
|
|
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
|
|
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
|
|
end
|
|
|
|
class Profile < Model
|
|
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
|
|
|
|
Post = Class.new(Model)
|
|
Like = Class.new(Model)
|
|
Author = Class.new(Model)
|
|
Bio = Class.new(Model)
|
|
Blog = Class.new(Model) do
|
|
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
|
|
|
|
def digest
|
|
FILE_DIGEST
|
|
end
|
|
end
|
|
Role = Class.new(Model)
|
|
User = Class.new(Model)
|
|
Location = Class.new(Model)
|
|
Place = Class.new(Model)
|
|
Tag = Class.new(Model)
|
|
VirtualValue = Class.new(Model)
|
|
Comment = Class.new(Model) do
|
|
# Uses a custom non-time-based cache key
|
|
def cache_key
|
|
"#{self.class.name.downcase}/#{self.id}"
|
|
end
|
|
end
|
|
|
|
class Employee < ActiveRecord::Base
|
|
has_many :pictures, as: :imageable
|
|
end
|
|
|
|
class Picture < ActiveRecord::Base
|
|
belongs_to :imageable, polymorphic: true
|
|
end
|
|
|
|
module Spam; end
|
|
Spam::UnrelatedLink = Class.new(Model)
|
|
|
|
PostSerializer = Class.new(ActiveModel::Serializer) do
|
|
cache key: 'post', expires_in: 0.1, skip_digest: true
|
|
attributes :id, :title, :body
|
|
|
|
has_many :comments
|
|
belongs_to :blog
|
|
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
|
|
|
|
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id
|
|
has_many :related
|
|
end
|
|
|
|
CommentSerializer = Class.new(ActiveModel::Serializer) do
|
|
cache expires_in: 1.day, skip_digest: true
|
|
attributes :id, :body
|
|
|
|
belongs_to :post
|
|
belongs_to :author
|
|
|
|
def custom_options
|
|
instance_options
|
|
end
|
|
end
|
|
|
|
AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
|
cache key: 'writer', skip_digest: true
|
|
attribute :id
|
|
attribute :name
|
|
|
|
has_many :posts
|
|
has_many :roles
|
|
has_one :bio
|
|
end
|
|
|
|
RoleSerializer = Class.new(ActiveModel::Serializer) do
|
|
cache only: [:name], skip_digest: true
|
|
attributes :id, :name, :description, :slug
|
|
|
|
def slug
|
|
"#{object.name}-#{object.id}"
|
|
end
|
|
|
|
belongs_to :author
|
|
end
|
|
|
|
LikeSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id, :time
|
|
|
|
belongs_to :likeable
|
|
end
|
|
|
|
LocationSerializer = Class.new(ActiveModel::Serializer) do
|
|
cache only: [:place], skip_digest: true
|
|
attributes :id, :lat, :lng
|
|
|
|
belongs_to :place
|
|
|
|
def place
|
|
'Nowhere'
|
|
end
|
|
end
|
|
|
|
PlaceSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id, :name
|
|
|
|
has_many :locations
|
|
end
|
|
|
|
BioSerializer = Class.new(ActiveModel::Serializer) do
|
|
cache except: [:content], skip_digest: true
|
|
attributes :id, :content, :rating
|
|
|
|
belongs_to :author
|
|
end
|
|
|
|
BlogSerializer = Class.new(ActiveModel::Serializer) do
|
|
cache key: 'blog'
|
|
attributes :id, :name
|
|
|
|
belongs_to :writer
|
|
has_many :articles
|
|
end
|
|
|
|
PaginatedSerializer = Class.new(ActiveModel::Serializer::CollectionSerializer) do
|
|
def json_key
|
|
'paginated'
|
|
end
|
|
end
|
|
|
|
AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
|
|
attribute :id
|
|
attribute :name, key: :title
|
|
end
|
|
|
|
CustomBlogSerializer = Class.new(ActiveModel::Serializer) do
|
|
attribute :id
|
|
attribute :special_attribute
|
|
|
|
has_many :articles
|
|
end
|
|
|
|
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id
|
|
|
|
belongs_to :post
|
|
end
|
|
|
|
AuthorPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id
|
|
|
|
has_many :posts
|
|
end
|
|
|
|
PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :title, :body, :id
|
|
|
|
has_many :comments, serializer: CommentPreviewSerializer
|
|
belongs_to :author, serializer: AuthorPreviewSerializer
|
|
end
|
|
|
|
PostWithTagsSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id
|
|
|
|
has_many :tags
|
|
end
|
|
|
|
PostWithCustomKeysSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id
|
|
|
|
has_many :comments, key: :reviews
|
|
belongs_to :author, key: :writer
|
|
has_one :blog, key: :site
|
|
end
|
|
|
|
VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id
|
|
|
|
has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
|
|
has_one :maker, virtual_value: { id: 1 }
|
|
|
|
def reviews
|
|
end
|
|
|
|
def maker
|
|
end
|
|
end
|
|
|
|
PolymorphicHasManySerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id, :name
|
|
end
|
|
|
|
PolymorphicBelongsToSerializer = Class.new(ActiveModel::Serializer) do
|
|
attributes :id, :title
|
|
|
|
has_one :imageable, serializer: PolymorphicHasManySerializer
|
|
end
|
|
|
|
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
|
cache only: [:id]
|
|
attributes :id
|
|
end
|
|
$VERBOSE = verbose
|