Class: ActiveModel::Serializer::Association Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This class holds all information about serializer’s association.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAssociation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Association.



12
13
14
15
# File 'lib/active_model/serializer/association.rb', line 12

def initialize(*)
  super
  @lazy_association = LazyAssociation.new(reflection, association_options)
end

Instance Attribute Details

#association_optionsObject

Returns the value of attribute association_options

Returns:

  • (Object)

    the current value of association_options



8
9
10
# File 'lib/active_model/serializer/association.rb', line 8

def association_options
  @association_options
end

#lazy_associationObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



9
10
11
# File 'lib/active_model/serializer/association.rb', line 9

def lazy_association
  @lazy_association
end

#reflectionObject

Returns the value of attribute reflection

Returns:

  • (Object)

    the current value of reflection



8
9
10
# File 'lib/active_model/serializer/association.rb', line 8

def reflection
  @reflection
end

Instance Method Details

#belongs_to?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


41
42
43
# File 'lib/active_model/serializer/association.rb', line 41

def belongs_to?
  reflection.foreign_key_on == :self
end

#keySymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Symbol)


21
22
23
# File 'lib/active_model/serializer/association.rb', line 21

def key
  reflection_options.fetch(:key, name)
end

#key?True, False

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (True, False)


26
27
28
# File 'lib/active_model/serializer/association.rb', line 26

def key?
  reflection_options.key?(:key)
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash)


31
32
33
# File 'lib/active_model/serializer/association.rb', line 31

def links
  reflection_options.fetch(:links) || {}
end

#metaHash?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This gets mutated, so cannot use the cached reflection_options

Returns:

  • (Hash, nil)


37
38
39
# File 'lib/active_model/serializer/association.rb', line 37

def meta
  reflection.options[:meta]
end

#nameSymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Symbol)


18
# File 'lib/active_model/serializer/association.rb', line 18

delegate :name, to: :reflection

#polymorphic?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


45
46
47
# File 'lib/active_model/serializer/association.rb', line 45

def polymorphic?
  true == reflection_options[:polymorphic]
end

#serializable_hash(adapter_options, adapter_instance) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_model/serializer/association.rb', line 50

def serializable_hash(adapter_options, adapter_instance)
  association_serializer = lazy_association.serializer
  return virtual_value if virtual_value
  association_object = association_serializer && association_serializer.object
  return unless association_object

  serialization = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)

  if polymorphic? && serialization
    polymorphic_type = association_object.class.name.underscore
    serialization = { type: polymorphic_type, polymorphic_type.to_sym => serialization }
  end

  serialization
end