Skip to content

Commit

Permalink
[padrino-mailer] Allow template path to be specified in mailer and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nesquena committed Apr 9, 2010
1 parent 5df58c7 commit 798dc56
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGES.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Fixes an issue with generator not casing controller / model names
* Added sequel support for padrino-admin [Thanks to Aemadrid]
* Added basic sequel migration tasks [Thanks to Aemadrid]
* Mailer now supports setting template path to render explicitly

== 0.9.9

Expand Down
1 change: 1 addition & 0 deletions padrino-mailer/README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Next, we should define a custom mailer extended from <tt>Padrino::Mailer::Base</
type 'html' # optional, defaults to plain/text
charset 'windows-1252' # optional, defaults to utf-8
via :sendmail # optional, to smtp if defined otherwise sendmail
template 'sample_mailer/foo' # optional, defaults to views/sample_mailer/registration_email.erb
end
end

Expand Down
22 changes: 15 additions & 7 deletions padrino-mailer/lib/padrino-mailer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Base
# Returns the available mail fields when composing a message
#
def self.mail_fields
[:to, :cc, :bcc, :reply_to, :from, :subject, :content_type, :charset, :via, :attachments]
[:to, :cc, :bcc, :reply_to, :from, :subject, :content_type, :charset, :via, :attachments, :template]
end

@@views_path = []
Expand All @@ -45,18 +45,18 @@ def initialize(mail_name=nil) #:nodoc:
# Assigns the body key to the mail attributes either with the rendered body from a template or the given string value
#
def body(body_value)
template = template_path
raise "Template for '#{@mail_name}' could not be located in views path!" unless template
@mail_attributes[:body] = Tilt.new(template).render(self, body_value.symbolize_keys) if body_value.is_a?(Hash)
final_template = template_path
raise "Template for '#{@mail_name}' could not be located in views path!" unless final_template
@mail_attributes[:body] = Tilt.new(final_template).render(self, body_value.symbolize_keys) if body_value.is_a?(Hash)
@mail_attributes[:body] = body_value if body_value.is_a?(String)
end

##
# Returns the path to the email template searched for using glob pattern
#
def template_path
self.views_path.each do |path|
template = Dir[File.join(path, self.class.name.underscore.split("/").last, "#{@mail_name}.*")].first
self.views_path.each do |view_path|
template = Dir[File.join(view_path, template_pattern)].first
return template if template
end
end
Expand Down Expand Up @@ -89,6 +89,14 @@ def self.respond_to?(method_sym, include_private = false)
def self.method_missing(method_sym, *arguments, &block)
method_sym.to_s =~ /deliver_(.*)/ ? self.deliver($1, *arguments) : super(method_sym, *arguments, &block)
end

private

# Returns the glob pattern of the template file to locate and render
def template_pattern
@_pattern ||= (@mail_attributes[:template].present? ? "#{@mail_attributes[:template]}.*" :
File.join(self.class.name.underscore.split("/").last, "#{@mail_name}.*"))
end
end # Base
end # Mailer
end # Padrino
end # Padrino
15 changes: 14 additions & 1 deletion padrino-mailer/test/fixtures/mailer_app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class MailerDemo < Sinatra::Base
register Padrino::Mailer

class SampleMailer < Padrino::Mailer::Base

def birthday_message(name, age)
subject "Happy Birthday!"
to 'john@fake.com'
Expand All @@ -33,13 +32,27 @@ def anniversary_message(names, years_married)
body 'names' => names, 'years_married' => years_married
content_type 'text/html'
end

def welcome_message(name)
template 'sample_mailer/foo_message'
subject "Welcome Message!"
to 'john@fake.com'
from 'noreply@custom.com'
body 'name' => name
via :smtp
end
end

post "/deliver/plain" do
result = SampleMailer.deliver_birthday_message("Joey", 21)
result ? "mail delivered" : 'mail not delivered'
end

post "/deliver/custom" do
result = SampleMailer.deliver_welcome_message("Bobby")
result ? "mail delivered" : 'mail not delivered'
end

post "/deliver/html" do
result = SampleMailer.deliver_anniversary_message("Joey & Charlotte", 16)
result ? "mail delivered" : 'mail not delivered'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello to <%= name %>
19 changes: 14 additions & 5 deletions padrino-mailer/test/test_padrino_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ def app
assert_equal 'mail delivered', last_response.body
end

should 'be able to deliver emails with custom view' do
assert_email_sent(:template => 'sample_mailer/foo_message', :to => 'john@fake.com',
:from => 'noreply@custom.com', :via => :smtp,
:subject => "Welcome Message!", :body => "Hello to Bobby")
visit '/deliver/custom', :post
assert_equal 'mail delivered', last_response.body
end

should 'be able to deliver html emails' do
assert_email_sent(:to => 'julie@fake.com', :from => 'noreply@anniversary.com', :content_type => 'text/html', :via => :smtp,
assert_email_sent(:to => 'julie@fake.com', :from => 'noreply@anniversary.com',
:content_type => 'text/html', :via => :smtp,
:subject => "Happy anniversary!", :body => "<p>Yay Joey & Charlotte!</p>\n<p>You have been married 16 years</p>")
visit '/deliver/html', :post
assert_equal 'mail delivered', last_response.body
Expand All @@ -29,8 +38,8 @@ def app

protected

def assert_email_sent(mail_attributes)
delivery_attributes = mail_attributes.merge(:smtp => MailerDemo.smtp_settings)
Padrino::Mailer::MailObject.any_instance.expects(:send_mail).with(delivery_attributes).once.returns(true)
end
def assert_email_sent(mail_attributes)
delivery_attributes = mail_attributes.merge(:smtp => MailerDemo.smtp_settings)
Padrino::Mailer::MailObject.any_instance.expects(:send_mail).with(delivery_attributes).once.returns(true)
end
end

0 comments on commit 798dc56

Please sign in to comment.