From 2e707119c2829754b94498a049e4745ac7fa4db6 Mon Sep 17 00:00:00 2001 From: Jeremy Fleischman Date: Thu, 14 Jun 2018 10:23:13 -0700 Subject: [PATCH] For CompetitionTab.slug to behave the same in all locales. This fixes #2959. --- WcaOnRails/app/models/competition_tab.rb | 5 ++++- WcaOnRails/spec/models/competition_tab_spec.rb | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/WcaOnRails/app/models/competition_tab.rb b/WcaOnRails/app/models/competition_tab.rb index bba8ee8234..9d17400397 100644 --- a/WcaOnRails/app/models/competition_tab.rb +++ b/WcaOnRails/app/models/competition_tab.rb @@ -18,7 +18,10 @@ class CompetitionTab < ApplicationRecord ).freeze def slug - "#{id}-#{name.parameterize}" + # parameterization behaves differently under different locales. However, we + # want slugs to be the same across all locales, so we intentionally wrap + # the call to parameterize in a I18n.with_locale. + I18n.with_locale(:en) { "#{id}-#{name.parameterize}" } end after_create :set_display_order diff --git a/WcaOnRails/spec/models/competition_tab_spec.rb b/WcaOnRails/spec/models/competition_tab_spec.rb index 0831b5ee19..2f0b72c14e 100644 --- a/WcaOnRails/spec/models/competition_tab_spec.rb +++ b/WcaOnRails/spec/models/competition_tab_spec.rb @@ -11,6 +11,14 @@ expect(CompetitionTab.column_names).to match_array(CompetitionTab::CLONEABLE_ATTRIBUTES + CompetitionTab::UNCLONEABLE_ATTRIBUTES) end + context "#slug" do + it "generates the same slug under different locales" do + competition_tab = FactoryBot.build(:competition_tab, id: 42, name: "Schedule / Расписание") + expect(I18n.with_locale(:en) { competition_tab.slug }).to eq "42-schedule" + expect(I18n.with_locale(:ru) { competition_tab.slug }).to eq "42-schedule" + end + end + context "#display_order" do let(:competition) { FactoryBot.create(:competition) } let(:other_competition) { FactoryBot.create(:competition) }