27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Receiving emails<br />

Listing 12.14 app/mailers/notifier_spec.rb<br />

require "spec_helper"<br />

describe Notifier do<br />

it "correctly sets the reply-to" do<br />

comment = Factory(:comment)<br />

mail = ActionMailer::Base.deliveries.last<br />

mail.from.should eql(["youraccount+#{comment.project.id}+" +<br />

"#{comment.ticket.id}@example.com"])<br />

end<br />

end<br />

Here you test that the from for the latest email sent out contains the ids of the project<br />

and ticket related to the comment you create. With this information contained in the<br />

email address, you’ll be able to know what project and ticket to create the comment<br />

for when a user replies to that email.<br />

When you run this spec using bin/rspec spec/mailers/notifier_spec.rb, you<br />

see that you need to define the comment factory:<br />

No such factory: comment (ArgumentError)<br />

Let’s define this new factory in factories/comment_factory.rb like this:<br />

Factory.define :comment do |comment|<br />

comment.text "A plain old boring comment."<br />

comment.ticket { |t| t.association(:ticket) }<br />

comment.user { |u| u.association(:user) }<br />

end<br />

Now when you run bin/rspec spec/mailers/notifier_spec.rb, you see that it fails<br />

with this error:<br />

expected ["ticketee+1+1@gmail.com"]<br />

got ["ticketee@gmail.com"]<br />

Right then! A failing test is a great place to begin, and now you need to fix it. Let’s<br />

open app/mailers/notifier.rb and add a :from option to the mail call inside the<br />

comment_updated method:<br />

:from => "Ticketee "<br />

This will change the “from” address on emails that go out to your users by tagging the<br />

addresses with the project and ticket id. When the user replies to this email, you can<br />

use this tag to find the project and ticket that you need to create a new comment on.<br />

Let’s run bin/rspec spec/mailers/notifier_spec.rb again to see it pass:<br />

1 example, 0 failures<br />

Now you need to work on the actual receiving of replies directed at this tagged<br />

address!<br />

341

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!