rails/rails — pull request #7202 by asanghi (2012-07-30 18:39:13+00:00)
Title: Allow perform_deliveries to be set within mailer action

It was found that there were some good use cases where it was prudent to ````prevent_deliveries```` even if ````prevent_deliveries```` was globally set to true for e.g. you wish to not send emails to users who are unsubscribed or users who are blocked or unconfirmed.

There are other use cases as well, such as spam detection within the body should prevent delivery.

This currently requires a common pattern which goes something like this

````ruby
SomeMailer.someemail(user).deliver if user.sendable?
````
With this change it can be written alternatively as

````ruby
def somemail(user)
mail :to => user.email, :subject => ... , :perform_deliveries => user.sendable?
end
SomeMailer.somemail(user).deliver # expect no delivery if user was unsendable
````

This allows for conditional deliveries based on user's own logic.

The way I've implemented this, ensures that if ````perform_deliveries```` is switched_off globally, then it cant be switched on by the mail message instance.

After checking further it was found that ````perform_deliveries```` could be overridden in interceptors but at that stage you do not always have access to the underlying models used for decisions.

/cc @schneems / @josevalim / @mikel