From 071bcb7023d112708743ecba74936b3ba6dc9c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 11 Aug 2021 05:16:06 +0200 Subject: [PATCH] Add and apply Rails/I18nLocaleAssignment rule This rule was added in rubocop-rails 2.11.0. Although we prevent I18n locale leaking between tests by setting it before each test, the `with_locale` method makes the scope of the locale change more obvious. --- .rubocop.yml | 5 +++++ spec/i18n_spec.rb | 11 ++++++----- spec/models/budget_spec.rb | 20 ++++++++++++-------- spec/models/poll/question_spec.rb | 7 ++++--- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c239baeb6..250f2c94b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -248,6 +248,11 @@ Rails/HasManyOrHasOneDependent: Rails/HttpStatus: Enabled: true +Rails/I18nLocaleAssignment: + Enabled: true + Exclude: + - "spec/spec_helper.rb" + Rails/InverseOf: Enabled: true Exclude: diff --git a/spec/i18n_spec.rb b/spec/i18n_spec.rb index afe7af76b..15e588638 100644 --- a/spec/i18n_spec.rb +++ b/spec/i18n_spec.rb @@ -42,12 +42,13 @@ describe "I18n" do I18n.backend.store_translations(:zz, {}) I18n.enforce_available_locales = false - I18n.locale = :zz - I18n.fallbacks[:zz] << I18n.default_locale + I18n.with_locale(:zz) do + I18n.fallbacks[:zz] << I18n.default_locale - expect(I18n.t("test_plural", count: 0)).to eq("No comments") - expect(I18n.t("test_plural", count: 1)).to eq("1 comment") - expect(I18n.t("test_plural", count: 2)).to eq("2 comments") + expect(I18n.t("test_plural", count: 0)).to eq("No comments") + expect(I18n.t("test_plural", count: 1)).to eq("1 comment") + expect(I18n.t("test_plural", count: 2)).to eq("2 comments") + end end it "returns count if specific plural rule not present" do diff --git a/spec/models/budget_spec.rb b/spec/models/budget_spec.rb index 75c2e0234..11ca8e932 100644 --- a/spec/models/budget_spec.rb +++ b/spec/models/budget_spec.rb @@ -336,30 +336,34 @@ describe Budget do describe "#formatted_amount" do it "correctly formats Euros with Spanish" do budget.update!(currency_symbol: "€") - I18n.locale = :es - expect(budget.formatted_amount(1000.00)).to eq "1.000 €" + I18n.with_locale(:es) do + expect(budget.formatted_amount(1000.00)).to eq "1.000 €" + end end it "correctly formats Dollars with Spanish" do budget.update!(currency_symbol: "$") - I18n.locale = :es - expect(budget.formatted_amount(1000.00)).to eq "1.000 $" + I18n.with_locale(:es) do + expect(budget.formatted_amount(1000.00)).to eq "1.000 $" + end end it "correctly formats Dollars with English" do budget.update!(currency_symbol: "$") - I18n.locale = :en - expect(budget.formatted_amount(1000.00)).to eq "$1,000" + I18n.with_locale(:en) do + expect(budget.formatted_amount(1000.00)).to eq "$1,000" + end end it "correctly formats Euros with English" do budget.update!(currency_symbol: "€") - I18n.locale = :en - expect(budget.formatted_amount(1000.00)).to eq "€1,000" + I18n.with_locale(:en) do + expect(budget.formatted_amount(1000.00)).to eq "€1,000" + end end end diff --git a/spec/models/poll/question_spec.rb b/spec/models/poll/question_spec.rb index 056e86e95..de7155f5d 100644 --- a/spec/models/poll/question_spec.rb +++ b/spec/models/poll/question_spec.rb @@ -33,10 +33,11 @@ RSpec.describe Poll::Question, type: :model do end context "locale with non-underscored name" do - before { I18n.locale = :"pt-BR" } - it "correctly creates a translation" do - poll_question.copy_attributes_from_proposal(proposal) + I18n.with_locale(:"pt-BR") do + poll_question.copy_attributes_from_proposal(proposal) + end + translation = poll_question.translations.first expect(poll_question.title).to eq(proposal.title)