Skip to content

Commit 6684fe2

Browse files
authored
MONGOID-5437 extract fallback enabling logic into macro (#5428)
* MONGOID-5438 initial changes from Johnny's PR * MONGOID-5438 additional changes from johnny's PR * MONGOID-5437 fix tests * MONGOID-5437 fix uniqueness test
1 parent ecb65b2 commit 6684fe2

File tree

8 files changed

+27
-41
lines changed

8 files changed

+27
-41
lines changed

spec/integration/i18n_fallbacks_spec.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
require 'spec_helper'
44

55
describe 'i18n fallbacks' do
6-
# These tests modify the environment
7-
before(:all) do
8-
unless %w(yes true 1).include?((ENV['TEST_I18N_FALLBACKS'] || '').downcase)
9-
skip 'Set TEST_I18N_FALLBACKS=1 environment variable to run these tests'
10-
end
11-
end
12-
13-
before(:all) do
14-
puts "I18n version: #{I18n::VERSION}"
15-
16-
require "i18n/backend/fallbacks"
17-
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
18-
end
6+
with_i18n_fallbacks
197

208
context 'when fallbacks are enabled with a locale list' do
219
before do
@@ -36,15 +24,13 @@
3624
end
3725

3826
context 'when translation is missing in active locale and present in fallback locale' do
39-
4027
it 'falls back on default locale' do
4128
product = Product.new
4229
I18n.locale = :en
4330
product.description = "Marvelous!"
4431
I18n.locale = :de
4532
product.description.should == 'Marvelous!'
4633
end
47-
4834
end
4935

5036
context 'when translation is missing in all locales' do
@@ -64,7 +50,6 @@
6450
I18n.locale = :ru
6551
product.description.should be nil
6652
end
67-
6853
end
6954

7055
context 'i18n 1.0' do
@@ -82,7 +67,6 @@
8267
I18n.locale = :ru
8368
product.description.should == 'Marvelous!'
8469
end
85-
8670
end
8771
end
8872
end

spec/mongoid/contextual/memory_spec.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@
576576
end
577577

578578
context 'when fallbacks are enabled with a locale list' do
579-
require_fallbacks
579+
with_i18n_fallbacks
580580

581581
around(:all) do |example|
582582
prev_fallbacks = I18n.fallbacks.dup
@@ -595,7 +595,7 @@
595595

596596
it "correctly uses the fallback" do
597597
I18n.locale = :en
598-
d = Dictionary.create!(description: 'english-text')
598+
Dictionary.create!(description: 'english-text')
599599
I18n.locale = :he
600600
distinct.should == "english-text"
601601
end
@@ -1766,7 +1766,7 @@
17661766
end
17671767

17681768
context 'when fallbacks are enabled with a locale list' do
1769-
require_fallbacks
1769+
with_i18n_fallbacks
17701770

17711771
around(:all) do |example|
17721772
prev_fallbacks = I18n.fallbacks.dup
@@ -1785,7 +1785,7 @@
17851785

17861786
it "correctly uses the fallback" do
17871787
I18n.locale = :en
1788-
d = Dictionary.create!(description: 'english-text')
1788+
Dictionary.create!(description: 'english-text')
17891789
I18n.locale = :he
17901790
plucked.should == "english-text"
17911791
end
@@ -2128,7 +2128,6 @@
21282128
address2b.name = "de3"
21292129
person1
21302130
person2
2131-
21322131
I18n.locale = :en
21332132
end
21342133

spec/mongoid/contextual/mongo_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@
660660
end
661661

662662
context 'when fallbacks are enabled with a locale list' do
663-
require_fallbacks
663+
with_i18n_fallbacks
664664

665665
around(:all) do |example|
666666
prev_fallbacks = I18n.fallbacks.dup
@@ -686,7 +686,7 @@
686686

687687
it "correctly uses the fallback" do
688688
I18n.locale = :en
689-
d = Dictionary.create!(description: 'english-text')
689+
Dictionary.create!(description: 'english-text')
690690
I18n.locale = :he
691691
distinct.should == "english-text"
692692
end
@@ -854,7 +854,6 @@
854854
d2.save!
855855
d3.save!
856856
d4.save!
857-
858857
I18n.locale = :en
859858
end
860859

@@ -908,7 +907,6 @@
908907
address2b.name = "de3"
909908
Person.create!(addresses: [ address1a, address1b ])
910909
Person.create!(addresses: [ address2a, address2b ])
911-
912910
I18n.locale = :en
913911
end
914912

spec/mongoid/criteria_projection_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@
171171

172172
let(:dictionary) do
173173
Dictionary.only(:description).first
174-
175174
end
176175

177176
it 'loads all translations' do

spec/mongoid/criteria_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@
20592059
end
20602060

20612061
context 'when fallbacks are enabled with a locale list' do
2062-
require_fallbacks
2062+
with_i18n_fallbacks
20632063

20642064
around(:all) do |example|
20652065
prev_fallbacks = I18n.fallbacks.dup

spec/mongoid/fields/localized_spec.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@
128128

129129
context "when using fallbacks" do
130130

131-
before(:all) do
132-
require "i18n/backend/fallbacks"
133-
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
134-
end
131+
with_i18n_fallbacks
135132

136133
context "when fallbacks are defined" do
137134

@@ -306,10 +303,7 @@
306303

307304
context "when using fallbacks" do
308305

309-
before(:all) do
310-
require "i18n/backend/fallbacks"
311-
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
312-
end
306+
with_i18n_fallbacks
313307

314308
context "when fallbacks are defined" do
315309

@@ -513,10 +507,7 @@
513507

514508
context "when fallbacks are defined" do
515509

516-
before(:all) do
517-
require "i18n/backend/fallbacks"
518-
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
519-
end
510+
with_i18n_fallbacks
520511

521512
context "when the lookup does not need to use fallbacks" do
522513

spec/mongoid/validatable/uniqueness_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2476,7 +2476,6 @@ class SpanishActor < EuropeanActor
24762476
describe "i18n" do
24772477

24782478
context 'when using a different locale' do
2479-
24802479
around do |example|
24812480
I18n.with_locale(:fr) { example.run }
24822481
end

spec/support/macros.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
module Mongoid
44
module Macros
5+
class I18nBackendWithFallbacks < I18n::Backend::Simple
6+
include I18n::Backend::Fallbacks
7+
end
8+
59
def use_spec_mongoid_config
610
around do |example|
711
config_path = File.join(File.dirname(__FILE__), "..", "config", "mongoid.yml")
@@ -39,6 +43,18 @@ def with_config_values(key, *values, &block)
3943
end
4044
end
4145

46+
def with_i18n_fallbacks
47+
require_fallbacks
48+
49+
around do |example|
50+
old_backend = I18n.backend
51+
I18n.backend = I18nBackendWithFallbacks.new
52+
example.run
53+
ensure
54+
I18n.backend = old_backend
55+
end
56+
end
57+
4258
def driver_config_override(key, value)
4359
around do |example|
4460
existing = Mongo.send(key)

0 commit comments

Comments
 (0)