Skip to content

Commit

Permalink
For CompetitionTab.slug to behave the same in all locales.
Browse files Browse the repository at this point in the history
This fixes thewca#2959.
  • Loading branch information
jfly committed Jun 14, 2018
1 parent 984a2dc commit 2e70711
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion WcaOnRails/app/models/competition_tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions WcaOnRails/spec/models/competition_tab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 2e70711

Please sign in to comment.