module Negroni::Mailers::Helpers

Common helpers for mailers

Attributes

resource[R]

@!attribute [r] resource

@return [Object, #email] the resource object that we are mailing to
scope_name[R]

@!attribute [r] scope_name

@return [String] the name of the record's class, underscored

Protected Instance Methods

headers_for(action, opts = {}) click to toggle source

Generate a set of mail headers for a given action

@param action [String, Symbol, to_s] The mail action that will occur @param opts [Hash] a hash of additional options to merge with the

headers
# File lib/negroni/mailers/helpers.rb, line 45
def headers_for(action, opts = {})
  headers = {
    subject: subject_for(action),
    to: resource.email,
    from: mailer_from,
    reply_to: mailer_reply_to,
    template_path: template_paths,
    template_name: action
  }.merge(opts)

  @email = headers[:to]
  headers
end
initialize_from_record(record) click to toggle source

Initialize the including class with a record

@param record [Object] the record to send the email to

# File lib/negroni/mailers/helpers.rb, line 35
def initialize_from_record(record)
  @scope_name = record.class.to_s.underscore
  @resource = instance_variable_set("@#{scope_name}", record)
end
mailer_from() click to toggle source
# File lib/negroni/mailers/helpers.rb, line 63
def mailer_from
  mailer_sender(:from)
end
mailer_reply_to() click to toggle source
# File lib/negroni/mailers/helpers.rb, line 59
def mailer_reply_to
  mailer_sender(:reply_to)
end
mailer_sender(sender = :from) click to toggle source
# File lib/negroni/mailers/helpers.rb, line 67
def mailer_sender(sender = :from)
  default_sender = default_params[sender]

  unless default_sender.present?
    ms = Negroni.mailer_sender
    return ms.is_a?(Proc) ? instance_eval(&ms) : ms
  end

  if default_sender.respond_to?(:to_proc)
    instance_eval(&default_sender)
  else
    default_sender
  end
end
negroni_mail(record, action, opts = {}, &block) click to toggle source

Configure default email options

@param record [Object] the object (record) that we are mailing to @param action [Symbol] the mail action to perform @param opts [Hash] a hash of options that will be sent to `headers_for`

# File lib/negroni/mailers/helpers.rb, line 26
def negroni_mail(record, action, opts = {}, &block)
  initialize_from_record(record)
  mail headers_for(action, opts), &block
end
subject_for(key) click to toggle source

Set up a subject doing an I18n lookup. At first, it attempts to set a subject based on the current mapping:

en:
  negroni:
    mailer:
      confirmation_instructions:
        user_subject: '...'

If one does not exist, it fallbacks to ActionMailer default:

en:
  negroni:
    mailer:
      confirmation_instructions:
        subject: '...'

@param key [Symbol, String] the key to lookup for I18n @return [String]

# File lib/negroni/mailers/helpers.rb, line 105
def subject_for(key)
  I18n.t(:"#{@scope_name}_subject",
         scope: [:negroni, :mailer, key],
         default: [:subject, key.to_s.humanize])
end
template_paths() click to toggle source
# File lib/negroni/mailers/helpers.rb, line 82
def template_paths
  _prefixes.dup
end