class Outbox::Notifier

Public Class Methods

defaults(value = nil) click to toggle source

Sets the default options for a message for this Notifier and its descendants. This is similar to ActionMailer's `default` method, but allows you to set options for multiple message types at once:

class UsersNotifier < OutboxNotifier
  defaults email: { from: 'noreply@myapp.com' }, sms: { from: '+12255551234' }
end
# File lib/outbox/notifier.rb, line 26
def defaults(value = nil)
  self.default_message_options = default_message_options.merge(value).freeze if value
  default_message_options
end
notifier_name(value = nil) click to toggle source

Returns the name of current notifier. This method is also being used as a path for a view lookup. If this is an anonymous notifier, this method will return anonymous instead.

# File lib/outbox/notifier.rb, line 34
def notifier_name(value = nil)
  if value.nil?
    mailer_name
  else
    self.mailer_name = value
  end
end
Also aliased as: notifier_name=
notifier_name=(value = nil)
Alias for: notifier_name

Public Instance Methods

attachments() click to toggle source
# File lib/outbox/notifier.rb, line 102
def attachments
  # Make sure the email message instance exists
  email({}) if email.nil?
  email.attachments
end
message() click to toggle source

The composed Outbox::Message instance.

# File lib/outbox/notifier.rb, line 71
def message
  render_message unless message_rendered?
  @_message
end
message_rendered?() click to toggle source

Returns true if the message has already been rendered.

# File lib/outbox/notifier.rb, line 77
def message_rendered?
  @_message_rendered
end
render_message(options = {}, &block) click to toggle source

Renders the message body. This is analagous to ActionMailer's mail method, but is not required - it will be called implicitly when the message object is retrieved.

# File lib/outbox/notifier.rb, line 84
def render_message(options = {}, &block)
  @_message_rendered = true
  render_email(@_message.email, options, &block) if @_message.email
  render_message_types(options)
  @_message.assign_message_type_values(options)
  @_message
end

Protected Instance Methods

assign_body(body, only_message_types = nil) click to toggle source
# File lib/outbox/notifier.rb, line 160
def assign_body(body, only_message_types = nil)
  only_message_types =
    if only_message_types
      only_message_types.map(&:to_sym)
    else
      message_types_without_email
    end
  @_message.each_message_type do |message_type, message|
    message.body = body if message && message.body.nil? && message_type.in?(only_message_types)
  end
end
build_message() click to toggle source
# File lib/outbox/notifier.rb, line 114
def build_message
  message = Outbox::Message.new(self.class.default_message_options.dup)
  Outbox::Message.message_types.each_key do |message_type|
    message.public_send(message_type, {})
  end
  message
end
details_for_lookup() click to toggle source
# File lib/outbox/notifier.rb, line 110
def details_for_lookup
  { variants: [:email] }
end
find_message_type_templates(options) click to toggle source
# File lib/outbox/notifier.rb, line 150
def find_message_type_templates(options)
  template_path = options[:template_path] || self.class.mailer_name
  template_name = options[:template_name] || action_name
  lookup_context.find_all(
    template_name, Array(template_path), false, [],
    formats: [:text],
    variants: message_types_without_email
  )
end
message_types() click to toggle source
# File lib/outbox/notifier.rb, line 172
def message_types
  Outbox::Message.message_types.keys
end
message_types_without_email() click to toggle source
# File lib/outbox/notifier.rb, line 176
def message_types_without_email
  message_types - [:email]
end
render_email(email, options, &block) click to toggle source
# File lib/outbox/notifier.rb, line 122
def render_email(email, options, &block)
  email_options_keys = %i[content_type charset parts_order body template_name template_path]
  email_options = options.select { |key, _| email_options_keys.include? key }
  email_options.merge!(options.delete(:email)) if options[:email]
  # ActionMailer will use the default i18n subject
  # unless we explicitly set it on this options hash.
  email_options[:subject] ||= email.subject if email.subject

  outbox_message = @_message
  @_message = email
  _render_email(email_options, &block)
ensure
  @_message = outbox_message
  email
end
render_message_types(options) click to toggle source
# File lib/outbox/notifier.rb, line 138
def render_message_types(options)
  templates = find_message_type_templates(options)
  templates.each do |template|
    variants = template_variants(template)
    if variants.empty?
      assign_body(render(template: template))
    else
      assign_body(render(template: template, variants: variants), variants)
    end
  end
end
template_variants(template) click to toggle source
# File lib/outbox/notifier.rb, line 180
def template_variants(template)
  if template.respond_to?(:variant)
    [template.variant].compact
  elsif template.respond_to?(:variants)
    template.variants.compact
  else
    []
  end
end