18
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
|
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/functions/one_to_many.rb', line 18
def call(list, assoc_dao, **opts)
return if list.empty?
root_ids = list.map(&:id)
assoc_setter = if opts.key?(:assoc_setter)
opts[:assoc_setter]
else
dto_class = assoc_dao
.opts[:schema_mapper]
.dto(:db_load)
name = underscore(demodulize(dto_class.name))
"set_#{name}s".to_sym
end
foreign_key = if opts.key?(:foreign_key)
opts[:foreign_key]
else
name = underscore(demodulize(list.first.class.name))
"#{name}_id".to_sym
end
assoc_by_group = group_by(assoc_dao.where(foreign_key => root_ids).all) { _1.send(foreign_key) }
list.each do |item|
item.send(assoc_setter, assoc_by_group[item.id] || [])
end
nil
end
|