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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rom/factory/attributes/association.rb', line 71

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.merge(assoc.target.primary_key => attrs[foreign_key])
               )
             end

    tuple = {name => parent}

    assoc.associate(tuple, parent)
  end
end