Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1
Contributing to Rails | 289

# User administration tasks go in a separate namespace
namespace :users do
desc "Send a nasty email to over-quota users"
task :send_quota_warnings do
users = User.find(:all).select{|u| u.storage_used > u.account.quota }
users.each do |user|
Mailer.deliver_quota_exceeded_notification(user)
end
end
end

We can now kick off the email with one command from the project’s directory:


rake users:send_quota_warnings

Receiving email


The ActionMailer documentation includes instructions on how to receive incoming
email using Rails and ActionMailer. This method involves having your MTA pipe
incoming mail to a command such as this:


script/runner 'Mailer.receive(STDIN.read)'

Do not do this except in the absolute simplest of cases. Usingscript/runneris very
computationally expensive, because it loads the entire Rails environment on each
invocation. Loading a Ruby interpreter with all of Rails for each incoming email is
ridiculous.


The standard method for processing incoming mail is to batch email in a mailbox
and have a process retrieve that mail at a regular interval. The mail can be retrieved
usingNet::POP3 orNet::IMAP from the Ruby standard library.


If mail really needs to be processed immediately upon receipt, a custom solution,
even using Ruby and ActionMailer, will still be much faster than the preceding exam-
ple that loads all of Rails. But if you need immediate delivery, you should probably
first consider a solution like SMS or Jabber rather than SMTP.


Contributing to Rails

Rails, as an open source framework, benefits greatly from contributions from the
community. Rails incorporates code from hundreds of developers, not just the dozen
or so on the core team. Writing code to expand, extend, or fix Rails is often the best
way to learn about its internals.


Of course, not all functionality belongs in Rails itself. Rails is an opinionated frame-
work, so there are some defaults that may not be useful to everyone. The plugin sys-
tem was designed so that Rails would not have to incorporate every feature that is
useful to someone. Refer to Chapter 3 for information on writing plugins to extend
Rails; it is only minimally more work than patching the Rails codebase.

Free download pdf