Class: ActiveModel::Serializer::Association
- Inherits:
-
Object
- Object
- ActiveModel::Serializer::Association
- Defined in:
- lib/active_model/serializer/associations.rb
Overview
:nodoc:
Defined Under Namespace
Instance Attribute Summary collapse
-
#embed_ids ⇒ Object
(also: #embed_ids?)
readonly
Returns the value of attribute embed_ids.
-
#embed_in_root ⇒ Object
(also: #embed_in_root?)
readonly
Returns the value of attribute embed_in_root.
-
#embed_objects ⇒ Object
(also: #embed_objects?)
readonly
Returns the value of attribute embed_objects.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#object ⇒ Object
(also: #embeddable?)
readonly
Returns the value of attribute object.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#initialize(name, options = {}, serializer_options = {}) ⇒ Association
constructor
name: The name of the association.
- #key ⇒ Object
Constructor Details
#initialize(name, options = {}, serializer_options = {}) ⇒ Association
name: The name of the association.
options: A hash. These keys are accepted:
value: The object we're associating with.
serializer: The class used to serialize the association.
embed: Define how associations should be embedded.
- :objects # Embed associations as full objects.
- :ids # Embed only the association ids.
- :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.
root: Used in conjunction with include: true. Defines the key used to embed the objects.
key: Key name used to store the ids in.
embed_key: Method used to fetch ids. Defaults to :id.
polymorphic: Is the association is polymorphic?. Values: true or false.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/active_model/serializer/associations.rb', line 26 def initialize(name, ={}, ={}) @name = name @object = [:value] = [:embed] @embed_ids = == :id || == :ids @embed_objects = == :object || == :objects @embed_key = [:embed_key] || :id @embed_in_root = [:include] serializer = [:serializer] @serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer @options = @serializer_options = end |
Instance Attribute Details
#embed_ids ⇒ Object (readonly) Also known as: embed_ids?
Returns the value of attribute embed_ids.
43 44 45 |
# File 'lib/active_model/serializer/associations.rb', line 43 def @embed_ids end |
#embed_in_root ⇒ Object (readonly) Also known as: embed_in_root?
Returns the value of attribute embed_in_root.
43 44 45 |
# File 'lib/active_model/serializer/associations.rb', line 43 def @embed_in_root end |
#embed_objects ⇒ Object (readonly) Also known as: embed_objects?
Returns the value of attribute embed_objects.
43 44 45 |
# File 'lib/active_model/serializer/associations.rb', line 43 def @embed_objects end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
43 44 45 |
# File 'lib/active_model/serializer/associations.rb', line 43 def name @name end |
#object ⇒ Object (readonly) Also known as: embeddable?
Returns the value of attribute object.
43 44 45 |
# File 'lib/active_model/serializer/associations.rb', line 43 def object @object end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
43 44 45 |
# File 'lib/active_model/serializer/associations.rb', line 43 def root @root end |
Instance Method Details
#key ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/active_model/serializer/associations.rb', line 50 def key if key = [:key] key elsif use_id_key? id_key else name end end |