Sha256: 516bf5164c35a460dd502d3e38177522803c76aec49e49895d2c5b9a71275c4e

Contents?: true

Size: 1.39 KB

Versions: 53

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Motor
  module Notes
    module NotifyReminder
      NOTIFICATION_DESCRIPTION_LIMIT = 100

      module_function

      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

      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

      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
    end
  end
end

Version data entries

53 entries across 53 versions & 6 rubygems

Version Path
motor-admin-0.4.36 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.35 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.34 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.33 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.32 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.31 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.30 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.29 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.28 lib/motor/notes/notify_reminder.rb
motor-admin-cstham8-0.4.35 lib/motor/notes/notify_reminder.rb
motor-admin-cstham8-0.4.34 lib/motor/notes/notify_reminder.rb
motor-admin-cstham8-0.4.33 lib/motor/notes/notify_reminder.rb
motor-admin-cstham8-0.4.32 lib/motor/notes/notify_reminder.rb
motor-admin-cstham8-0.4.31 lib/motor/notes/notify_reminder.rb
motor-admin-cstham8-0.4.30 lib/motor/notes/notify_reminder.rb
motor-admin-cstham8-0.4.29 lib/motor/notes/notify_reminder.rb
motor-admin-cstham8-0.4.28 lib/motor/notes/notify_reminder.rb
motor-admin-cstham8-0.4.27 lib/motor/notes/notify_reminder.rb
motor-admin-0.4.27 lib/motor/notes/notify_reminder.rb
motor-admin-unosquare-0.4.28 lib/motor/notes/notify_reminder.rb