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.

API:

  • private

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.

API:

  • private



14
15
16
17
# File 'lib/active_model/serializer/association.rb', line 14

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

Instance Attribute Details

#association_optionsObject

Returns the value of attribute association_options

Returns:

  • the current value of association_options



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

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.

API:

  • private



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

def lazy_association
  @lazy_association
end

#reflectionObject

Returns the value of attribute reflection

Returns:

  • the current value of reflection



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

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:

API:

  • private



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

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:

API:

  • private



23
24
25
# File 'lib/active_model/serializer/association.rb', line 23

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:

API:

  • private



28
29
30
# File 'lib/active_model/serializer/association.rb', line 28

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:

API:

  • private



33
34
35
# File 'lib/active_model/serializer/association.rb', line 33

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:

API:

  • private



39
40
41
# File 'lib/active_model/serializer/association.rb', line 39

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:

API:

  • private



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

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:

API:

  • private



47
48
49
# File 'lib/active_model/serializer/association.rb', line 47

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.

API:

  • private



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

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