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.
This commit is contained in:
Javi Martín
2021-08-11 05:16:06 +02:00
parent 2bfa9068f1
commit 071bcb7023
4 changed files with 27 additions and 16 deletions

View File

@@ -248,6 +248,11 @@ Rails/HasManyOrHasOneDependent:
Rails/HttpStatus:
Enabled: true
Rails/I18nLocaleAssignment:
Enabled: true
Exclude:
- "spec/spec_helper.rb"
Rails/InverseOf:
Enabled: true
Exclude:

View File

@@ -42,13 +42,14 @@ describe "I18n" do
I18n.backend.store_translations(:zz, {})
I18n.enforce_available_locales = false
I18n.locale = :zz
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")
end
end
it "returns count if specific plural rule not present" do
keys = { zero: "No comments" }

View File

@@ -336,32 +336,36 @@ describe Budget do
describe "#formatted_amount" do
it "correctly formats Euros with Spanish" do
budget.update!(currency_symbol: "")
I18n.locale = :es
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
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
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
I18n.with_locale(:en) do
expect(budget.formatted_amount(1000.00)).to eq "€1,000"
end
end
end
describe "#investments_milestone_tags" do
let(:investment1) { build(:budget_investment, :winner) }

View File

@@ -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
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)