mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
AMS::Associations::Base is now AMS::Association. HasMany and HasOne inherits from it.
This commit is contained in:
parent
787b7cf24a
commit
055f8fe33c
@ -152,7 +152,7 @@ module ActiveModel
|
|||||||
# with the association name does not exist, the association name is
|
# with the association name does not exist, the association name is
|
||||||
# dispatched to the serialized object.
|
# dispatched to the serialized object.
|
||||||
def has_many(*attrs)
|
def has_many(*attrs)
|
||||||
associate(Associations::HasMany, attrs)
|
associate(Association::HasMany, attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Defines an association in the object should be rendered.
|
# Defines an association in the object should be rendered.
|
||||||
@ -162,7 +162,7 @@ module ActiveModel
|
|||||||
# with the association name does not exist, the association name is
|
# with the association name does not exist, the association name is
|
||||||
# dispatched to the serialized object.
|
# dispatched to the serialized object.
|
||||||
def has_one(*attrs)
|
def has_one(*attrs)
|
||||||
associate(Associations::HasOne, attrs)
|
associate(Association::HasOne, attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return a schema hash for the current serializer. This information
|
# Return a schema hash for the current serializer. This information
|
||||||
@ -372,9 +372,9 @@ module ActiveModel
|
|||||||
options = klass_options.merge options
|
options = klass_options.merge options
|
||||||
klass
|
klass
|
||||||
elsif value.respond_to?(:to_ary)
|
elsif value.respond_to?(:to_ary)
|
||||||
Associations::HasMany
|
Association::HasMany
|
||||||
else
|
else
|
||||||
Associations::HasOne
|
Association::HasOne
|
||||||
end
|
end
|
||||||
|
|
||||||
options = default_embed_options.merge!(options)
|
options = default_embed_options.merge!(options)
|
||||||
|
|||||||
@ -1,79 +1,77 @@
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
module Associations #:nodoc:
|
class Association #:nodoc:
|
||||||
class Base #:nodoc:
|
# name: The name of the association.
|
||||||
# name: The name of the association.
|
#
|
||||||
#
|
# options: A hash. These keys are accepted:
|
||||||
# options: A hash. These keys are accepted:
|
#
|
||||||
#
|
# value: The object we're associating with.
|
||||||
# value: The object we're associating with.
|
#
|
||||||
#
|
# serializer: The class used to serialize the association.
|
||||||
# serializer: The class used to serialize the association.
|
#
|
||||||
#
|
# embed: Define how associations should be embedded.
|
||||||
# embed: Define how associations should be embedded.
|
# - :objects # Embed associations as full objects.
|
||||||
# - :objects # Embed associations as full objects.
|
# - :ids # Embed only the association ids.
|
||||||
# - :ids # Embed only the association ids.
|
# - :ids, :include => true # Embed the association ids and include objects in the root.
|
||||||
# - :ids, :include => true # Embed the association ids and include objects in the root.
|
#
|
||||||
#
|
# include: Used in conjunction with embed :ids. Includes the objects in the root.
|
||||||
# include: Used in conjunction with embed :ids. Includes the objects in the root.
|
#
|
||||||
#
|
# root: Used in conjunction with include: true. Defines the key used to embed the objects.
|
||||||
# root: Used in conjunction with include: true. Defines the key used to embed the objects.
|
#
|
||||||
#
|
# key: Key name used to store the ids in.
|
||||||
# key: Key name used to store the ids in.
|
#
|
||||||
#
|
# embed_key: Method used to fetch ids. Defaults to :id.
|
||||||
# embed_key: Method used to fetch ids. Defaults to :id.
|
#
|
||||||
#
|
# polymorphic: Is the association is polymorphic?. Values: true or false.
|
||||||
# polymorphic: Is the association is polymorphic?. Values: true or false.
|
def initialize(name, options={}, serializer_options={})
|
||||||
def initialize(name, options={}, serializer_options={})
|
@name = name
|
||||||
@name = name
|
@object = options[:value]
|
||||||
@object = options[:value]
|
|
||||||
|
|
||||||
embed = options[:embed]
|
embed = options[:embed]
|
||||||
@embed_ids = embed == :id || embed == :ids
|
@embed_ids = embed == :id || embed == :ids
|
||||||
@embed_objects = embed == :object || embed == :objects
|
@embed_objects = embed == :object || embed == :objects
|
||||||
@embed_key = options[:embed_key] || :id
|
@embed_key = options[:embed_key] || :id
|
||||||
@embed_in_root = options[:include]
|
@embed_in_root = options[:include]
|
||||||
|
|
||||||
serializer = options[:serializer]
|
serializer = options[:serializer]
|
||||||
@serializer = serializer.is_a?(String) ? serializer.constantize : serializer
|
@serializer = serializer.is_a?(String) ? serializer.constantize : serializer
|
||||||
|
|
||||||
@options = options
|
@options = options
|
||||||
@serializer_options = serializer_options
|
@serializer_options = serializer_options
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :object, :root, :name, :embed_ids, :embed_objects, :embed_in_root
|
attr_reader :object, :root, :name, :embed_ids, :embed_objects, :embed_in_root
|
||||||
alias embeddable? object
|
alias embeddable? object
|
||||||
alias embed_objects? embed_objects
|
alias embed_objects? embed_objects
|
||||||
alias embed_ids? embed_ids
|
alias embed_ids? embed_ids
|
||||||
alias use_id_key? embed_ids?
|
alias use_id_key? embed_ids?
|
||||||
alias embed_in_root? embed_in_root
|
alias embed_in_root? embed_in_root
|
||||||
|
|
||||||
def key
|
def key
|
||||||
if key = options[:key]
|
if key = options[:key]
|
||||||
key
|
key
|
||||||
elsif use_id_key?
|
elsif use_id_key?
|
||||||
id_key
|
id_key
|
||||||
else
|
else
|
||||||
name
|
name
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
attr_reader :embed_key, :serializer, :options, :serializer_options
|
|
||||||
|
|
||||||
def find_serializable(object)
|
|
||||||
if serializer
|
|
||||||
serializer.new(object, serializer_options)
|
|
||||||
elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer)
|
|
||||||
ams.new(object, serializer_options)
|
|
||||||
else
|
|
||||||
object
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HasMany < Base #:nodoc:
|
private
|
||||||
|
|
||||||
|
attr_reader :embed_key, :serializer, :options, :serializer_options
|
||||||
|
|
||||||
|
def find_serializable(object)
|
||||||
|
if serializer
|
||||||
|
serializer.new(object, serializer_options)
|
||||||
|
elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer)
|
||||||
|
ams.new(object, serializer_options)
|
||||||
|
else
|
||||||
|
object
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class HasMany < Association #:nodoc:
|
||||||
def root
|
def root
|
||||||
options[:root] || name
|
options[:root] || name
|
||||||
end
|
end
|
||||||
@ -101,7 +99,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HasOne < Base #:nodoc:
|
class HasOne < Association #:nodoc:
|
||||||
def initialize(name, options={}, serializer_options={})
|
def initialize(name, options={}, serializer_options={})
|
||||||
super
|
super
|
||||||
@polymorphic = options[:polymorphic]
|
@polymorphic = options[:polymorphic]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user