lib/roda/plugins/mailer.rb in roda-2.29.0 vs lib/roda/plugins/mailer.rb in roda-3.0.0
- old
+ new
@@ -5,18 +5,19 @@
class Roda
module RodaPlugins
# The mailer plugin allows your Roda application to send emails easily.
#
- # class App < Roda
+ # class Mailer < Roda
# plugin :render
# plugin :mailer
#
# route do |r|
- # r.on "albums" do
- # r.mail "added" do |album|
- # @album = album
+ # r.on "albums", Integer do |album_id|
+ # @album = Album[album_id]
+ #
+ # r.mail "added" do
# from 'from@example.com'
# to 'to@example.com'
# cc 'cc@example.com'
# bcc 'bcc@example.com'
# subject 'Album Added'
@@ -27,30 +28,30 @@
# end
# end
#
# The default method for sending a mail is +sendmail+:
#
- # App.sendmail("/albums/added", Album[1])
+ # Mailer.sendmail("/albums/1/added")
#
# If you want to return the <tt>Mail::Message</tt> instance for further modification,
# you can just use the +mail+ method:
#
- # mail = App.mail("/albums/added", Album[1])
+ # mail = Mailer.mail("/albums/1/added")
# mail.from 'from2@example.com'
# mail.deliver
#
# The mailer plugin uses the mail gem, so if you want to configure how
# email is sent, you can use <tt>Mail.defaults</tt> (see the mail gem documentation for
# more details):
#
# Mail.defaults do
- # delivery_method :smtp, :address=>'smtp.example.com', :port=>587
+ # delivery_method :smtp, address: 'smtp.example.com', port: 587
# end
#
# You can support multipart emails using +text_part+ and +html_part+:
#
- # r.mail "added" do |album_added|
+ # r.mail "added" do
# from 'from@example.com'
# to 'to@example.com'
# subject 'Album Added'
# text_part render('album_added.txt') # views/album_added.txt.erb
# html_part render('album_added.html') # views/album_added.html.erb
@@ -95,71 +96,60 @@
# r.mail 'welcome', Integer do |id|
# no_mail! unless user = User[id]
# # ...
# end
#
+ # You can pass arguments when calling +mail+ or +sendmail+, and they
+ # will be yielded as additional arguments to the appropriate +r.mail+ block:
+ #
+ # Mailer.sendmail('/welcome/1', 'foo@example.com')
+ #
+ # r.mail 'welcome' do |user_id, mail_from|
+ # from mail_from
+ # to User[user_id].email
+ # # ...
+ # end
+ #
# By default, the mailer uses text/plain as the Content-Type for emails.
# You can override the default by specifying a :content_type option when
# loading the plugin:
#
- # plugin :mailer, :content_type=>'text/html'
+ # plugin :mailer, content_type: 'text/html'
#
# The mailer plugin does support being used inside a Roda application
# that is handling web requests, where the routing block for mails and
# web requests is shared. However, it's recommended that you create a
# separate Roda application for emails. This can be a subclass of your main
# Roda application if you want your helper methods to automatically be
# available in your email views.
module Mailer
- OPTS = {}.freeze
- RodaPlugins.deprecate_constant(self, :OPTS)
- REQUEST_METHOD = "REQUEST_METHOD".freeze
- RodaPlugins.deprecate_constant(self, :REQUEST_METHOD)
- PATH_INFO = "PATH_INFO".freeze
- RodaPlugins.deprecate_constant(self, :PATH_INFO)
- SCRIPT_NAME = 'SCRIPT_NAME'.freeze
- RodaPlugins.deprecate_constant(self, :SCRIPT_NAME)
- EMPTY_STRING = ''.freeze
- RodaPlugins.deprecate_constant(self, :EMPTY_STRING)
- RACK_INPUT = 'rack.input'.freeze
- RodaPlugins.deprecate_constant(self, :RACK_INPUT)
- RODA_MAIL = 'roda.mail'.freeze
- RodaPlugins.deprecate_constant(self, :RODA_MAIL)
- RODA_MAIL_ARGS = 'roda.mail_args'.freeze
- RodaPlugins.deprecate_constant(self, :RODA_MAIL_ARGS)
- MAIL = "MAIL".freeze
- RodaPlugins.deprecate_constant(self, :MAIL)
- CONTENT_TYPE = 'Content-Type'.freeze
- RodaPlugins.deprecate_constant(self, :CONTENT_TYPE)
- TEXT_PLAIN = "text/plain".freeze
- RodaPlugins.deprecate_constant(self, :TEXT_PLAIN)
-
# Error raised when the using the mail class method, but the routing
# tree doesn't return the mail object.
class Error < ::Roda::RodaError; end
# Set the options for the mailer. Options:
# :content_type :: The default content type for emails (default: text/plain)
- def self.configure(app, opts=RodaPlugins::OPTS)
- app.opts[:mailer] = (app.opts[:mailer]||RodaPlugins::OPTS).merge(opts).freeze
+ def self.configure(app, opts=OPTS)
+ app.opts[:mailer] = (app.opts[:mailer]||OPTS).merge(opts).freeze
end
module ClassMethods
# Return a Mail::Message instance for the email for the given request path
- # and arguments. You can further manipulate the returned mail object before
- # calling +deliver+ to send the mail.
+ # and arguments. Any arguments given are yielded to the appropriate +r.mail+
+ # block after any usual match block arguments. You can further manipulate the
+ #returned mail object before calling +deliver+ to send the mail.
def mail(path, *args)
mail = ::Mail.new
catch(:no_mail) do
unless mail.equal?(new("PATH_INFO"=>path, 'SCRIPT_NAME'=>'', "REQUEST_METHOD"=>"MAIL", 'rack.input'=>StringIO.new, 'roda.mail'=>mail, 'roda.mail_args'=>args).call(&route_block))
raise Error, "route did not return mail instance for #{path.inspect}, #{args.inspect}"
end
mail
end
end
- # Calls +mail+ and immediately sends the resulting mail.
+ # Calls +mail+ with given arguments and immediately sends the resulting mail.
def sendmail(*args)
if m = mail(*args)
m.deliver
end
end
@@ -226,11 +216,11 @@
module InstanceMethods
# Add delegates for common email methods.
[:from, :to, :cc, :bcc, :subject].each do |meth|
define_method(meth) do |*args|
- env['roda.mail'].send(meth, *args)
+ env['roda.mail'].public_send(meth, *args)
nil
end
end
[:text_part, :html_part].each do |meth|
define_method(meth) do |*args|
@@ -265,10 +255,10 @@
private
# Set the text_part or html_part (depending on the method) in the related email,
# using the given body and optional headers.
def _mail_part(meth, body, headers=nil)
- env['roda.mail'].send(meth) do
+ env['roda.mail'].public_send(meth) do
body(body)
headers(headers) if headers
end
nil
end