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.

318 CHAPTER 12 Sending email<br />

Now that you have the user who created the ticket watching it, your CommentObserver<br />

will have something to act on. Let’s see what happens when you run bin/cucumber<br />

features/ticket_notifications.feature:<br />

And I press "Create Comment"<br />

uninitialized constant CommentObserver::Notifier (NameError)<br />

This time, your feature is failing because it can’t find the constant Notifier, which is<br />

actually going to be the class that you use to send out the notifications of new activity<br />

to your users. To create this class, you’ll use Action Mailer.<br />

12.1.4 Introducing Action Mailer<br />

You need to define the Notifier mailer to send out ticket-update notifications using<br />

your fresh-out-of-the-oven CommentObserver’s after_create method. You can do this<br />

by running the mailer generator.<br />

A mailer is a class defined for sending out emails. To define your mailer, you run<br />

this command:<br />

rails g mailer notifier<br />

When running this command, you see this output:<br />

create app/mailers/notifier.rb<br />

invoke erb<br />

create app/views/notifier<br />

invoke rspec<br />

create spec/mailers/notifier_spec.rb<br />

The first thing the command generates is the Notifier class itself, defining it in a new<br />

file at app/mailers/notifier.rb. This is done to keep the models and mailers separate.<br />

In previous versions of Rails, mailers used to live in the app/models directory, which<br />

led to clutter. By separating mailers out into their own folder, the codebase becomes<br />

easier to manage. Inside this class, you define (as methods) your different notifications<br />

that you’ll send out, beginning with the comment notification. You’ll get to that<br />

in just a minute.<br />

The second thing that is generated is the app/views/notifier directory, which is<br />

used to store all the templates for your emails. The methods in the Notifier class will<br />

correspond to each of the files in this directory.<br />

The final thing that is generated is the spec/mailers/notifier_spec.rb, which you<br />

won’t use because you’ve got your feature testing this notifier anyway.<br />

In app/mailers/notifier.rb you see this code:<br />

class Notifier < ActionMailer::Base<br />

default from: "from@example.com"<br />

end<br />

ActionMailer::Base defines helpful methods such as the default one, which you can<br />

use to send out your emails. 6 The default method here configures default options for<br />

6 Action Mailer had a revamp with Rails 3, switching to be based on the new mail gem rather than the old<br />

tmail gem. mail’s syntax is much nicer and won’t crash when it parses a spam email, unlike tmail.

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

Saved successfully!

Ooh no, something went wrong!