From 2aaa7a7aa167b1717b367dbe9cf990f738a46505 Mon Sep 17 00:00:00 2001 From: Gabriel Silva Date: Mon, 23 May 2016 20:22:02 +0000 Subject: [PATCH] CommentHandler delayed job saves the context locale --- app/models/comment_handler.rb | 10 +++++++++- test/unit/comment_handler_test.rb | 10 ++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/models/comment_handler.rb b/app/models/comment_handler.rb index dcb2ff3..8b902ba 100644 --- a/app/models/comment_handler.rb +++ b/app/models/comment_handler.rb @@ -1,8 +1,16 @@ -class CommentHandler < Struct.new(:comment_id, :method) +class CommentHandler < Struct.new(:comment_id, :method, :locale) + def initialize(*args) + super + self.locale ||= FastGettext.locale + end def perform + saved_locale = FastGettext.locale + FastGettext.locale = locale + comment = Comment.find(comment_id) comment.send(method) + FastGettext.locale = saved_locale rescue ActiveRecord::RecordNotFound # just ignore non-existing comments end diff --git a/test/unit/comment_handler_test.rb b/test/unit/comment_handler_test.rb index b3fa6f2..61a2ba3 100644 --- a/test/unit/comment_handler_test.rb +++ b/test/unit/comment_handler_test.rb @@ -21,4 +21,14 @@ class CommentHandlerTest < ActiveSupport::TestCase handler.perform end + should 'save locale from creation context and not change current locale' do + FastGettext.stubs(:locale).returns("pt") + handler = CommentHandler.new(5, :whatever_method) + + FastGettext.stubs(:locale).returns("en") + assert_equal "pt", handler.locale + + handler.perform + assert_equal "en", FastGettext.locale + end end -- libgit2 0.21.2