Module: ROM::SQL::Plugin::Associates::InstanceMethods Private

Defined in:
lib/rom/sql/plugin/associates.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__registry__Object (readonly)

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.



17
18
19
# File 'lib/rom/sql/plugin/associates.rb', line 17

def __registry__
  @__registry__
end

#assocObject (readonly)

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.



17
18
19
# File 'lib/rom/sql/plugin/associates.rb', line 17

def assoc
  @assoc
end

Instance Method Details

#SQL::Commands::CreateArray<Hash>, Hash

Set fk on tuples from parent tuple

Parameters:

  • tuples (Array<Hash>, Hash)

    The input tuple(s)

  • parent (Hash)

    The parent tuple with its pk already set

Returns:

  • (Array<Hash>, Hash)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rom/sql/plugin/associates.rb', line 42

def execute(tuples, parent)
  input_tuples =
    case assoc
    when Array
      fk, pk = assoc

      input_tuples = with_input_tuples(tuples).map { |tuple|
        tuple.merge(fk => parent.fetch(pk))
      }

      super(input_tuples)
    when Association::ManyToMany
      new_tuples = super(tuples)

      join_tuples = assoc.associate(__registry__, new_tuples, parent)
      join_relation = assoc.join_relation(__registry__)
      join_relation.multi_insert(join_tuples)

      pk, fk = __registry__[assoc.target]
        .associations[assoc.source]
        .combine_keys(__registry__).to_a.flatten

      pk_extend = { fk => parent[pk] }

      new_tuples.map { |tuple| tuple.update(pk_extend) }
    when Association
      input_tuples = with_input_tuples(tuples).map { |tuple|
        assoc.associate(relation.__registry__, tuple, parent)
      }
      super(input_tuples)
    end
end

#initializeObject

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.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rom/sql/plugin/associates.rb', line 20

def initialize(*)
  super
  @__registry__ = relation.__registry__
  assoc_name, assoc_opts = self.class.associations[0]
  @assoc =
    if assoc_opts.any?
      assoc_opts[:key]
    else
      relation.associations[assoc_name]
    end
end