Class: ActiveModel::Serializer::Reflection

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

Overview

Holds all the meta-data about an association as it was specified in the ActiveModel::Serializer class.

So you can inspect reflections in your Adapters.

Examples:

class PostSerializer < ActiveModel::Serializer
   has_one :author, serializer: AuthorSerializer
   has_many :comments
end

PostSerializer._reflections #=>
  # [
  #   HasOneReflection.new(:author, serializer: AuthorSerializer),
  #   HasManyReflection.new(:comments)
  # ]

Direct Known Subclasses

CollectionReflection, SingularReflection

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



20
21
22
# File 'lib/active_model/serializer/reflection.rb', line 20

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



20
21
22
# File 'lib/active_model/serializer/reflection.rb', line 20

def options
  @options
end

Instance Method Details

#build_association(subject, parent_serializer_options) ⇒ 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.

Build association. This method is used internally to build serializer’s association by its reflection.

Examples:

# Given the following serializer defined:
class PostSerializer < ActiveModel::Serializer
  has_many :comments, serializer: CommentSummarySerializer
end

# Then you instantiate your serializer
post_serializer = PostSerializer.new(post, foo: 'bar') #
# to build association for comments you need to get reflection
comments_reflection = PostSerializer._reflections.detect { |r| r.name == :comments }
# and #build_association
comments_reflection.build_association(post_serializer, foo: 'bar')

Parameters:

  • subject (Serializer)

    is a parent serializer for given association

  • parent_serializer_options (Hash{Symbol => Object})


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_model/serializer/reflection.rb', line 42

def build_association(subject, parent_serializer_options)
  association_value = subject.send(name)
  reflection_options = options.dup
  serializer_class = ActiveModel::Serializer.serializer_for(association_value, reflection_options)

  if serializer_class
    begin
      serializer = serializer_class.new(
        association_value,
        serializer_options(parent_serializer_options, reflection_options)
      )
    rescue ActiveModel::Serializer::ArraySerializer::NoSerializerError
      reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
    end
  elsif !association_value.nil? && !association_value.instance_of?(Object)
    reflection_options[:virtual_value] = association_value
  end

  Association.new(name, serializer, reflection_options)
end