Skip to content

Instantly share code, notes, and snippets.

@bryanstearns
Created August 30, 2009 20:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanstearns/178125 to your computer and use it in GitHub Desktop.
Save bryanstearns/178125 to your computer and use it in GitHub Desktop.
config/initializers/delayed_job.rb with conditional mail delaying
# 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