Class: NotificationRecipients::Builder::Base

Inherits:
Object
  • Object
show all
Defined in:
app/services/notification_recipients/builder/base.rb

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.


6
7
8
# File 'app/services/notification_recipients/builder/base.rb', line 6

def initialize(*)
  raise 'abstract'
end

Instance Method Details

#acting_userObject


18
19
20
# File 'app/services/notification_recipients/builder/base.rb', line 18

def acting_user
  current_user
end

#add_recipients(users, type, reason) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


44
45
46
47
48
49
50
51
# File 'app/services/notification_recipients/builder/base.rb', line 44

def add_recipients(users, type, reason)
  if users.is_a?(ActiveRecord::Relation)
    users = users.includes(:notification_settings)
  end

  users = Array(users).compact
  recipients.concat(users.map { |u| make_recipient(u, type, reason) })
end

#build!Object


10
11
12
# File 'app/services/notification_recipients/builder/base.rb', line 10

def build!
  raise 'abstract'
end

#custom_actionObject


82
83
84
# File 'app/services/notification_recipients/builder/base.rb', line 82

def custom_action
  nil
end

#filter!Object


14
15
16
# File 'app/services/notification_recipients/builder/base.rb', line 14

def filter!
  recipients.select!(&:notifiable?)
end

#groupObject


35
36
37
# File 'app/services/notification_recipients/builder/base.rb', line 35

def group
  project&.group || target.try(:group)
end

#make_recipient(user, type, reason) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord


60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/notification_recipients/builder/base.rb', line 60

def make_recipient(user, type, reason)
  NotificationRecipient.new(
    user, type,
    reason: reason,
    project: project,
    group: group,
    custom_action: custom_action,
    target: recipients_target,
    acting_user: acting_user
  )
end

#notification_recipientsObject


72
73
74
75
76
77
78
79
80
# File 'app/services/notification_recipients/builder/base.rb', line 72

def notification_recipients
  @notification_recipients ||=
    begin
      build!
      filter!
      recipients = self.recipients.sort_by { |r| NotificationReason.priority(r.reason) }.uniq(&:user)
      recipients.freeze
    end
end

#projectObject


31
32
33
# File 'app/services/notification_recipients/builder/base.rb', line 31

def project
  target.project
end

#recipientsObject


39
40
41
# File 'app/services/notification_recipients/builder/base.rb', line 39

def recipients
  @recipients ||= []
end

#recipients_targetObject

override if needed


27
28
29
# File 'app/services/notification_recipients/builder/base.rb', line 27

def recipients_target
  target
end

#targetObject


22
23
24
# File 'app/services/notification_recipients/builder/base.rb', line 22

def target
  raise 'abstract'
end

#user_scopeObject

rubocop: disable CodeReuse/ActiveRecord


55
56
57
# File 'app/services/notification_recipients/builder/base.rb', line 55

def user_scope
  User.includes(:notification_settings)
end