Class: GroupTagNotificationDefault

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

Class Method Summary collapse

Class Method Details

.batch_set(group, level, tag_names) ⇒ 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
# File 'app/models/group_tag_notification_default.rb', line 15

def self.batch_set(group, level, tag_names)
  tag_names ||= []
  changed = false

  records = self.where(group: group, notification_level: notification_levels[level])
  old_ids = records.pluck(:tag_id)

  tag_ids = tag_names.empty? ? [] : Tag.where_name(tag_names).pluck(:id)

  Tag
    .where_name(tag_names)
    .joins(:target_tag)
    .each { |tag| tag_ids[tag_ids.index(tag.id)] = tag.target_tag_id }

  tag_ids.uniq!

  remove = (old_ids - tag_ids)
  if remove.present?
    records.where("tag_id in (?)", remove).destroy_all
    changed = true
  end

  new_records_attrs =
    (tag_ids - old_ids).map do |tag_id|
      { group_id: group.id, tag_id: tag_id, notification_level: notification_levels[level] }
    end

  unless new_records_attrs.empty?
    result = GroupTagNotificationDefault.insert_all(new_records_attrs)
    changed = true if result.rows.length > 0
  end

  changed
end

.lookup(group, level) ⇒ Object



11
12
13
# File 'app/models/group_tag_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_tag_notification_default.rb', line 7

def self.notification_levels
  NotificationLevels.all
end