19
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
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/functions/one_to_one.rb', line 19
def call(list, assoc_dao, **opts)
return if list.empty?
dto_class = assoc_dao
.opts[:schema_mapper]
.dto(:db_load)
assoc_name = underscore(demodulize(dto_class.name))
reverse = opts[:reverse]
assoc_setter = if opts.key?(:assoc_setter)
opts[:assoc_setter]
else
"set_#{assoc_name}"
end
foreign_key = if opts.key?(:foreign_key)
opts[:foreign_key]
else
if reverse
name = underscore(demodulize(list.first.class.name))
"#{name}_id".to_sym
else
:id
end
end
root_ids = if reverse
list.map(&:id)
else
list.map(&:"#{foreign_key}")
end
assoc_by_fk = index_by(assoc_dao.where(foreign_key => root_ids).all) { _1.send(foreign_key) }
list.each do |item|
item.send(assoc_setter, assoc_by_fk[item.id])
end
nil
end
|