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 />

The template is the final part for your feature: yay! When you run bin/cucumber<br />

features/ticket_notifications.feature, you see that it’s now all passing:<br />

1 scenario (1 passed)<br />

18 steps (18 passed)<br />

When the email is opened for this final time, you see that it has the content you’re<br />

looking for:<br />

Hello!<br />

bob@ticketee.com has just updated the Release date ticket for TextMate 2.<br />

➥They wrote:<br />

Is it out yet?<br />

You can view this ticket online by going to:<br />

➥http://localhost:3000/projects/1/tickets/1<br />

You’ve done quite a lot to get this little simple feature to pass.<br />

In the beginning you created an observer called CommentObserver, which watches<br />

the Comment model for any specific changes. You defined an after_create method on<br />

this, which took the comment object that was being updated and then called Notifier<br />

.comment_updated, passing along the comment object.<br />

Notifier is an Action Mailer class that is responsible for sending out emails to the<br />

users of your application, and in this file you defined the comment_updated method<br />

called in your CommentObserver and set the recipients up to use the comment<br />

object’s related ticket watchers.<br />

To define the watchers method, you used a has_and_belongs_to_many join table<br />

again. Your first experience using these was back in chapter 10, when you linked the<br />

Ticket and Tag models by setting one up on both of them. Back then, you used the<br />

tags_tickets table to link the two. This is the default naming schema of a has_and<br />

_belongs_to_many join table in Rails. In the case of your ticket watchers, however,<br />

your method was called watchers, and so would look for a class called Watcher to<br />

determine where it should find your watchers. This was incorrect, so you told your<br />

association that your join table should be ticket_watchers and that the related<br />

model was User, not Watcher. You used the :join_table and :class_name methods<br />

for this.<br />

Finally, you defined the template for the comment_updated email at app/views/<br />

notifier/comment_updated.html.erb and filled it with the content you’re expecting to<br />

see, including the link that you click to complete the final step of your scenario.<br />

This scenario completes the first steps of sending email notifications to your users.<br />

You should now run all your tests to make sure you didn’t break anything by running<br />

rake cucumber:ok spec:<br />

60 scenarios (60 passed)<br />

676 steps (676 passed)<br />

# and<br />

40 examples, 0 failures, 19 pending<br />

321

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

Saved successfully!

Ooh no, something went wrong!