Created
August 30, 2009 20:44
-
-
Save bryanstearns/178125 to your computer and use it in GitHub Desktop.
config/initializers/delayed_job.rb with conditional mail delaying
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Conditionally delay outgoing mail | |
class DelayedMailerBase < ActionMailer::Base | |
class << self | |
def method_missing(method_symbol, *parameters) | |
# If this is "deliver_xx", maybe delay it; if we do, change | |
# its name so that we don't delay in the worker. | |
method_name = method_symbol.to_s | |
if matches_deliver_method?(method_name) and delaying_mail? | |
return self.send_later("delayed_#{method_name}".to_sym, *parameters) | |
end | |
# If this is a delayed name (meaning we're the worker), | |
# convert it back. | |
if match = matches_delayed_deliver_method?(method_name) | |
return super(match[1], *parameters) | |
end | |
super # Nothing funny - do the normal thing. | |
end | |
def respond_to?(method) | |
super or matches_deliver_method?(method) \ | |
or matches_delayed_deliver_method?(method) \ | |
end | |
def delaying_mail? | |
Rails.env.production? | |
end | |
private | |
def matches_deliver_method?(method_symbol) #:nodoc: | |
/^deliver_[_a-z]\w*/.match(method_symbol.to_s) | |
end | |
def matches_delayed_deliver_method?(method_symbol) #:nodoc: | |
/^delayed_(deliver.*)/.match(method_symbol.to_s) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment