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.

342 CHAPTER 12 Sending email<br />

12.4.2 Receiving a reply<br />

With the correct reply-to set, you can implement the feature responsible for creating<br />

new comments from email replies. You create a new class for dealing with incoming<br />

email and call it Receiver, placing it in app/mailers by running this command:<br />

rails g mailer receiver<br />

This will generate the mailer you use for receiving email, as well as the RSpec file that<br />

you can use to write the tests for the class. To test this particular feature, you use a<br />

setup very similar to the spec/notifier_spec.rb test that you just wrote. This test needs<br />

to generate a comment and then a reply to the email you would receive from the comment.<br />

This new reply should have the same body as the original email, but prefixed<br />

with some text. This new text will become the new comment.<br />

At this stage you only want to check that you can parse emails using this new class<br />

and a currently undefined method on it called parse. This method will take a<br />

Mail::Message object and create a new comment on a ticket. You’ll do permission<br />

checking later on, but for now let’s just get the basic functionality down.<br />

You begin with these lines in spec/mailers/receiver_spec.rb:<br />

require 'spec_helper'<br />

describe Receiver do<br />

it "parses a reply from a comment update into a comment" do<br />

comment = Factory(:comment)<br />

This will set up a comment and a ticket by using the factory, which will also cause a<br />

comment_updated notification to be delivered. You can retrieve this notification using<br />

this line:<br />

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

This is possible because in the test environment, ActionMailer::Base.delivery<br />

_method is set to :test, which stores the emails that have been sent in Action-<br />

Mailer::Base.deliveries. The last email that’s been sent out will be the notification<br />

for the comment. With this email object, you can build a new Mail::Message reply to<br />

this email using these lines:<br />

mail = Mail.new(:from => "user@ticketee.com",<br />

:subject => "Re: #{comment_email.subject}",<br />

:body => %Q{This is a brand new comment<br />

#{comment_email.body}<br />

},<br />

:to => comment_email.from)<br />

B Quoted string<br />

With these lines, you’re constructing a new reply using the body B from the original<br />

email to generate a multi-lined string with “This is a brand new comment” before the<br />

body of the first email. The first line in this first email will eventually be “== ADD YOUR<br />

REPLY ABOVE THIS LINE ==”, which is how you distinguish what should be the new<br />

content for the comment and what’s just from the old email.

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

Saved successfully!

Ooh no, something went wrong!