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.

Sending ticket notifications<br />

this mailer and will set the “from” address on all emails to be the one specified. Let’s<br />

change this to be ticketee@gmail.com.<br />

Now that you have the Notifier class defined, what happens when you run your<br />

feature? Let’s run it using bin/cucumber features/ticket_notifications.feature<br />

and find out:<br />

undefined method `comment_updated' for Notifier:Class (NoMethodError)<br />

./app/observers/comment_observer.rb:3:in `after_create'<br />

In this class, you need to define the comment_updated method, which will build an<br />

email to send out when a comment is updated. This method needs to get the email<br />

address for all the watchers for comment’s ticket and send an email to each of them. To<br />

do this, you can define the method like this:<br />

def comment_updated(comment, user)<br />

@comment = comment<br />

@user = user<br />

mail(:to => user.email,<br />

:subject => "[ticketee] #{comment.ticket.project.name} -<br />

#{comment.ticket.title}")<br />

end<br />

Even though you’re defining this as an instance method (the error complains about a<br />

class method), the comment_updated method is truly the method that is used by Action<br />

Mailer to set up your email. This is a little bit of magic performed by Action Mailer for<br />

your benefit. 7<br />

When this method is called, it will attempt to render a plain-text template for the<br />

email, which should be found at app/views/notifier/comment_updated.text.erb. You’ll<br />

define this template after you’ve got the method working. You define a @comment<br />

instance variable as the first line of your method so that the object in comment will be<br />

available to your template.<br />

You use the mail method to generate a new email, passing it a hash containing to<br />

and subject keys, which define where the email goes to as well as the subject for the<br />

email.<br />

When you run bin/cucumber features/ticket_notifications.feature, you see<br />

that the user now receives an email and therefore is able to open it, but the content<br />

you’re looking for is not there:<br />

Then "alice@ticketee.com" should receive an email<br />

When "alice@ticketee.com" opens the email<br />

Then they should see "updated the Release date ticket" in the email body<br />

expected "" to include "updated the Release date ticket" ...<br />

But why is this not seeing the content? Because you don’t have a template set up just at<br />

the moment! It’s good to know at this point that if you ever wanted to debug an<br />

email’s content, there’s a “Then show me the page” inspired step that you can use<br />

called “Then save and open current email.” Let’s add this on a new line right before<br />

7 By calling the method on the class, it’s caught by method_missing, which initializes a new instance of this<br />

class and then eventually ends up calling your comment_update method.<br />

319

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

Saved successfully!

Ooh no, something went wrong!