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 Method Summary collapse

Instance Method Details

#__registry__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.



102
103
104
# File 'lib/rom/sql/plugin/associates.rb', line 102

def __registry__
  relation.__registry__
end

#associate(tuples, parent, assoc:, keys:) ⇒ Array<Hash>

Set fk on tuples from parent tuple



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
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rom/sql/plugin/associates.rb', line 47

def associate(tuples, parent, assoc:, keys:)
  result_type = result

  output_tuples =
    case assoc
    when Symbol
      fk, pk = keys

      with_input_tuples(tuples).map { |tuple|
        tuple.merge(fk => parent.fetch(pk))
      }
    when Association::ManyToMany
      result_type = tuples.is_a?(Array) ? :many : :one

      join_tuples = assoc.associate(__registry__, 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

      case parent
      when Array
        parent.map do |p|
          tuples.map { |tuple| tuple.merge(fk => p[pk]) }
        end.flatten(1)
      else
        tuples.map { |tuple| Hash(tuple).update(fk => parent[pk]) }
      end
    when Association
      with_input_tuples(tuples).map { |tuple|
        assoc.associate(relation.__registry__, tuple, parent)
      }
    end

  result_type == :one ? output_tuples[0] : output_tuples
end

#associations_configured?Boolean

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.



93
94
95
96
97
98
99
# File 'lib/rom/sql/plugin/associates.rb', line 93

def associations_configured?
  if configured_associations.empty?
    false
  else
    configured_associations.all? { |name| associations.key?(name) }
  end
end

#with_association(name, opts = EMPTY_HASH) ⇒ Object



87
88
89
90
91
# File 'lib/rom/sql/plugin/associates.rb', line 87

def with_association(name, opts = EMPTY_HASH)
  self.class.build(
    relation, options.merge(associations: associations.merge(name => opts))
  )
end