Class: ROM::SQL::Association::ManyToMany
- Inherits:
-
ROM::SQL::Association
- Object
- ROM::SQL::Association
- ROM::SQL::Association::ManyToMany
- Defined in:
- lib/rom/sql/association/many_to_many.rb
Direct Known Subclasses
Instance Method Summary collapse
- #associate(relations, children, parent) ⇒ Object private
- #call(relations, target_rel = nil) ⇒ Object
- #combine_keys(relations) ⇒ Object
-
#initialize ⇒ ManyToMany
constructor
private
A new instance of ManyToMany.
- #join(relations, type, source = , target = ) ⇒ Object
- #join_keys(relations) ⇒ Object
- #join_relation(relations) ⇒ Object private
Constructor Details
#initialize ⇒ ManyToMany
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of ManyToMany.
12 13 14 15 16 17 |
# File 'lib/rom/sql/association/many_to_many.rb', line 12 def initialize(*) super @through = Relation::Name[ [:through] || [:through_relation], [:through] ] end |
Instance Method Details
#associate(relations, children, parent) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rom/sql/association/many_to_many.rb', line 71 def associate(relations, children, parent) ((spk, sfk), (tfk, tpk)) = join_key_map(relations) case parent when Array parent.map { |p| associate(relations, children, p) }.flatten(1) else children.map { |tuple| { sfk => tuple.fetch(spk), tfk => parent.fetch(tpk) } } end end |
#call(relations, target_rel = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rom/sql/association/many_to_many.rb', line 20 def call(relations, target_rel = nil) join_rel = join_relation(relations) assocs = join_rel.associations left = target_rel ? assocs[target].(relations, target_rel) : assocs[target].(relations) right = relations[target.relation] left_fk = foreign_key || join_rel.foreign_key(source.relation) schema = if left.schema.key?(left_fk) if target_rel target_rel.schema.merge(left.schema.project(left_fk)) else left.schema.project(*(right.schema.map(&:name) + [left_fk])) end else right.schema.merge(join_rel.schema.project(left_fk)) end.qualified relation = left .inner_join(source, join_keys(relations)) .order(*right.schema.project_pk.qualified) if view apply_view(schema, relation) else schema.(relation) end end |
#combine_keys(relations) ⇒ Object
66 67 68 |
# File 'lib/rom/sql/association/many_to_many.rb', line 66 def combine_keys(relations) Hash[*with_keys(relations)] end |
#join(relations, type, source = , target = ) ⇒ Object
52 53 54 55 56 |
# File 'lib/rom/sql/association/many_to_many.rb', line 52 def join(relations, type, source = relations[self.source], target = relations[self.target]) through_assoc = source.associations[through] joined = through_assoc.join(relations, type, source) joined.__send__(type, target.name.dataset, join_keys(relations)).qualified end |
#join_keys(relations) ⇒ Object
59 60 61 62 63 |
# File 'lib/rom/sql/association/many_to_many.rb', line 59 def join_keys(relations) with_keys(relations) { |source_key, target_key| { qualify(source, source_key) => qualify(through, target_key) } } end |
#join_relation(relations) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
85 86 87 |
# File 'lib/rom/sql/association/many_to_many.rb', line 85 def join_relation(relations) relations[through.relation] end |