Module: OpenLoam::Notifications

Defined in:
lib/open_loam/notifications.rb

Overview

The one way to tell someone something inside the app.

OpenLoam::Notifications.notify(user, title: "Order approved", source: order)
OpenLoam::Notifications.notify_role(:manager, title: "New damage report")

Records land in the CURRENT tenant (OpenLoam::Notification is tenant-scoped), so a notification can never be delivered across a tenant boundary.

The intended pattern is event -> notification: subscribe to a domain event in config/initializers/open_loam.rb and notify from there, instead of scattering delivery calls through models and controllers.

Class Method Summary collapse

Class Method Details

.notify(recipients, title:, body: nil, source: nil) ⇒ Object

Returns the created notifications. Accepts one user or many; source is any record the message is about, stored as type + id.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/open_loam/notifications.rb', line 16

def self.notify(recipients, title:, body: nil, source: nil)
  Array(recipients).map do |user|
    Notification.create!(
      user: user,
      title: title,
      body: body,
      source_type: source&.class&.name,
      source_id: source&.id
    )
  end
end

.notify_role(role, title:, body: nil, source: nil) ⇒ Object

Everyone holding role in the current tenant. Memberships are tenant-scoped, so this cannot reach into another tenant's staff.



30
31
32
33
34
# File 'lib/open_loam/notifications.rb', line 30

def self.notify_role(role, title:, body: nil, source: nil)
  recipients = Membership.where(role: role.to_s).includes(:user).map(&:user)

  notify(recipients, title: title, body: body, source: source)
end