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

You’re not really sending emails<br />

These emails aren’t actually sent to these addresses in the real world, but captured<br />

by Action Mailer and stored in ActionMailer::Base.deliveries. You then access<br />

these emails using the helpers provided by email_spec. There’s a setting inside<br />

config/environments/test.rb that goes like this:<br />

config.action_mailer.delivery_method = :test<br />

By default, this setting is set to :smtp, which means that Action Mailer will attempt<br />

to connect to an SMTP server that is running on localhost. You don’t have one of<br />

these set up yet, nor will you. Later on, we’ll look at how you can send real-world<br />

emails from your application using a Gmail account.<br />

The setting in config/environments/test.rb will tell Action Mailer to store all sent<br />

emails internally in ActionMailer::Base.deliveries.<br />

In this feature, you’ll sign in as the second user and create a comment on the ticket<br />

that the first user created. After the comment has been created, the ticket creator<br />

should receive an email and click the View This Ticket Online Here link that will take<br />

them to the updated ticket’s page to see the latest comment. When you run this feature<br />

using bin/cucumber features/ticket_notifications.feature, you see that<br />

everything up to the “should receive an email” step passes, because you’ve already<br />

implemented it all:<br />

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

expected: 1,<br />

got: 0 (using ==) ...<br />

When bob@ticketee.com updates the ticket, alice@ticketee.com doesn’t receive an<br />

email, yet. That’s why you wrote the feature: so you can test the behavior that you’re<br />

about to create!<br />

To make alice@ticketee.com receive an email, you’re going to use what’s known as<br />

an observer.<br />

12.1.2 Using observers<br />

An observer is a class that sits outside the model, watching it for specific actions such<br />

as a save to the database. If new instances of the model are created, then the<br />

before_create and after_create methods in the observer will be called. Observers<br />

are handy if you have complex logic for your callbacks, or for sending out email. Hey,<br />

isn’t that what you want to do? Indeed it is!<br />

In this instance, your observer will be called CommentObserver. It’s named like that<br />

because it will observe the Comment model. Observers watch a model for specific<br />

changes and allow you to implement callback-like methods in them to order your<br />

application to do something when an action takes place in the model. Although you<br />

could use a callback in a model, abstracting out code such as this to an observer is<br />

much better because it can lead to reduced code clutter in the model.<br />

315

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

Saved successfully!

Ooh no, something went wrong!