Method: ROM::Factory::Attributes::Association::OneToOne#call

Defined in:
lib/rom/factory/attributes/association.rb

#call(attrs = EMPTY_HASH, parent, 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.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rom/factory/attributes/association.rb', line 104

def call(attrs = EMPTY_HASH, parent, persist: true)
  # do not associate if count is 0
  return { name => nil } if count.zero?

  return if attrs.key?(name)

  association_hash = assoc.associate(attrs, parent)

  struct = if persist
             builder.persistable.create(*traits, association_hash)
           else
             belongs_to_name = Dry::Core::Inflector.singularize(assoc.source_alias)
             belongs_to_associations = { belongs_to_name.to_sym => parent }
             final_attrs = attrs.merge(association_hash).merge(belongs_to_associations)
             builder.struct(*traits, final_attrs)
           end

  { name => struct }
end