class Noticed::Base

Attributes

recipient[RW]

Gives notifications access to the record and recipient when formatting for delivery

record[RW]

Gives notifications access to the record and recipient when formatting for delivery

Public Class Methods

deliver_by(name, options = {}) click to toggle source
# File lib/noticed/base.rb, line 18
def deliver_by(name, options = {})
  delivery_methods.push(name: name, options: options)
  define_model_callbacks(name)
end
new(params = {}) click to toggle source
# File lib/noticed/base.rb, line 40
def initialize(params = {})
  @params = params
end
param(*names)
Alias for: params
params(*names) click to toggle source
# File lib/noticed/base.rb, line 34
def params(*names)
  param_names.concat Array.wrap(names)
end
Also aliased as: param
with(params) click to toggle source
# File lib/noticed/base.rb, line 30
def with(params)
  new(params)
end

Public Instance Methods

deliver(recipients) click to toggle source
# File lib/noticed/base.rb, line 44
def deliver(recipients)
  validate!

  run_callbacks :deliver do
    Array.wrap(recipients).uniq.each do |recipient|
      run_delivery(recipient, enqueue: false)
    end
  end
end
deliver_later(recipients) click to toggle source
# File lib/noticed/base.rb, line 54
def deliver_later(recipients)
  validate!

  run_callbacks :deliver do
    Array.wrap(recipients).uniq.each do |recipient|
      run_delivery(recipient, enqueue: true)
    end
  end
end
params() click to toggle source
# File lib/noticed/base.rb, line 64
def params
  @params || {}
end

Private Instance Methods

delivery_method_for(name, options) click to toggle source
# File lib/noticed/base.rb, line 115
def delivery_method_for(name, options)
  if options[:class]
    options[:class].constantize
  else
    "Noticed::DeliveryMethods::#{name.to_s.camelize}".constantize
  end
end
run_delivery(recipient, enqueue: true) click to toggle source

Runs all delivery methods for a notification

# File lib/noticed/base.rb, line 71
def run_delivery(recipient, enqueue: true)
  delivery_methods = self.class.delivery_methods.dup

  # Set recipient to instance var so it is available to Notification class
  @recipient = recipient

  # Run database delivery inline first if it exists so other methods have access to the record
  if (index = delivery_methods.find_index { |m| m[:name] == :database })
    delivery_method = delivery_methods.delete_at(index)
    @record = run_delivery_method(delivery_method, recipient: recipient, enqueue: false)
  end

  delivery_methods.each do |delivery_method|
    run_delivery_method(delivery_method, recipient: recipient, enqueue: enqueue)
  end
end
run_delivery_method(delivery_method, recipient:, enqueue:) click to toggle source

Actually runs an individual delivery

# File lib/noticed/base.rb, line 89
def run_delivery_method(delivery_method, recipient:, enqueue:)
  args = {
    notification_class: self.class.name,
    options: delivery_method[:options],
    params: params,
    recipient: recipient,
    record: record
  }

  run_callbacks delivery_method[:name] do
    method = delivery_method_for(delivery_method[:name], delivery_method[:options])

    # If the queue is `nil`, ActiveJob will use a default queue name.
    queue = delivery_method.dig(:options, :queue)

    # Always perfrom later if a delay is present
    if (delay = delivery_method.dig(:options, :delay))
      method.set(wait: delay, queue: queue).perform_later(args)
    elsif enqueue
      method.set(queue: queue).perform_later(args)
    else
      method.perform_now(args)
    end
  end
end
validate!() click to toggle source
# File lib/noticed/base.rb, line 123
def validate!
  validate_params_present!
  validate_options_of_delivery_methods!
end
validate_options_of_delivery_methods!() click to toggle source
# File lib/noticed/base.rb, line 137
def validate_options_of_delivery_methods!
  delivery_methods.each do |delivery_method|
    method = delivery_method_for(delivery_method[:name], delivery_method[:options])
    method.validate!(delivery_method[:options])
  end
end
validate_params_present!() click to toggle source

Validates that all params are present

# File lib/noticed/base.rb, line 129
def validate_params_present!
  self.class.param_names.each do |param_name|
    if params[param_name].nil?
      raise ValidationError, "#{param_name} is missing."
    end
  end
end