module ROM::SQL::Plugin::Associates::ClassMethods

@api public

Public Instance Methods

associates(name, options = EMPTY_HASH) click to toggle source

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

@example

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

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

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

command = create_user >> create_tasks
command.call

@param [Symbol] name The name of associated table @param [Hash] options The options @option options [Array] :key The association keys

@api public

# File lib/rom/sql/plugin/associates.rb, line 93
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) click to toggle source

@see ROM::Command::ClassInterface.build

@api public

Calls superclass method
# File lib/rom/sql/plugin/associates.rb, line 53
def build(relation, **options)
  command = super

  configured_assocs = command.configured_associations

  associate_options = command.associations.map { |(name, opts)|
    next if configured_assocs.include?(name)
    AssociateOptions.new(name, relation, opts)
  }.compact

  before_hooks = associate_options.reject(&:after?).map(&:to_hash)
  after_hooks = associate_options.select(&:after?).map(&:to_hash)

  command.
    with(configured_associations: configured_assocs + associate_options.map(&:name)).
    before(*before_hooks).
    after(*after_hooks)
end