diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8b95d8ee4..20a57954d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -718,25 +718,6 @@ Rails/Delegate: - 'lib/mongoid/findable.rb' - 'lib/mongoid/scopable.rb' -# Offense count: 91 -# Configuration parameters: Include. -# Include: spec/**/*.rb, test/**/*.rb -Rails/I18nLocaleAssignment: - Exclude: - - 'spec/integration/i18n_fallbacks_spec.rb' - - 'spec/mongoid/contextual/memory_spec.rb' - - 'spec/mongoid/contextual/mongo_spec.rb' - - 'spec/mongoid/copyable_spec.rb' - - 'spec/mongoid/criteria/queryable/options_spec.rb' - - 'spec/mongoid/criteria/queryable/selector_spec.rb' - - 'spec/mongoid/criteria_projection_spec.rb' - - 'spec/mongoid/criteria_spec.rb' - - 'spec/mongoid/fields/localized_spec.rb' - - 'spec/mongoid/fields_spec.rb' - - 'spec/mongoid/persistable/updatable_spec.rb' - - 'spec/mongoid/validatable/uniqueness_spec.rb' - - 'spec/support/macros.rb' - # Offense count: 133 # Configuration parameters: ForbiddenMethods, AllowedMethods. # ForbiddenMethods: decrement!, decrement_counter, increment!, increment_counter, insert, insert!, insert_all, insert_all!, toggle!, touch, touch_all, update_all, update_attribute, update_column, update_columns, update_counters, upsert, upsert_all diff --git a/spec/integration/i18n_fallbacks_spec.rb b/spec/integration/i18n_fallbacks_spec.rb index 3d9b6b807..6a3fb8d58 100644 --- a/spec/integration/i18n_fallbacks_spec.rb +++ b/spec/integration/i18n_fallbacks_spec.rb @@ -8,38 +8,37 @@ context 'when fallbacks are enabled with a locale list' do with_default_i18n_configs - before do - I18n.fallbacks[:de] = [:en] - end + before { I18n.fallbacks[:de] = [:en] } + + let(:product) { Product.new } context 'when translation is present in active locale' do - it 'uses active locale' do - product = Product.new - I18n.locale = :de + around { |example| I18n.with_locale(:de) { example.run } } + + before do product.description = 'Marvelous in German' - I18n.locale = :en - product.description = 'Marvelous!' - I18n.locale = :de + I18n.with_locale(:en) { product.description = 'Marvelous!' } + end + + it 'uses active locale' do expect(product.description).to eq('Marvelous in German') end end context 'when translation is missing in active locale and present in fallback locale' do + around { |example| I18n.with_locale(:de) { example.run } } + before { I18n.with_locale(:en) { product.description = 'Marvelous!' } } + it 'falls back on default locale' do - product = Product.new - I18n.locale = :en - product.description = 'Marvelous!' - I18n.locale = :de expect(product.description).to eq('Marvelous!') end end context 'when translation is missing in all locales' do + around { |example| I18n.with_locale(:ru) { example.run } } + before { I18n.with_locale(:en) { product.description = 'Marvelous!' } } + it 'returns nil' do - product = Product.new - I18n.locale = :en - product.description = 'Marvelous!' - I18n.locale = :ru expect(product.description).to be_nil end end diff --git a/spec/mongoid/contextual/memory_spec.rb b/spec/mongoid/contextual/memory_spec.rb index 62749d432..d232a645e 100644 --- a/spec/mongoid/contextual/memory_spec.rb +++ b/spec/mongoid/contextual/memory_spec.rb @@ -459,52 +459,55 @@ context 'when getting a localized field' do with_default_i18n_configs - before do - I18n.locale = :en - d = Dictionary.create!(description: 'english-text') - I18n.locale = :de - d.description = 'deutsch-text' - d.save! - end - let(:criteria) do Dictionary.all.tap do |crit| crit.documents = [Dictionary.first] end end - context 'when getting the field without _translations' do - it 'gets the demongoized localized field' do - expect(context.distinct(:description)).to eq(['deutsch-text']) - end - end + context 'without fallbacks' do + let(:dictionary) { Dictionary.new } - context 'when getting the field with _translations' do + around { |example| I18n.with_locale(:de) { example.run } } - it 'gets the full hash' do - expect(context.distinct(:description_translations)).to eq([{ 'de' => 'deutsch-text', 'en' => 'english-text' }]) + before do + I18n.with_locale(:en) { dictionary.description = 'english-text' } + dictionary.description = 'deutsch-text' + dictionary.save! end - end - - context 'when plucking a specific locale' do - let(:distinct) do - context.distinct(:'description.de') + context 'when getting the field without _translations' do + it 'gets the demongoized localized field' do + expect(context.distinct(:description)).to eq(['deutsch-text']) + end end - it 'returns the specific translation' do - expect(distinct).to eq(['deutsch-text']) + context 'when getting the field with _translations' do + it 'gets the full hash' do + expect(context.distinct(:description_translations)).to eq([{ 'de' => 'deutsch-text', 'en' => 'english-text' }]) + end end - end - context 'when plucking a specific locale from _translations field' do + context 'when plucking a specific locale' do - let(:distinct) do - context.distinct(:'description_translations.de') + let(:distinct) do + context.distinct(:'description.de') + end + + it 'returns the specific translation' do + expect(distinct).to eq(['deutsch-text']) + end end - it 'returns the specific translations' do - expect(distinct).to eq(['deutsch-text']) + context 'when plucking a specific locale from _translations field' do + + let(:distinct) do + context.distinct(:'description_translations.de') + end + + it 'returns the specific translations' do + expect(distinct).to eq(['deutsch-text']) + end end end @@ -512,18 +515,18 @@ with_i18n_fallbacks with_default_i18n_configs - before do - I18n.fallbacks[:he] = [:en] - end - let(:distinct) do context.distinct(:description).first end + around { |example| I18n.with_locale(:he) { example.run } } + + before do + I18n.fallbacks[:he] = [:en] + I18n.with_locale(:en) { Dictionary.create!(description: 'english-text') } + end + it 'correctly uses the fallback' do - I18n.locale = :en - Dictionary.create!(description: 'english-text') - I18n.locale = :he expect(distinct).to eq 'english-text' end end @@ -533,10 +536,8 @@ let(:person) do p = Passport.new - I18n.locale = :en - p.name = 'Neil' - I18n.locale = :he - p.name = 'Nissim' + I18n.with_locale(:en) { p.name = 'Neil' } + I18n.with_locale(:he) { p.name = 'Nissim' } Person.create!(passport: p, employer_id: 12345) end @@ -559,6 +560,8 @@ context.distinct('pass.name_translations.en').first end + around { |example| I18n.with_locale(:he) { example.run } } + it 'returns the translation for the current locale' do expect(distinct).to eq('Nissim') end @@ -1624,66 +1627,70 @@ context 'when plucking a localized field' do with_default_i18n_configs - before do - I18n.locale = :en - d = Dictionary.create!(description: 'english-text') - I18n.locale = :de - d.description = 'deutsch-text' - d.save! - end - let(:criteria) do Dictionary.all.tap do |crit| crit.documents = [Dictionary.first] end end - context 'when plucking the entire field' do + context 'without fallbacks' do + let(:dictionary) { Dictionary.new } - let(:plucked) do - context.pluck(:description) - end + around { |example| I18n.with_locale(:de) { example.run } } - let(:plucked_translations) do - context.pluck(:description_translations) + before do + I18n.with_locale(:en) { dictionary.description = 'english-text' } + dictionary.description = 'deutsch-text' + dictionary.save! end - let(:plucked_translations_both) do - context.pluck(:description_translations, :description) - end + context 'when plucking the entire field' do - it 'returns the demongoized translations' do - expect(plucked.first).to eq('deutsch-text') - end + let(:plucked) do + context.pluck(:description) + end - it 'returns the full translations hash to _translations' do - expect(plucked_translations.first).to eq({ 'de' => 'deutsch-text', 'en' => 'english-text' }) - end + let(:plucked_translations) do + context.pluck(:description_translations) + end - it 'returns both' do - expect(plucked_translations_both.first).to eq([{ 'de' => 'deutsch-text', 'en' => 'english-text' }, 'deutsch-text']) - end - end + let(:plucked_translations_both) do + context.pluck(:description_translations, :description) + end - context 'when plucking a specific locale' do + it 'returns the demongoized translations' do + expect(plucked.first).to eq('deutsch-text') + end - let(:plucked) do - context.pluck(:'description.de') - end + it 'returns the full translations hash to _translations' do + expect(plucked_translations.first).to eq({ 'de' => 'deutsch-text', 'en' => 'english-text' }) + end - it 'returns the specific translations' do - expect(plucked.first).to eq('deutsch-text') + it 'returns both' do + expect(plucked_translations_both.first).to eq([{ 'de' => 'deutsch-text', 'en' => 'english-text' }, 'deutsch-text']) + end end - end - context 'when plucking a specific locale from _translations field' do + context 'when plucking a specific locale' do - let(:plucked) do - context.pluck(:'description_translations.de') + let(:plucked) do + context.pluck(:'description.de') + end + + it 'returns the specific translations' do + expect(plucked.first).to eq('deutsch-text') + end end - it 'returns the specific translations' do - expect(plucked.first).to eq('deutsch-text') + context 'when plucking a specific locale from _translations field' do + + let(:plucked) do + context.pluck(:'description_translations.de') + end + + it 'returns the specific translations' do + expect(plucked.first).to eq('deutsch-text') + end end end @@ -1691,18 +1698,18 @@ with_i18n_fallbacks with_default_i18n_configs - before do - I18n.fallbacks[:he] = [:en] - end - let(:plucked) do context.pluck(:description).first end + around { |example| I18n.with_locale(:he) { example.run } } + + before do + I18n.fallbacks[:he] = [:en] + I18n.with_locale(:en) { Dictionary.create!(description: 'english-text') } + end + it 'correctly uses the fallback' do - I18n.locale = :en - Dictionary.create!(description: 'english-text') - I18n.locale = :he expect(plucked).to eq 'english-text' end end @@ -1710,15 +1717,7 @@ context 'when the localized field is embedded' do with_default_i18n_configs - before do - p = Passport.new - I18n.locale = :en - p.name = 'Neil' - I18n.locale = :he - p.name = 'Nissim' - - Person.create!(passport: p, employer_id: 12345) - end + let(:passport) { Passport.new } let(:plucked) do Person.where(employer_id: 12345).pluck('pass.name').first @@ -1732,6 +1731,14 @@ Person.where(employer_id: 12345).pluck('pass.name_translations.en').first end + around { |example| I18n.with_locale(:he) { example.run } } + + before do + I18n.with_locale(:en) { passport.name = 'Neil' } + passport.name = 'Nissim' + Person.create!(passport: passport, employer_id: 12345) + end + it 'returns the translation for the current locale' do expect(plucked).to eq('Nissim') end @@ -2093,18 +2100,20 @@ let(:d3) { Dictionary.new(description: 'en1') } let(:d4) { Dictionary.new(description: 'en2') } + around { |example| I18n.with_locale(:en) { example.run } } + before do - I18n.locale = :en d1 d2 d3 d4 - I18n.locale = :de - d1.description = 'de1' - d2.description = 'de1' - d3.description = 'de2' - d4.description = 'de3' - I18n.locale = :en + + I18n.with_locale(:de) do + d1.description = 'de1' + d2.description = 'de1' + d3.description = 'de2' + d4.description = 'de3' + end end context 'when getting the demongoized field' do @@ -2188,20 +2197,22 @@ let(:address2a) { Address.new(name: 'en1') } let(:address2b) { Address.new(name: 'en3') } + around { |example| I18n.with_locale(:en) { example.run } } + before do - I18n.locale = :en address1a address1b address2a address2b - I18n.locale = :de - address1a.name = 'de1' - address1b.name = 'de2' - address2a.name = 'de1' - address2b.name = 'de3' - person1 - person2 - I18n.locale = :en + + I18n.with_locale(:de) do + address1a.name = 'de1' + address1b.name = 'de2' + address2a.name = 'de1' + address2b.name = 'de3' + person1 + person2 + end end context 'when getting the demongoized field' do diff --git a/spec/mongoid/contextual/mongo_spec.rb b/spec/mongoid/contextual/mongo_spec.rb index ed5447aae..07ee7d411 100644 --- a/spec/mongoid/contextual/mongo_spec.rb +++ b/spec/mongoid/contextual/mongo_spec.rb @@ -565,14 +565,6 @@ context 'when getting a localized field' do with_default_i18n_configs - before do - I18n.locale = :en - d = Dictionary.create!(description: 'english-text') - I18n.locale = :de - d.description = 'deutsch-text' - d.save! - end - let(:criteria) do Dictionary.criteria end @@ -581,6 +573,16 @@ described_class.new(criteria) end + let(:dictionary) { Dictionary.new } + + around { |example| I18n.with_locale(:de) { example.run } } + + before do + I18n.with_locale(:en) { dictionary.description = 'english-text' } + dictionary.description = 'deutsch-text' + dictionary.save! + end + context 'when getting the field without _translations' do it 'gets the demongoized localized field' do expect(context.distinct(:description)).to eq(['deutsch-text']) @@ -619,18 +621,16 @@ with_i18n_fallbacks with_default_i18n_configs + let(:distinct) { context.distinct(:description).first } + + around { |example| I18n.with_locale(:he) { example.run } } + before do I18n.fallbacks[:he] = [:en] - end - - let(:distinct) do - context.distinct(:description).first + I18n.with_locale(:en) { Dictionary.create!(description: 'english-text') } end it 'correctly uses the fallback' do - I18n.locale = :en - Dictionary.create!(description: 'english-text') - I18n.locale = :he expect(distinct).to eq('english-text') end end @@ -638,15 +638,7 @@ context 'when the localized field is embedded' do with_default_i18n_configs - before do - p = Passport.new - I18n.locale = :en - p.name = 'Neil' - I18n.locale = :he - p.name = 'Nissim' - - Person.create!(passport: p, employer_id: 12345) - end + let(:passport) { Passport.new } let(:criteria) do Person.where(employer_id: 12345) @@ -668,6 +660,14 @@ context.distinct('pass.name_translations.en').first end + around { |example| I18n.with_locale(:he) { example.run } } + + before do + I18n.with_locale(:en) { passport.name = 'Neil' } + passport.name = 'Nissim' + Person.create!(passport: passport, employer_id: 12345) + end + it 'returns the translation for the current locale' do expect(distinct).to eq('Nissim') end @@ -754,22 +754,24 @@ context 'when tallying a localized field' do with_default_i18n_configs + around { |example| I18n.with_locale(:en) { example.run } } + before do - I18n.locale = :en d1 = Dictionary.create!(description: 'en1') d2 = Dictionary.create!(description: 'en1') d3 = Dictionary.create!(description: 'en1') d4 = Dictionary.create!(description: 'en2') - I18n.locale = :de - d1.description = 'de1' - d2.description = 'de1' - d3.description = 'de2' - d4.description = 'de3' - d1.save! - d2.save! - d3.save! - d4.save! - I18n.locale = :en + + I18n.with_locale(:de) do + d1.description = 'de1' + d2.description = 'de1' + d3.description = 'de2' + d4.description = 'de3' + d1.save! + d2.save! + d3.save! + d4.save! + end end context 'when getting the demongoized field' do @@ -878,20 +880,22 @@ context 'when tallying an embedded localized field' do with_default_i18n_configs + around { |example| I18n.with_locale(:en) { example.run } } + before do - I18n.locale = :en address1a = Address.new(name: 'en1') address1b = Address.new(name: 'en2') address2a = Address.new(name: 'en1') address2b = Address.new(name: 'en3') - I18n.locale = :de - address1a.name = 'de1' - address1b.name = 'de2' - address2a.name = 'de1' - address2b.name = 'de3' - Person.create!(addresses: [address1a, address1b]) - Person.create!(addresses: [address2a, address2b]) - I18n.locale = :en + + I18n.with_locale(:de) do + address1a.name = 'de1' + address1b.name = 'de2' + address2a.name = 'de1' + address2b.name = 'de3' + Person.create!(addresses: [address1a, address1b]) + Person.create!(addresses: [address2a, address2b]) + end end context 'when getting the demongoized field' do diff --git a/spec/mongoid/copyable_spec.rb b/spec/mongoid/copyable_spec.rb index 726b83351..99a0575e9 100644 --- a/spec/mongoid/copyable_spec.rb +++ b/spec/mongoid/copyable_spec.rb @@ -166,10 +166,11 @@ with_default_i18n_configs before do - I18n.locale = 'pt_BR' - person.desc = 'descrição' - person.addresses.first.name = 'descrição' - person.save! + I18n.with_locale(:pt_BR) do + person.desc = 'descrição' + person.addresses.first.name = 'descrição' + person.save! + end end let!(:from_db) do @@ -180,29 +181,38 @@ from_db.send(method) end - it 'sets the pt_BR version' do - I18n.locale = 'pt_BR' - expect(copy.desc).to eq('descrição') - end + context 'with pt_BR locale' do + around { |example| I18n.with_locale(:pt_BR) { example.run } } - it 'sets the english version' do - I18n.locale = :en - expect(copy.desc).to eq('description') - end + it 'sets the pt_BR version' do + expect(copy.desc).to eq('descrição') + end - it 'sets to nil an nonexistent lang' do - I18n.locale = :fr - expect(copy.desc).to be_nil + it 'sets embedded translations' do + expect(copy.addresses.first.name).to eq('descrição') + end end - it 'sets embedded translations' do - I18n.locale = 'pt_BR' - expect(copy.addresses.first.name).to eq('descrição') + context 'with en locale' do + around { |example| I18n.with_locale(:en) { example.run } } + + it 'sets the english version' do + expect(copy.desc).to eq('description') + end + + it 'sets embedded english version' do + I18n.with_locale(:en) do + expect(copy.addresses.first.name).to eq('Bond') + end + end end - it 'sets embedded english version' do - I18n.locale = :en - expect(copy.addresses.first.name).to eq('Bond') + context 'with fr (nonexistent) locale' do + around { |example| I18n.with_locale(:fr) { example.run } } + + it 'sets the fallback (en) version' do + expect(copy.desc).to eq('description') + end end end @@ -213,8 +223,9 @@ person.addresses.build({ shipping_name: 'Title' }, ShipmentAddress) end + around { |example| I18n.with_locale(:pt_BR) { example.run } } + before do - I18n.locale = 'pt_BR' person.addresses.type(ShipmentAddress).each { |address| address.shipping_name = 'Título' } person.save! end @@ -228,7 +239,6 @@ end it 'sets embedded translations' do - I18n.locale = 'pt_BR' copy.addresses.type(ShipmentAddress).each do |address| expect(address.shipping_name).to eq('Título') end diff --git a/spec/mongoid/criteria/queryable/options_spec.rb b/spec/mongoid/criteria/queryable/options_spec.rb index eb7134e30..ec5e87df1 100644 --- a/spec/mongoid/criteria/queryable/options_spec.rb +++ b/spec/mongoid/criteria/queryable/options_spec.rb @@ -238,9 +238,7 @@ def localized? described_class.new({}, { 'key' => Field.new }) end - before do - I18n.locale = :de - end + around { |example| I18n.with_locale(:de) { example.run } } context 'when the criterion is simple' do diff --git a/spec/mongoid/criteria/queryable/selector_spec.rb b/spec/mongoid/criteria/queryable/selector_spec.rb index 9e54f9d4c..36474cfdc 100644 --- a/spec/mongoid/criteria/queryable/selector_spec.rb +++ b/spec/mongoid/criteria/queryable/selector_spec.rb @@ -797,9 +797,7 @@ def localized? described_class.new({}, { 'key' => Field.new }) end - before do - I18n.locale = :de - end + around { |example| I18n.with_locale(:de) { example.run } } context 'when the criterion is simple' do diff --git a/spec/mongoid/criteria_projection_spec.rb b/spec/mongoid/criteria_projection_spec.rb index 1a6b2dc93..b74882774 100644 --- a/spec/mongoid/criteria_projection_spec.rb +++ b/spec/mongoid/criteria_projection_spec.rb @@ -156,82 +156,93 @@ context 'when the field is localized' do with_default_i18n_configs + let(:dictionary) { Dictionary.new } + + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :en - d = Dictionary.create!(description: 'english-text') - I18n.locale = :de - d.description = 'deutsch-text' - d.save! + I18n.with_locale(:en) { dictionary.description = 'english-text' } + dictionary.description = 'deutsch-text' + dictionary.save! end context 'when entire field is included' do - let(:dictionary) do + let(:dict_descr) do Dictionary.only(:description).first end it 'loads all translations' do - expect(dictionary.description_translations.keys).to include('de', 'en') + expect(dict_descr.description_translations.keys).to include('de', 'en') end it 'returns the field value for the current locale' do - I18n.locale = :en - expect(dictionary.description).to eq('english-text') - I18n.locale = :de - expect(dictionary.description).to eq('deutsch-text') + expect(dict_descr.description).to eq('deutsch-text') + end + + it 'returns the field value for the en locale' do + I18n.with_locale(:en) do + expect(dict_descr.description).to eq('english-text') + end end end context 'when a specific locale is included' do - let(:dictionary) do + let(:dict_descr_de) do Dictionary.only(:'description.de').first end it 'loads translations only for the included locale' do - expect(dictionary.description_translations.keys).to include('de') - expect(dictionary.description_translations.keys).to_not include('en') + expect(dict_descr_de.description_translations.keys).to include('de') + expect(dict_descr_de.description_translations.keys).to_not include('en') end it 'returns the field value for the included locale' do - I18n.locale = :en - expect(dictionary.description).to be_nil - I18n.locale = :de - expect(dictionary.description).to eq('deutsch-text') + expect(dict_descr_de.description).to eq('deutsch-text') + end + + it 'doesnt return the field value for the excluded locale' do + I18n.with_locale(:en) do + expect(dict_descr_de.description).to be_nil + end end end context 'when entire field is excluded' do - let(:dictionary) do + let(:dict_wo_descr) do Dictionary.without(:description).first end it 'does not load all translations' do - expect(dictionary.description_translations.keys).to_not include('de', 'en') + expect(dict_wo_descr.description_translations.keys).to_not include('de', 'en') end it 'raises an Mongoid::Errors::AttributeNotLoaded when attempting to access the field' do - expect { dictionary.description }.to raise_error Mongoid::Errors::AttributeNotLoaded + expect { dict_wo_descr.description }.to raise_error Mongoid::Errors::AttributeNotLoaded end end context 'when a specific locale is excluded' do - let(:dictionary) do + let(:dict_wo_descr_de) do Dictionary.without(:'description.de').first end it 'does not load excluded translations' do - expect(dictionary.description_translations.keys).to_not include('de') - expect(dictionary.description_translations.keys).to include('en') + expect(dict_wo_descr_de.description_translations.keys).to_not include('de') + expect(dict_wo_descr_de.description_translations.keys).to include('en') end it 'returns nil for excluded translations' do - I18n.locale = :en - expect(dictionary.description).to eq('english-text') - I18n.locale = :de - expect(dictionary.description).to be_nil + expect(dict_wo_descr_de.description).to be_nil + end + + it 'returns the field value for included translations' do + I18n.with_locale(:en) do + expect(dict_wo_descr_de.description).to eq('english-text') + end end end end diff --git a/spec/mongoid/criteria_spec.rb b/spec/mongoid/criteria_spec.rb index dc9efc4bc..df4622640 100644 --- a/spec/mongoid/criteria_spec.rb +++ b/spec/mongoid/criteria_spec.rb @@ -1879,12 +1879,14 @@ context 'when plucking a localized field' do with_default_i18n_configs + let(:dictionary) { Dictionary.new } + + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :en - d = Dictionary.create!(description: 'english-text') - I18n.locale = :de - d.description = 'deutsch-text' - d.save! + I18n.with_locale(:en) { dictionary.description = 'english-text' } + dictionary.description = 'deutsch-text' + dictionary.save! end context 'when plucking the entire field' do @@ -1935,37 +1937,10 @@ end end - context 'when fallbacks are enabled with a locale list' do - with_i18n_fallbacks - - before do - I18n.fallbacks[:he] = [:en] - end - - let(:plucked) do - Dictionary.all.pluck(:description).first - end - - it 'correctly uses the fallback' do - I18n.locale = :en - Dictionary.create!(description: 'english-text') - I18n.locale = :he - expect(plucked).to eq('english-text') - end - end - context 'when the localized field is embedded' do with_default_i18n_configs - before do - p = Passport.new - I18n.locale = :en - p.name = 'Neil' - I18n.locale = :he - p.name = 'Nissim' - - Person.create!(passport: p, employer_id: 12345) - end + let(:passport) { Passport.new } let(:plucked) do Person.where(employer_id: 12345).pluck('pass.name').first @@ -1979,6 +1954,15 @@ Person.where(employer_id: 12345).pluck('pass.name_translations.en').first end + around { |example| I18n.with_locale(:he) { example.run } } + + before do + I18n.with_locale(:en) { passport.name = 'Neil' } + passport.name = 'Nissim' + + Person.create!(passport: passport, employer_id: 12345) + end + it 'returns the translation for the current locale' do expect(plucked).to eq('Nissim') end @@ -1993,6 +1977,31 @@ end end + context 'when plucking a localized field with fallbacks' do + with_default_i18n_configs + + around { |example| I18n.with_locale(:he) { example.run } } + + before do + I18n.fallbacks[:he] = [:en] + I18n.with_locale(:en) do + Dictionary.create!(description: 'english-text') + end + end + + context 'when fallbacks are enabled with a locale list' do + with_i18n_fallbacks + + let(:plucked) do + Dictionary.all.pluck(:description).first + end + + it 'correctly uses the fallback' do + expect(plucked).to eq('english-text') + end + end + end + context 'when plucking a field to be demongoized' do let(:plucked) do @@ -2286,16 +2295,14 @@ context 'when plucking a localized field' do - before do - I18n.locale = :en - d = Dictionary.create!(description: 'english-text') - I18n.locale = :de - d.description = 'deutsch-text' - d.save! - end + let(:dictionary) { Dictionary.new } - after do - I18n.locale = :en + around { |example| I18n.with_locale(:de) { example.run } } + + before do + I18n.with_locale(:en) { dictionary.description = 'english-text' } + dictionary.description = 'deutsch-text' + dictionary.save! end context 'when plucking the entire field' do @@ -2350,39 +2357,34 @@ expect(plucked.first).to eq('deutsch-text') end end + end + context 'when plucking a localized field with fallbacks' do context 'when fallbacks are enabled with a locale list' do with_i18n_fallbacks - around(:all) do |example| + let(:plucked) do + Dictionary.all.pluck_each(:description).first + end + + around do |example| prev_fallbacks = I18n.fallbacks.dup I18n.fallbacks[:he] = [:en] - example.run + I18n.with_locale(:he) { example.run } I18n.fallbacks = prev_fallbacks end - let(:plucked) do - Dictionary.all.pluck_each(:description).first + before do + I18n.with_locale { Dictionary.create!(description: 'english-text') } end it 'correctly uses the fallback' do - I18n.locale = :en - Dictionary.create!(description: 'english-text') - I18n.locale = :he expect(plucked).to eq 'english-text' end end context 'when the localized field is embedded' do - before do - p = Passport.new - I18n.locale = :en - p.name = 'Neil' - I18n.locale = :he - p.name = 'Nissim' - - Person.create!(passport: p, employer_id: 12345) - end + let(:passport) { Passport.new } let(:plucked) do Person.where(employer_id: 12345).pluck_each('pass.name').first @@ -2396,6 +2398,15 @@ Person.where(employer_id: 12345).pluck_each('pass.name_translations.en').first end + around { |example| I18n.with_locale(:he) { example.run } } + + before do + I18n.with_locale(:en) { passport.name = 'Neil' } + passport.name = 'Nissim' + + Person.create!(passport: passport, employer_id: 12345) + end + it 'returns the translation for the current locale' do expect(plucked).to eq('Nissim') end diff --git a/spec/mongoid/fields/localized_spec.rb b/spec/mongoid/fields/localized_spec.rb index 04c70c92a..4ab4331bd 100644 --- a/spec/mongoid/fields/localized_spec.rb +++ b/spec/mongoid/fields/localized_spec.rb @@ -83,9 +83,7 @@ context 'when a locale is provided' do with_default_i18n_configs - before do - I18n.locale = :de - end + around { |example| I18n.with_locale(:de) { example.run } } context 'when the value exists' do @@ -268,9 +266,7 @@ context 'when a locale is provided' do with_default_i18n_configs - before do - I18n.locale = :de - end + around { |example| I18n.with_locale(:de) { example.run } } context 'when the value exists' do @@ -302,6 +298,8 @@ context 'when fallbacks are defined' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do I18n.fallbacks[:de] = %i[de en es] end @@ -401,9 +399,7 @@ context 'when a locale is provided' do with_default_i18n_configs - before do - I18n.locale = :de - end + around { |example| I18n.with_locale(:de) { example.run } } let(:value) do field.mongoize('This is a test') @@ -435,9 +431,7 @@ context 'when a locale is provided' do with_default_i18n_configs - before do - I18n.locale = :de - end + around { |example| I18n.with_locale(:de) { example.run } } let(:value) do field.mongoize('100') @@ -451,9 +445,7 @@ context 'when the type is Boolean' do with_default_i18n_configs - before do - I18n.locale = :de - end + around { |example| I18n.with_locale(:de) { example.run } } context 'when the value is false' do @@ -489,14 +481,15 @@ with_i18n_fallbacks context 'when the lookup does not need to use fallbacks' do + with_default_i18n_configs - context 'when the value is false' do - with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } - before do - I18n.fallbacks[:de] = %i[en es] - end + before do + I18n.fallbacks[:de] = %i[en es] + end + context 'when the value is false' do let(:field) do described_class.new(:boolean_value, localize: true, type: Mongoid::Boolean) end @@ -513,6 +506,8 @@ context 'when the value is true' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do I18n.fallbacks[:de] = %i[en es] end diff --git a/spec/mongoid/fields_spec.rb b/spec/mongoid/fields_spec.rb index 5c0158ac6..778b993b7 100644 --- a/spec/mongoid/fields_spec.rb +++ b/spec/mongoid/fields_spec.rb @@ -15,10 +15,12 @@ context 'when translations exist' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :en - product.description = 'test' - I18n.locale = :de + I18n.with_locale(:en) do + product.description = 'test' + end product.description = 'The best' end @@ -525,8 +527,9 @@ context 'when a single locale is set' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :de product.description = 'The best' end @@ -542,10 +545,10 @@ context 'when multiple locales are set' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :end - product.description = 'Cheap drinks' - I18n.locale = :de + I18n.with_locale(:en) { product.description = 'Cheap drinks' } product.description = 'Cheaper drinks' end @@ -871,8 +874,9 @@ context 'when a locale is set' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :de product.description = 'Cheaper drinks' end @@ -888,10 +892,10 @@ context 'when having multiple locales' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :en - product.description = 'Cheap drinks' - I18n.locale = :de + I18n.with_locale(:en) { product.description = 'Cheap drinks' } product.description = 'Cheaper drinks' end @@ -2052,10 +2056,10 @@ class DiscriminatorChild2 < DiscriminatorParent context 'when assigning an empty string' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :en - product.title = 'hello' - I18n.locale = :de + I18n.with_locale(:en) { product.title = 'hello' } product.title = 'hello there!' product.title = '' end @@ -2072,10 +2076,10 @@ class DiscriminatorChild2 < DiscriminatorParent context 'when assigning nil' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :en - product.title = 'hello' - I18n.locale = :de + I18n.with_locale(:en) { product.title = 'hello' } product.title = 'hello there!' product.title = nil end @@ -2092,10 +2096,10 @@ class DiscriminatorChild2 < DiscriminatorParent context 'when assigning an empty array' do with_default_i18n_configs + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :en - product.title = 'hello' - I18n.locale = :de + I18n.with_locale(:en) { product.title = 'hello' } product.title = 'hello there!' product.title = [] end diff --git a/spec/mongoid/persistable/updatable_spec.rb b/spec/mongoid/persistable/updatable_spec.rb index ec3124f05..a985fb027 100644 --- a/spec/mongoid/persistable/updatable_spec.rb +++ b/spec/mongoid/persistable/updatable_spec.rb @@ -236,15 +236,20 @@ context 'when persisting a localized field' do with_default_i18n_configs - let!(:product) do - Product.create!(description: 'The bomb') - end + let!(:product) { Product.new } + let(:attributes) do product.attributes['description'] end + around { |example| I18n.with_locale(:de) { example.run } } + before do - I18n.locale = :de + I18n.with_locale(:en) do + product.description = 'The bomb' + product.save! + end + product.update_attribute(:description, 'Die Bombe') end diff --git a/spec/mongoid/validatable/uniqueness_spec.rb b/spec/mongoid/validatable/uniqueness_spec.rb index 5b7d9582a..ba1112e2c 100644 --- a/spec/mongoid/validatable/uniqueness_spec.rb +++ b/spec/mongoid/validatable/uniqueness_spec.rb @@ -2521,8 +2521,9 @@ class SpanishActor < EuropeanActor context 'when using a different locale' do with_default_i18n_configs + around { |example| I18n.with_locale(:fr) { example.run } } + before do - I18n.locale = :fr # Translation key location is as per rails-i18n gem. # See: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en.yml I18n.backend.store_translations(:fr, { errors: { messages: { taken: 'est déjà utilisé(e)' } } }) diff --git a/spec/support/macros.rb b/spec/support/macros.rb index a99b331ef..420c586e9 100644 --- a/spec/support/macros.rb +++ b/spec/support/macros.rb @@ -110,13 +110,12 @@ def time_zone_override(time_zone) def with_default_i18n_configs around do |example| - I18n.locale = :en I18n.default_locale = :en I18n.try(:fallbacks=, I18n::Locale::Fallbacks.new) I18n.enforce_available_locales = false - example.run + I18n.with_locale(:en) { example.run } ensure - I18n.locale = :en + # I18n.locale = :en I18n.default_locale = :en I18n.try(:fallbacks=, I18n::Locale::Fallbacks.new) I18n.enforce_available_locales = false