Commit 2aaa7a7aa167b1717b367dbe9cf990f738a46505
Committed by
Macartur Sousa
1 parent
3ea3b1d1
Exists in
elasticsearch_api
CommentHandler delayed job saves the context locale
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com>
Showing
2 changed files
with
19 additions
and
1 deletions
Show diff stats
app/models/comment_handler.rb
1 | -class CommentHandler < Struct.new(:comment_id, :method) | 1 | +class CommentHandler < Struct.new(:comment_id, :method, :locale) |
2 | + def initialize(*args) | ||
3 | + super | ||
4 | + self.locale ||= FastGettext.locale | ||
5 | + end | ||
2 | 6 | ||
3 | def perform | 7 | def perform |
8 | + saved_locale = FastGettext.locale | ||
9 | + FastGettext.locale = locale | ||
10 | + | ||
4 | comment = Comment.find(comment_id) | 11 | comment = Comment.find(comment_id) |
5 | comment.send(method) | 12 | comment.send(method) |
13 | + FastGettext.locale = saved_locale | ||
6 | rescue ActiveRecord::RecordNotFound | 14 | rescue ActiveRecord::RecordNotFound |
7 | # just ignore non-existing comments | 15 | # just ignore non-existing comments |
8 | end | 16 | end |
test/unit/comment_handler_test.rb
@@ -21,4 +21,14 @@ class CommentHandlerTest < ActiveSupport::TestCase | @@ -21,4 +21,14 @@ class CommentHandlerTest < ActiveSupport::TestCase | ||
21 | handler.perform | 21 | handler.perform |
22 | end | 22 | end |
23 | 23 | ||
24 | + should 'save locale from creation context and not change current locale' do | ||
25 | + FastGettext.stubs(:locale).returns("pt") | ||
26 | + handler = CommentHandler.new(5, :whatever_method) | ||
27 | + | ||
28 | + FastGettext.stubs(:locale).returns("en") | ||
29 | + assert_equal "pt", handler.locale | ||
30 | + | ||
31 | + handler.perform | ||
32 | + assert_equal "en", FastGettext.locale | ||
33 | + end | ||
24 | end | 34 | end |