Module: UNotifier::UserNotifier

Defined in:
lib/unotifier/user_notifier.rb

Class Method Summary collapse

Class Method Details

.notify_external?(notification, user_settings) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/unotifier/user_notifier.rb', line 46

def self.notify_external?(notification, )
  case notification.urgency
  when "immediate"
    true
  when "regular", "optional"
     == "external" && !notification.target.online?
  when "onsite"
    false
  else
    false
  end
end

.notify_onsite?(notification, user_settings) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/unotifier/user_notifier.rb', line 40

def self.notify_onsite?(notification, )
  return false if notification.urgency == "optional" &&  == "off"

  notification.target.online?
end

.notify_user(notification, params = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/unotifier/user_notifier.rb', line 3

def self.notify_user(notification, params = {})
  locale_key = UNotifier.locale_key_for(notification.key, params)
   = notification.target.notification_settings[notification.key]
   ||= Settings::DEFAULT_URGENCY

  notify_with = []

  if !params[:external_only] && notify_onsite?(notification, )
    notify_with += UNotifier.configuration.site_providers
  end

  if !params[:onsite_only] && notify_external?(notification, )
    notify_with += UNotifier.configuration.external_providers
  end

  notify_with.each do |provider|
    title_provider_key = "#{locale_key}.#{provider.class.to_s.split("::").last}.title"
    body_provider_key  = "#{locale_key}.#{provider.class.to_s.split("::").last}.body"

    if UNotifier.configuration.localization_provider.exists?(title_provider_key)
      notification.title =
        UNotifier.configuration.localization_provider.t(
          title_provider_key, params.merge(locale: notification.target.locale)
        )
    end

    if UNotifier.configuration.localization_provider.exists?(body_provider_key)
      notification.body =
        UNotifier.configuration.localization_provider.t(
          body_provider_key, params.merge(locale: notification.target.locale, default: "")
        )
    end

    provider.notify(notification)
  end
end