Skip to content

Instantly share code, notes, and snippets.

@leifcr
Forked from glebm/async_emails_i18n.rb
Created September 4, 2013 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leifcr/6435680 to your computer and use it in GitHub Desktop.
Save leifcr/6435680 to your computer and use it in GitHub Desktop.
require 'resque_mailer'
require 'devise/async'
# pop and set locale from the args before running
module PerformWithLocale
def perform(*args)
I18n.with_locale(args.pop) do
super(*args)
end
end
end
module DeliverWithLocale
def deliver
# @args are not used when rendered inline, only when enqueueing
@args << I18n.locale.to_s
super
end
end
#= resque_mailer locale support
if defined?(Resque::Mailer)
module Resque::Mailer
# push locale onto the args when queueing
MessageDecoy.send :prepend, DeliverWithLocale
# pop when dequeueing
ClassMethods.send :prepend, PerformWithLocale
end
end
#= devise/async locale support
if defined?(Devise::Async)
module Devise::Async
module EnqueueWithLocale
def enqueue(*args)
super *(args + [I18n.locale])
end
end
class Worker
class << self
prepend Devise::Async::EnqueueWithLocale
end
end
class Backend::Resque
class << self
prepend PerformWithLocale
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment