Class: Aerogel::Mailer::Definition
- Inherits:
-
Object
- Object
- Aerogel::Mailer::Definition
- Includes:
- Render::Scope
- Defined in:
- lib/aerogel/mailer/definition.rb
Defined Under Namespace
Classes: TemplateNameCache
Constant Summary collapse
- DEFAULT_LAYOUT =
"mailer"
Instance Attribute Summary collapse
-
#blk ⇒ Object
Returns the value of attribute blk.
-
#name ⇒ Object
Returns the value of attribute name.
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
Instance Method Summary collapse
-
#body(args) ⇒ Object
Sets message body.
- #compile(context, *args) ⇒ Object
- #from(str) ⇒ Object
-
#initialize(name, blk) ⇒ Definition
constructor
A new instance of Definition.
-
#layout(name) ⇒ Object
Sets layout name for text/plain and text/html layouts or disables layout for message body templates.
-
#locals(args) ⇒ Object
Sets local variables to be passed to template.
- #method_missing(method, *args, &block) ⇒ Object
- #subject(str) ⇒ Object
- #to(str) ⇒ Object
Constructor Details
#initialize(name, blk) ⇒ Definition
Returns a new instance of Definition.
10 11 12 13 14 15 16 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
97 98 99 |
# File 'lib/aerogel/mailer/definition.rb', line 97 def method_missing( method, *args, &block ) @self_before_instance_eval.send method, *args, &block end |
Instance Attribute Details
#blk ⇒ Object
Returns the value of attribute blk.
6 7 8 |
# File 'lib/aerogel/mailer/definition.rb', line 6 def blk @blk end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/aerogel/mailer/definition.rb', line 6 def name @name end |
#params ⇒ Object
Returns the value of attribute params.
6 7 8 |
# File 'lib/aerogel/mailer/definition.rb', line 6 def params @params end |
Class Method Details
.mailers ⇒ Object
106 107 108 |
# File 'lib/aerogel/mailer/definition.rb', line 106 def self.mailers @mailers || {} end |
.register_mailer(mailer) ⇒ Object
101 102 103 104 |
# File 'lib/aerogel/mailer/definition.rb', line 101 def self.register_mailer( mailer ) @mailers ||= {} @mailers[mailer.name] = mailer end |
Instance Method Details
#body(args) ⇒ Object
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"
56 57 58 59 60 61 62 63 64 65 66 |
# 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) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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) ⇒ Object
18 19 20 |
# File 'lib/aerogel/mailer/definition.rb', line 18 def from( str ) params[:from] = str end |
#layout(name) ⇒ Object
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'
38 39 40 |
# File 'lib/aerogel/mailer/definition.rb', line 38 def layout( name ) params[:layout] = name end |
#locals(args) ⇒ Object
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
76 77 78 79 |
# File 'lib/aerogel/mailer/definition.rb', line 76 def locals( args ) params[:locals] ||= {} params[:locals].merge! args end |
#subject(str) ⇒ Object
26 27 28 |
# File 'lib/aerogel/mailer/definition.rb', line 26 def subject( str ) params[:subject] = str end |
#to(str) ⇒ Object
22 23 24 |
# File 'lib/aerogel/mailer/definition.rb', line 22 def to( str ) params[:to] = str end |