Skip to content

Commit

Permalink
Selective multipart delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Sep 23, 2015
1 parent 6c6738c commit 3833a35
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/lotus/mailer.rb
Expand Up @@ -338,7 +338,8 @@ def __part(format)
end

def __part?(format)
!self.class.templates(format).nil?
@format == format ||
(!@format && !self.class.templates(format).nil?)
end
end
end
1 change: 1 addition & 0 deletions lib/lotus/mailer/rendering.rb
Expand Up @@ -18,6 +18,7 @@ module InstanceMethods
# @since 0.1.0
def initialize(locals = {})
@locals = locals
@format = locals.fetch(:format, nil)
@mail = Mail.new.tap do |m|
m.from = __dsl(:from)
m.to = __dsl(:to)
Expand Down
41 changes: 27 additions & 14 deletions test/delivery_test.rb
Expand Up @@ -64,26 +64,39 @@
end
end

describe 'test delivery with methods' do
before do
@user = User.new('Ines', 'ines@deigirls.com')
MethodMailer.deliver(user: @user)

@mail = Lotus::Mailer.deliveries.first
end

describe 'multipart' do
after do
Lotus::Mailer.deliveries.clear
end

it 'delivers the mail' do
Lotus::Mailer.deliveries.length.must_equal 1
it 'delivers all the parts by default' do
WelcomeMailer.deliver

mail = Lotus::Mailer.deliveries.first
body = mail.body.encoded

body.must_include %(<h1>Hello World!</h1>)
body.must_include %(This is a txt template)
end

it 'sends the correct information' do
@mail.from.must_equal ["hello-#{ @user.name.downcase }@example.com"]
@mail.to.must_equal [@user.email]
@mail.subject.must_equal "Hello, #{ @user.name }"
it 'can deliver only the text part' do
WelcomeMailer.deliver(format: :txt)

mail = Lotus::Mailer.deliveries.first
body = mail.body.encoded

body.wont_include %(<h1>Hello World!</h1>)
body.must_include %(This is a txt template)
end

it 'can deliver only the html part' do
WelcomeMailer.deliver(format: :html)

mail = Lotus::Mailer.deliveries.first
body = mail.body.encoded

body.must_include %(<h1>Hello World!</h1>)
body.wont_include %(This is a txt template)
end
end

Expand Down

0 comments on commit 3833a35

Please sign in to comment.