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
- #parent_combine_keys(relations) ⇒ Object private
- #persist(relations, children, parents) ⇒ 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.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rom/sql/association/many_to_many.rb', line 81 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 |
# 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)) if view apply_view(schema, relation) else schema.(relation) end end |
#combine_keys(relations) ⇒ Object
76 77 78 |
# File 'lib/rom/sql/association/many_to_many.rb', line 76 def combine_keys(relations) Hash[*with_keys(relations)] end |
#join(relations, type, source = , target = ) ⇒ Object
62 63 64 65 66 |
# File 'lib/rom/sql/association/many_to_many.rb', line 62 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
69 70 71 72 73 |
# File 'lib/rom/sql/association/many_to_many.rb', line 69 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.
95 96 97 |
# File 'lib/rom/sql/association/many_to_many.rb', line 95 def join_relation(relations) relations[through.relation] end |
#parent_combine_keys(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.
57 58 59 |
# File 'lib/rom/sql/association/many_to_many.rb', line 57 def parent_combine_keys(relations) relations[target].associations[source].combine_keys(relations).to_a.flatten(1) end |
#persist(relations, children, parents) ⇒ 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.
50 51 52 53 54 |
# File 'lib/rom/sql/association/many_to_many.rb', line 50 def persist(relations, children, parents) join_tuples = associate(relations, children, parents) join_relation = join_relation(relations) join_relation.multi_insert(join_tuples) end |