Class: GroupCategoryNotificationDefault

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/group_category_notification_default.rb

Class Method Summary collapse

Class Method Details

.batch_set(group, level, category_ids) ⇒ Object



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

  # Update pre-existing
  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

  # Remove extraneous category users
  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

    # we could use VALUES here but it would introduce a string
    # into the query, plus it is a bit of a micro optimisation
    category_ids.each do |category_id|
      params[:category_id] = category_id
      changed = true if DB.exec(sql, params) > 0
    end
  end

  changed
end

.lookup(group, level) ⇒ Object



11
12
13
# File 'app/models/group_category_notification_default.rb', line 11

def self.lookup(group, level)
  self.where(group: group, notification_level: notification_levels[level])
end

.notification_levelsObject



7
8
9
# File 'app/models/group_category_notification_default.rb', line 7

def self.notification_levels
  NotificationLevels.all
end