public
Last active

config/initializers/delayed_job.rb with conditional mail delaying

  • Download Gist
config/initializers/delayed_job.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.