class Aerogel::Mailer::Definition

Constants

DEFAULT_LAYOUT

Attributes

blk[RW]
name[RW]
params[RW]

Public Class Methods

mailers() click to toggle source
# File lib/aerogel/mailer/definition.rb, line 106
def self.mailers
  @mailers || {}
end
new( name, blk ) click to toggle source
# File lib/aerogel/mailer/definition.rb, line 10
def initialize( name, blk )
  self.name = name.to_sym
  self.params = {}
  self.blk = blk

  self.class.register_mailer( self )
end
register_mailer( mailer ) click to toggle source
# File lib/aerogel/mailer/definition.rb, line 101
def self.register_mailer( mailer )
  @mailers ||= {}
  @mailers[mailer.name] = mailer
end

Public Instance Methods

body( args ) click to toggle source

Sets message body. Multiple calls to body are allowed, e.g. for setting plain text part and html part separately.

If message body is set via call to body, existing mailer templates and layout will be ignored.

args can be a String, which sets the text/plain message body or a Hash.

Example:

body "This is a plain text message"
body html: "This is a HTML only message"
body text: "This is a plain text", html: "and <b>HTML</b> message"
# File lib/aerogel/mailer/definition.rb, line 56
def body( args )
  params[:body] ||= {}
  if args.is_a? String
    params[:body][:text] = args
  elsif args.is_a? Hash
    params[:body][:html] = args[:html] if args.include? :html
    params[:body][:text] = args[:text] if args.include? :text
  else
    raise ArgumentError.new "Invalid argument #{args.class} to #body"
  end
end
compile( context, *args ) click to toggle source
# File lib/aerogel/mailer/definition.rb, line 81
def compile( context, *args )
  unless args.size == blk.arity
    raise Aerogel::Mailer::Error.new("wrong number of arguments for mailer '#{name}': #{args.size} for #{blk.arity}")
  end
  # @self_before_instance_eval = eval "self", blk.binding
  @self_before_instance_eval = context
  params.clear
  instance_exec( *args, &blk )
  params[:from] ||= config.mailer.default_from!
  if params[:from].nil?
    raise Aerogel::Mailer::Error.new("'from' address is not set for mailer '#{name}'")
  end
  render_body
  params
end
from( str ) click to toggle source
# File lib/aerogel/mailer/definition.rb, line 18
def from( str )
  params[:from] = str
end
layout( name ) click to toggle source

Sets layout name for text/plain and text/html layouts or disables layout for message body templates.

Example

layout false # disables layout for text and html message templates
layout 'mailer-admin' # sets layouts to 'views/layouts/mailer-admin.text.erb'
                     # and 'views/layouts/mailer-admin.html.erb'
# File lib/aerogel/mailer/definition.rb, line 38
def layout( name )
  params[:layout] = name
end
locals( args ) click to toggle source

Sets local variables to be passed to template. Multiple calls to locals are allowed, variables passed this way will be merged into one set before passing to a template.

Example:

locals user: current_user, url: url
locals order: order
# File lib/aerogel/mailer/definition.rb, line 76
def locals( args )
  params[:locals] ||= {}
  params[:locals].merge! args
end
method_missing( method, *args, &block ) click to toggle source
# File lib/aerogel/mailer/definition.rb, line 97
def method_missing( method, *args, &block )
  @self_before_instance_eval.send method, *args, &block
end
subject( str ) click to toggle source
# File lib/aerogel/mailer/definition.rb, line 26
def subject( str )
  params[:subject] = str
end
to( str ) click to toggle source
# File lib/aerogel/mailer/definition.rb, line 22
def to( str )
  params[:to] = str
end

Private Instance Methods

render_body() click to toggle source

Renders message body using filled params. Stores rendered body (text and html parts) into params hash.

# File lib/aerogel/mailer/definition.rb, line 134
def render_body
  if Aerogel.config.aerogel.reloader?
    TemplateNameCache.clear
    template_cache.clear
  end
  params[:body] ||= {}
  return unless params[:body].blank? # body set in the mailer definition block
  if params[:layout] == false
    layout_text = false
    layout_html = false
  else
    layout_name = params[:layout] || DEFAULT_LAYOUT
    layout_text = TemplateNameCache.fetch( "layouts/#{layout_name}.text" )
    layout_html = TemplateNameCache.fetch( "layouts/#{layout_name}.html" )
  end
  body_text = TemplateNameCache.fetch( "mailers/#{name}.text" )
  body_html = TemplateNameCache.fetch( "mailers/#{name}.html" )
  if !body_text && !body_html
    raise Aerogel::Mailer::Error.new "No body templates found for mailer '#{name}'"
  end
  if body_text
    params[:body][:text] = erb body_text, layout: layout_text, locals: params[:locals]
  end
  if body_html
    params[:body][:html] = erb body_html, layout: layout_html, locals: params[:locals]
  end
  true
end