Class: Rasti::DB::Relations::ManyToOne
- Inherits:
-
Base
- Object
- Base
- Rasti::DB::Relations::ManyToOne
show all
- Defined in:
- lib/rasti/db/relations/many_to_one.rb
Instance Attribute Summary
Attributes inherited from Base
#name, #source_collection_class
Instance Method Summary
collapse
Methods inherited from Base
#from_many?, #from_one?, #initialize, #join_relation_name, #many_to_many?, #many_to_one?, #one_to_many?, #one_to_one?, #target_collection_class, #to_many?, #to_one?
Instance Method Details
#add_join(environment, dataset, prefix = nil) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/rasti/db/relations/many_to_one.rb', line 29
def add_join(environment, dataset, prefix=nil)
validate_join!
relation_alias = join_relation_name prefix
relation_name = prefix ? Sequel[prefix] : Sequel[source_collection_class.collection_name]
relation_condition = {
Sequel[relation_alias][target_collection_class.primary_key] => relation_name[foreign_key]
}
dataset.join(environment.qualify_collection(target_collection_class).as(relation_alias), relation_condition)
end
|
#apply_filter(environment, dataset, primary_keys) ⇒ Object
43
44
45
|
# File 'lib/rasti/db/relations/many_to_one.rb', line 43
def apply_filter(environment, dataset, primary_keys)
dataset.where(foreign_key => primary_keys)
end
|
#fetch_graph(environment, rows, selected_attributes = nil, excluded_attributes = nil, relations_graph = nil) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/rasti/db/relations/many_to_one.rb', line 10
def fetch_graph(environment, rows, selected_attributes=nil, excluded_attributes=nil, relations_graph=nil)
fks = rows.map { |row| row[foreign_key] }.uniq
target_collection = target_collection_class.new environment
query = target_collection.where(source_collection_class.primary_key => fks)
query = query.exclude_attributes(*excluded_attributes) if excluded_attributes
query = query.select_attributes(*selected_attributes) if selected_attributes
query = relations_graph.apply_to query if relations_graph
relation_rows = query.each_with_object({}) do |row, hash|
hash[row.public_send(source_collection_class.primary_key)] = row
end
rows.each do |row|
row[name] = relation_rows[row[foreign_key]]
end
end
|
#foreign_key ⇒ Object
6
7
8
|
# File 'lib/rasti/db/relations/many_to_one.rb', line 6
def foreign_key
@foreign_key ||= options[:foreign_key] || target_collection_class.foreign_key
end
|