lib/rom/sql/association/many_to_many.rb in rom-sql-0.9.1 vs lib/rom/sql/association/many_to_many.rb in rom-sql-1.0.0.beta1
- old
+ new
@@ -1,14 +1,14 @@
+require 'rom/types'
+
module ROM
module SQL
class Association
class ManyToMany < Association
- attr_reader :through
-
result :many
- option :through, default: nil, type: Symbol
+ option :through, type: Types::Strict::Symbol.optional
# @api private
def initialize(*)
super
@through = Relation::Name[
@@ -22,21 +22,28 @@
assocs = join_rel.associations
left = assocs[target].call(relations)
right = relations[target.relation]
- left_fk = join_rel.foreign_key(source.relation)
+ left_fk = foreign_key || join_rel.foreign_key(source.relation)
- columns = right.header.exclude(left_fk).qualified.to_a
- columns << left_fk unless right.header.names.include?(left_fk)
+ schema =
+ if left.schema.key?(left_fk)
+ left.schema.project(*(right.schema.map(&:name) + [left_fk]))
+ else
+ right.schema.merge(join_rel.schema.project(left_fk))
+ end.qualified
relation = left
.inner_join(source, join_keys(relations))
- .select(*columns)
- .order(*right.header.project(*right.primary_key).qualified)
+ .order(*right.schema.project_pk.qualified)
- relation.with(attributes: relation.header.names)
+ if view
+ schema.(relation.public_send(view))
+ else
+ schema.(relation)
+ end
end
# @api public
def join_keys(relations)
with_keys(relations) { |source_key, target_key|
@@ -66,10 +73,10 @@
protected
# @api private
def with_keys(relations, &block)
source_key = relations[source.relation].primary_key
- target_key = relations[through.relation].foreign_key(source.relation)
+ target_key = foreign_key || relations[through.relation].foreign_key(source.relation)
return [source_key, target_key] unless block
yield(source_key, target_key)
end
# @api private