Solve SystemStackError when changing locale inside ActionMailer [#5329 state:resolved]

This commit is contained in:
José Valim 2011-01-19 23:42:10 +01:00
parent b247f39442
commit 262b2ea8cd
4 changed files with 14 additions and 17 deletions

View File

@ -2,9 +2,8 @@ require 'abstract_unit'
require 'action_controller' require 'action_controller'
class I18nTestMailer < ActionMailer::Base class I18nTestMailer < ActionMailer::Base
configure do |c| configure do |c|
c.assets_dir = '' # To get the tests to pass c.assets_dir = ''
end end
def mail_with_i18n_subject(recipient) def mail_with_i18n_subject(recipient)
@ -17,13 +16,12 @@ end
class TestController < ActionController::Base class TestController < ActionController::Base
def send_mail def send_mail
I18nTestMailer.mail_with_i18n_subject(@recipient).deliver I18nTestMailer.mail_with_i18n_subject("test@localhost").deliver
render :text => 'Mail sent' render :text => 'Mail sent'
end end
end end
class ActionMailerI18nWithControllerTest < ActionDispatch::IntegrationTest class ActionMailerI18nWithControllerTest < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do Routes.draw do
match ':controller(/:action(/:id))' match ':controller(/:action(/:id))'
@ -35,12 +33,10 @@ class ActionMailerI18nWithControllerTest < ActionDispatch::IntegrationTest
def setup def setup
I18n.backend.store_translations('de', :email_subject => '[Signed up] Welcome') I18n.backend.store_translations('de', :email_subject => '[Signed up] Welcome')
set_delivery_method :test end
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries.clear
ActiveSupport::Deprecation.silenced = false
@recipient = 'test@localhost' def teardown
I18n.locale = :en
end end
def test_send_mail def test_send_mail

View File

@ -13,14 +13,15 @@ module AbstractController
# This is a class to fix I18n global state. Whenever you provide I18n.locale during a request, # This is a class to fix I18n global state. Whenever you provide I18n.locale during a request,
# it will trigger the lookup_context and consequently expire the cache. # it will trigger the lookup_context and consequently expire the cache.
class I18nProxy < ::I18n::Config #:nodoc: class I18nProxy < ::I18n::Config #:nodoc:
attr_reader :i18n_config, :lookup_context attr_reader :original_config, :lookup_context
def initialize(i18n_config, lookup_context) def initialize(original_config, lookup_context)
@i18n_config, @lookup_context = i18n_config, lookup_context original_config = original_config.original_config if original_config.respond_to?(:original_config)
@original_config, @lookup_context = original_config, lookup_context
end end
def locale def locale
@i18n_config.locale @original_config.locale
end end
def locale=(value) def locale=(value)

View File

@ -186,11 +186,11 @@ module ActionView
end end
# Overload locale= to also set the I18n.locale. If the current I18n.config object responds # Overload locale= to also set the I18n.locale. If the current I18n.config object responds
# to i18n_config, it means that it's has a copy of the original I18n configuration and it's # to original_config, it means that it's has a copy of the original I18n configuration and it's
# acting as proxy, which we need to skip. # acting as proxy, which we need to skip.
def locale=(value) def locale=(value)
if value if value
config = I18n.config.respond_to?(:i18n_config) ? I18n.config.i18n_config : I18n.config config = I18n.config.respond_to?(:original_config) ? I18n.config.original_config : I18n.config
config.locale = value config.locale = value
end end

View File

@ -73,7 +73,7 @@ class LookupContextTest < ActiveSupport::TestCase
assert_equal :pt, I18n.locale assert_equal :pt, I18n.locale
assert_equal :pt, @lookup_context.locale assert_equal :pt, @lookup_context.locale
ensure ensure
I18n.config = I18n.config.i18n_config I18n.config = I18n.config.original_config
end end
assert_equal :pt, I18n.locale assert_equal :pt, I18n.locale