Class: ActiveModel::Serializer::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/serializer/associations.rb

Overview

:nodoc:

Direct Known Subclasses

HasMany, HasOne

Defined Under Namespace

Classes: HasMany, HasOne

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={}, serializer_options={})
  @name          = name
  @object        = options[:value]

  embed          = options[:embed]
  @embed_ids     = embed == :id || embed == :ids
  @embed_objects = embed == :object || embed == :objects
  @embed_key     = options[:embed_key] || :id
  @embed_in_root = options[:include]

  serializer = options[:serializer]
  @serializer_class = serializer.is_a?(String) ? serializer.constantize : serializer

  @options = options
  @serializer_options = serializer_options
end

Instance Attribute Details

#embed_idsObject (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
  @embed_ids
end

#embed_in_rootObject (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
  @embed_in_root
end

#embed_objectsObject (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
  @embed_objects
end

#nameObject (readonly)

Returns the value of attribute name.



43
44
45
# File 'lib/active_model/serializer/associations.rb', line 43

def name
  @name
end

#objectObject (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

#rootObject (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

#keyObject



50
51
52
53
54
55
56
57
58
# File 'lib/active_model/serializer/associations.rb', line 50

def key
  if key = options[:key]
    key
  elsif use_id_key?
    id_key
  else
    name
  end
end