Module: ROM::SQL::Plugin::Associates::ClassMethods 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

#associates(name, options = EMPTY_HASH) ⇒ Object

Set command to associate tuples with a parent tuple using provided keys

Examples:

class CreateTask < ROM::Commands::Create[:sql]
  relation :tasks
  associates :user, key: [:user_id, :id]
end

create_user = rom.command(:user).create.with(name: 'Jane')

create_tasks = rom.command(:tasks).create
  .with [{ title: 'One' }, { title: 'Two' } ]

command = create_user >> create_tasks
command.call

Parameters:

  • name (Symbol)

    The name of associated table

  • options (Hash) (defaults to: EMPTY_HASH)

    The options

Options Hash (options):

  • :key (Array)

    The association keys



175
176
177
178
179
180
181
182
# File 'lib/rom/sql/plugin/associates.rb', line 175

def associates(name, options = EMPTY_HASH)
  if associations.key?(name)
    raise ArgumentError,
          "#{name} association is already defined for #{self.class}"
  end

  associations(associations.merge(name => options))
end

#build(relation, options = EMPTY_HASH) ⇒ Object

See Also:

  • Command::ClassInterface.build


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rom/sql/plugin/associates.rb', line 111

def build(relation, options = EMPTY_HASH)
  command = super

  if command.associations_configured?
    return command
  end

  associations = command.associations
  assoc_names = []

  before_hooks = associations.each_with_object([]) do |(name, opts), acc|
    relation.associations.try(name) do |assoc|
      unless assoc.is_a?(Association::ManyToMany)
        acc << { associate: { assoc: assoc, keys: assoc.join_keys(relation.__registry__) } }
      else
        true
      end
    end or acc << { associate: { assoc: name, keys: opts[:key] } }

    assoc_names << name
  end

  after_hooks = associations.each_with_object([]) do |(name, opts), acc|
    next unless relation.associations.key?(name)

    assoc = relation.associations[name]

    if assoc.is_a?(Association::ManyToMany)
      acc << { associate: { assoc: assoc, keys: assoc.join_keys(relation.__registry__) } }
      assoc_names << name
    end
  end

  [*before_hooks, *after_hooks].
    map { |hook| hook[:associate] }.
    each { |conf| raise MissingJoinKeysError.new(self, conf[:assoc]) unless conf[:keys] }

  command.
    with_opts(configured_associations: assoc_names).
    before(*before_hooks).
    after(*after_hooks)
end