Module: Motor::Notes::NotifyReminder

Defined in:
lib/motor/notes/notify_reminder.rb

Constant Summary collapse

NOTIFICATION_DESCRIPTION_LIMIT =
100

Class Method Summary collapse

Class Method Details

.build_notification_title(note) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/motor/notes/notify_reminder.rb', line 37

def build_notification_title(note)
  configs = Motor::BuildSchema.for_model(note.record.class)

  display_value = note.record.attributes[configs['display_column']]

  I18n.t('motor.new_reminder_for',
         resource: ["#{configs['display_name'].singularize} ##{note.record[note.record.class.primary_key]}",
                    display_value].join(' '))
end

.call(reminder) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/motor/notes/notify_reminder.rb', line 10

def call(reminder)
  notification = find_or_build_notification(reminder)

  return if notification.persisted?

  notification.save!

  Motor::NotificationsMailer.notify_reminder_email(notification).deliver_later!
  Motor::NotificationsChannel.broadcast_to(reminder.recipient,
                                           ['notify', notification.as_json(include: %i[record])])
end

.find_or_build_notification(reminder) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/motor/notes/notify_reminder.rb', line 22

def find_or_build_notification(reminder)
  notification = reminder.notifications.take

  return notification if notification

  note = reminder.record

  Motor::Notification.new(
    title: build_notification_title(note),
    description: note.body.truncate(NOTIFICATION_DESCRIPTION_LIMIT),
    recipient: reminder.recipient,
    record: reminder
  )
end