Class: ROM::Factory::Attributes::Association::ManyToOne Private

Inherits:
Core
  • Object
show all
Defined in:
lib/rom/factory/attributes/association.rb

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.

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from ROM::Factory::Attributes::Association::Core

Instance Method Details

#call(attrs, persist: true) ⇒ 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.

rubocop:disable Metrics/AbcSize



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
# File 'lib/rom/factory/attributes/association.rb', line 57

def call(attrs, persist: true)
  return if attrs.key?(name) && attrs[name].nil?

  assoc_data = attrs.fetch(name, EMPTY_HASH)

  if assoc_data.is_a?(::Hash) && assoc_data[assoc.target.primary_key] && !attrs[foreign_key]
    assoc.associate(attrs, attrs[name])
  elsif assoc_data.is_a?(::ROM::Struct)
    assoc.associate(attrs, assoc_data)
  else
    parent =
      if persist && !attrs[foreign_key]
        builder.persistable.create(*parent_traits, **assoc_data)
      else
        builder.struct(
          *parent_traits,
          **assoc_data, assoc.target.primary_key => attrs[foreign_key]
        )
      end

    tuple = {name => parent}

    assoc.associate(tuple, parent)
  end
end