Skip to content

Instantly share code, notes, and snippets.

@glebm
Last active August 8, 2017 09:42
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save glebm/5725347 to your computer and use it in GitHub Desktop.
Inject locale into Resque::Mailer and Devise::Async
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
@glebm
Copy link
Author

glebm commented Sep 5, 2013

requires ruby 2 and this devise-async PR mhfs/devise-async#37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment