lib/rom/sql/plugin/associates.rb in rom-sql-1.3.5 vs lib/rom/sql/plugin/associates.rb in rom-sql-2.0.0.beta1

- old
+ new

@@ -1,60 +1,39 @@ +require 'rom/sql/associations' + module ROM module SQL module Plugin # Make a command that automaticaly sets FK attribute on input tuples # # @api private module Associates - class MissingJoinKeysError < StandardError - ERROR_TEMPLATE = ':%{command} command for :%{relation} relation ' \ - 'is missing join keys configuration for :%{name} association' - - def initialize(command, assoc_name) - super(ERROR_TEMPLATE % tokens(command, assoc_name)) - end - - def tokens(command, assoc_name) - { command: command.register_as, - relation: command.relation, - name: assoc_name } - end - end - class AssociateOptions attr_reader :name, :assoc, :opts def initialize(name, relation, opts) @name = name - @opts = { assoc: name, keys: opts[:key] } - - relation.associations.try(name) do |assoc| - @assoc = assoc - @opts.update(assoc: assoc, keys: assoc.join_keys(relation.__registry__)) - end - + @assoc = relation.associations[name] + @opts = { assoc: assoc, keys: assoc.join_keys } @opts.update(parent: opts[:parent]) if opts[:parent] end def after? - assoc.is_a?(Association::ManyToMany) + assoc.is_a?(SQL::Associations::ManyToMany) end - def ensure_valid(command) - raise MissingJoinKeysError.new(command, name) unless opts[:keys] - end - def to_hash { associate: opts } end end # @api private def self.included(klass) klass.class_eval do extend ClassMethods include InstanceMethods + defines :associations associations Hash.new option :associations, default: -> { self.class.associations } @@ -75,12 +54,10 @@ associate_options = command.associations.map { |(name, opts)| next if configured_assocs.include?(name) AssociateOptions.new(name, relation, opts) }.compact - associate_options.each { |opts| opts.ensure_valid(self) } - before_hooks = associate_options.reject(&:after?).map(&:to_hash) after_hooks = associate_options.select(&:after?).map(&:to_hash) command. with_opts(configured_associations: configured_assocs + associate_options.map(&:name)). @@ -131,34 +108,28 @@ def associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent) 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 + when SQL::Associations::ManyToMany result_type = tuples.is_a?(Array) ? :many : :one - assoc.persist(__registry__, tuples, parent) + assoc.persist(tuples, parent) - pk, fk = assoc.parent_combine_keys(__registry__) + pk, fk = assoc.parent_combine_keys case parent when Array parent.map do |p| tuples.map { |tuple| Hash(tuple).merge(fk => p[pk]) } end.flatten(1) else tuples.map { |tuple| Hash(tuple).update(fk => parent[pk]) } end - when Association + else with_input_tuples(tuples).map { |tuple| - assoc.associate(__registry__, tuple, parent) + assoc.associate(tuple, parent) } end result_type == :one ? output_tuples[0] : output_tuples end @@ -168,14 +139,9 @@ self.class.build( relation, **options, associations: associations.merge(name => opts) ) - end - - # @api private - def __registry__ - relation.__registry__ end end end end end