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

Before you go about configuring your application to send emails into the real<br />

world, you’ll add two more features to Ticketee. The first feature automatically subscribes<br />

a user to a watchers list whenever that user creates a ticket. Every time this<br />

ticket is updated by another user, the creator of the ticket should receive an email.<br />

This is helpful, because it allows users to keep up-to-date with the tickets that they<br />

have created. The second feature will allow users to add themselves to or remove<br />

themselves from the watching list for a given ticket.<br />

With these features in place, all users who are watching a ticket will be notified via<br />

email that a comment has been posted to the ticket, what that comment was, and any<br />

state change that took place. This email message will additionally contain a link to the<br />

ticket and a link to unsubscribe from further notifications regarding the ticket. If a<br />

user posts a comment to a ticket and they’re not watching it, then they will automatically<br />

begin to watch it. They can unsubscribe later if they wish by following the unsubscribe<br />

link in the email. Email is a tried-and-true solution to receiving notifications of<br />

events such as this.<br />

Once that’s all said and done, you’ll work on sending emails through an actual<br />

server—Gmail—which will test that your application is able to send out emails into the<br />

real world and that you’re doing everything you can to let your users receive them.<br />

Gmail is great for low-volume sending, 2 but if you needed something with a larger<br />

capacity, other services such as SendGrid 3 and MailChimp 4 are acceptable alternatives.<br />

Although we don’t look at how to use large-volume services in this chapter, it’s always<br />

great to be aware of alternatives, should you ever need to scale up. To check for the<br />

emails on a Gmail account, you’ll be using the (unofficial) 5 gmail gem.<br />

After spending most of the chapter looking at how to send emails, you’ll take a look<br />

at how to receive them using the gmail gem and Action Mailer. When a user receives an<br />

email notifying them that a comment has been posted to a ticket, they will be able to<br />

send a reply that you can read using both the gmail gem and Action Mailer. You’ll also<br />

be able to create a new comment from their reply’s text. Nifty stuff.<br />

The first thing you’re going to do is set up a way for users to receive notifications<br />

when a comment is posted to a ticket they’ve created. Let’s dive into creating the feature<br />

and code for this functionality now.<br />

12.1 Sending ticket notifications<br />

The next feature of your application will provide users with the ability to watch a<br />

ticket. You’ll build off this functionality to notify users by email that a ticket has been<br />

updated any time somebody posts a comment to it. This email will contain the name<br />

of the user who updated the ticket, the comment text, a URL to the ticket, and finally<br />

a link to unsubscribe from all future ticket updates.<br />

2 Gmail has a daily send limit of 200 emails.<br />

3 http://sendgrid.com.<br />

4 http://mailchimp.com.<br />

5 As in, not sponsored by Google.<br />

313

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

Saved successfully!

Ooh no, something went wrong!