15
16
17
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
48
49
50
51
52
53
54
55
56
|
# File 'app/models/group_category_notification_default.rb', line 15
def self.batch_set(group, level, category_ids)
level_num = notification_levels[level]
category_ids = Category.where(id: category_ids).pluck(:id)
changed = false
if category_ids.present? &&
GroupCategoryNotificationDefault
.where(group_id: group.id, category_id: category_ids)
.where.not(notification_level: level_num)
.update_all(notification_level: level_num) > 0
changed = true
end
if GroupCategoryNotificationDefault
.where(group_id: group.id, notification_level: level_num)
.where.not(category_id: category_ids)
.delete_all > 0
changed = true
end
if category_ids.present?
params = { group_id: group.id, level_num: level_num }
sql = <<~SQL
INSERT INTO group_category_notification_defaults (group_id, category_id, notification_level)
SELECT :group_id, :category_id, :level_num
ON CONFLICT DO NOTHING
SQL
category_ids.each do |category_id|
params[:category_id] = category_id
changed = true if DB.exec(sql, params) > 0
end
end
changed
end
|