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.



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:

  • (Object)

    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.



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:

  • (Object)

    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:

  • (Boolean)


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:

  • (Symbol)


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:

  • (True, False)


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:

  • (Hash)


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:

  • (Hash, nil)


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:

  • (Symbol)


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:

  • (Boolean)


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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

  adapter_sub_options = adapter_options.deep_dup
  fields = adapter_options.fetch(:fields, nil)
  sub_fields = []
  if fields.is_a?(Hash)
    sub_fields = fields.fetch(association_serializer.json_key, nil)
  else
    fields.each do |f|
      sub_fields = f.fetch(association_serializer.json_key, nil) if f.is_a?(Hash)
      break if sub_fields.present?
    end unless fields.nil?
  end

  if sub_fields.present?
    adapter_sub_options[:fields] = sub_fields.collect { |f| f.to_sym }
  else
    adapter_sub_options = {}
  end

  serialization = association_serializer.serializable_hash(adapter_options, adapter_sub_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