Skip to content

Commit

Permalink
Multipart Delivery (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
inescoelho authored and rosafaria committed Aug 26, 2015
1 parent 95bee78 commit 66ea851
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 4 deletions.
69 changes: 69 additions & 0 deletions lib/lotus/mailer.rb
Expand Up @@ -52,6 +52,8 @@ def self.configure(&blk)
def self.included(base)
conf = self.configuration
conf.add_mailer(base)
base.extend(ClassMethods)
base.include(InstanceMethods)

base.class_eval do
extend Dsl.dup
Expand Down Expand Up @@ -94,5 +96,72 @@ def self.load!
def self.reset!
configuration.reset!
end

module ClassMethods
# Delivers a multipart email. It instantiates a mailer and deliver the email.
#
# @since 0.1.0
#
# @example email delivery through smtp via gmail configured with environment variables
# class DeliveryMethodMailer
# include Lotus::Mailer
#
# from ENV["GMAIL_USER"]
# to "inesopcoelho@gmail.com"
# subject "This is the subject"
#
# end
#
# MyCustomDeliveryMethod = {
# :address => "smtp.gmail.com",
# :port => 587,
# :domain => "localhost:8000",
# :user_name => ENV["GMAIL_USER"],
# :password => ENV["GMAIL_PASSWORD"],
# :authentication => "plain",
# :enable_starttls_auto => true
# }
# Lotus::Mailer.configure do
# delivery_method :smtp, MyCustomDeliveryMethod
# end
#
# DeliveryMethodMailer.deliver
def deliver(locals = {})
new(locals).deliver
end
end

module InstanceMethods
# Delivers a multipart email, by looking at all the associated templates and render them.
#
# @since 0.1.0
def deliver
mail['from'] = self.class.from
mail['to'] = self.class.to
mail['subject'] = self.class.subject
if Lotus::Mailer.configuration.delivery_method
mail.delivery_method *Lotus::Mailer.configuration.delivery_method
end

#attach templates
self.class.templates.each do |type, content|
case type
when :html
mail_body = Mail::Part.new
mail_body.content_type 'text/html; charset=UTF-8'
mail_body.body render(:html)
mail.html_part = mail_body
when :txt
mail_body = Mail::Part.new
mail_body.body render(:txt)
mail.text_part = mail_body
else
mail.attachments[content.name] = render(type)
end
end

mail.deliver
end
end
end
end
2 changes: 1 addition & 1 deletion lib/lotus/mailer/configuration.rb
Expand Up @@ -124,7 +124,6 @@ def root(value = nil)
# @since 0.1.0
#
# @see Lotus::Mailer.configure
# @see Lotus::Mailer.duplicate
def prepare(&blk)
if block_given?
@modules.push(blk)
Expand Down Expand Up @@ -152,6 +151,7 @@ def duplicate
c.namespace = namespace
c.root = root
c.modules = modules.dup
c.delivery_method = delivery_method
end
end

Expand Down
5 changes: 5 additions & 0 deletions lib/lotus/mailer/dsl.rb
Expand Up @@ -7,6 +7,8 @@ module Mailer
#
# @since 0.1.0
module Dsl
attr_reader :mail

# When a value is given, specify a templates root path for the mailer.
# Otherwise, it returns templates root path.
#
Expand Down Expand Up @@ -227,6 +229,9 @@ def load!
m.configuration.freeze
end
end

attr_writer :mail

end
end
end
3 changes: 3 additions & 0 deletions lib/lotus/mailer/rendering.rb
Expand Up @@ -19,6 +19,7 @@ module InstanceMethods
def initialize(locals = {})
@locals = locals
@scope = self
@mail = Mail.new
end

# Render a single template with the specified format.
Expand All @@ -32,6 +33,8 @@ def render(format)
self.class.templates
self.class.templates[format].render @scope, @locals
end

attr_accessor :mail
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/lotus/mailer/template.rb
Expand Up @@ -32,6 +32,15 @@ def file
@_template.file
end

# Get the template's name
#
# @return [String] template's name
#
# @since 0.1.0
def name
name = @_template.file.split('/')[-1]
name.split('.')[0] + "." + name.split('.')[1]
end
end
end
end
36 changes: 36 additions & 0 deletions test/delivery_test.rb
@@ -0,0 +1,36 @@
describe Lotus::Mailer do

describe '#deliver' do
before do
Lotus::Mailer.configure do
delivery_method :test
end

Mail::TestMailer.deliveries.clear
WelcomeMailer.deliver
end

it 'delivers the mail' do
Mail::TestMailer.deliveries.length.must_equal 1
end

it 'sends the correct information' do
Mail::TestMailer.deliveries.first.from.must_equal ["noreply@sender.com"]
Mail::TestMailer.deliveries.first.to.must_equal ["noreply@recipient.com"]
Mail::TestMailer.deliveries.first.subject.must_equal "Welcome"
end

it 'has the correct templates' do
Mail::TestMailer.deliveries.first.html_part.to_s.must_include %(template)
Mail::TestMailer.deliveries.first.text_part.to_s.must_include %(template)
Mail::TestMailer.deliveries.first.attachments[0].to_s.must_include %(welcome_mailer)
Mail::TestMailer.deliveries.first.attachments[1].to_s.must_include %(welcome_mailer)
end

after do
Lotus::Mailer.configure do
delivery_method :smtp
end
end
end
end
6 changes: 5 additions & 1 deletion test/fixtures.rb
Expand Up @@ -49,7 +49,11 @@ class ArrayMailer

class WelcomeMailer
include Lotus::Mailer


from "noreply@sender.com"
to "noreply@recipient.com"
subject "Welcome"

def greeting
"Ahoy"
end
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/templates/welcome_mailer.csv.erb
@@ -0,0 +1 @@
This;is;a;csv;template;
3 changes: 3 additions & 0 deletions test/fixtures/templates/welcome_mailer.haml.erb
@@ -0,0 +1,3 @@
%html
%body
This is a haml template
10 changes: 9 additions & 1 deletion test/fixtures/templates/welcome_mailer.html.erb
@@ -1 +1,9 @@
<%= greeting %>!

<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
<%=greeting%>
<p>This is a html template</p>
</body>
</html>
3 changes: 2 additions & 1 deletion test/fixtures/templates/welcome_mailer.txt.erb
@@ -1 +1,2 @@
<%= greeting %>!
This is a txt template
<%=greeting%>

0 comments on commit 66ea851

Please sign in to comment.