Module: ActiveModel::Serializer::Associations

Extended by:
ActiveSupport::Concern
Included in:
ActiveModel::Serializer
Defined in:
lib/active_model/serializer/associations.rb

Overview

Defines an association in the object should be rendered.

The serializer object should implement the association name as a method which should return an array when invoked. If a method with the association name does not exist, the association name is dispatched to the serialized object.

Defined Under Namespace

Modules: ClassMethods

Constant Summary

DEFAULT_INCLUDE_TREE =
ActiveModel::Serializer::IncludeTree.from_string('*')

Instance Method Summary (collapse)

Instance Method Details

- (Enumerator<Association>) associations(include_tree = DEFAULT_INCLUDE_TREE)

Parameters:

  • include_tree (IncludeTree) (defaults to: DEFAULT_INCLUDE_TREE)

    (defaults to all associations when not provided)

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/active_model/serializer/associations.rb', line 86

def associations(include_tree = DEFAULT_INCLUDE_TREE)
  return unless object

  Enumerator.new do |y|
    self.class._reflections.each do |reflection|
      next if reflection.excluded?(self)
      key = reflection.options.fetch(:key, reflection.name)
      next unless include_tree.key?(key)
      y.yield reflection.build_association(self, instance_options)
    end
  end
end