lib/rom/sql/plugin/associates.rb in rom-sql-1.0.0.beta3 vs lib/rom/sql/plugin/associates.rb in rom-sql-1.0.0.rc1
- old
+ new
@@ -3,10 +3,25 @@
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
+
# @api private
def self.included(klass)
klass.class_eval do
extend ClassMethods
include InstanceMethods
@@ -53,10 +68,12 @@
when Association
with_input_tuples(tuples).map { |tuple|
assoc.associate(relation.__registry__, tuple, parent)
}
end
+
+ one? ? input_tuples[0] : input_tuples
end
# @api public
def with_association(name, opts = EMPTY_HASH)
self.class.build(
@@ -112,9 +129,13 @@
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)