Skip to content

Commit

Permalink
Merge pull request #9885 from alphagov/slugs
Browse files Browse the repository at this point in the history
Make 'identifier' slug-related behaviour more intuitive/flexible
  • Loading branch information
ChrisBAshton authored Feb 5, 2025
2 parents b2491b0 + f40e845 commit 7b9d103
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
8 changes: 1 addition & 7 deletions app/models/html_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def translated_locales
end

def identifier
return slug if slug_eligible?

content_id
slug || content_id
end

def base_path
Expand Down Expand Up @@ -131,10 +129,6 @@ def sluggable_string
sluggable_locale? ? title : nil
end

def slug_eligible?
title.ascii_only? && sluggable_locale?
end

def clear_slug_if_non_english_locale
if locale_changed? && !sluggable_locale?
self.slug = nil
Expand Down
20 changes: 20 additions & 0 deletions test/unit/app/models/html_attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,33 @@ class HtmlAttachmentTest < ActiveSupport::TestCase
assert_equal expected_slug, attachment.to_param
end

test "slug is created even if the English title contains non-ASCII characters (since it's highly unlikely to be ALL non-ASCII characters like other languages)" do
attachment = create(:html_attachment, locale: "en", title: "A page about copyright ©")

expected_slug = "a-page-about-copyright"
assert_equal expected_slug, attachment.slug
assert_equal expected_slug, attachment.to_param
end

test "slug is cleared when changing from english to non-english" do
attachment = create(:html_attachment, locale: "en")

attachment.update!(locale: "fr")
assert attachment.slug.blank?
end

test "#identifier falls back to content_id if no slug available" do
attachment = create(:html_attachment)
attachment.slug = nil
assert_equal attachment.content_id, attachment.identifier
end

test "#identifier uses the slug if it's been set, irrespective of locale" do
attachment = create(:html_attachment, locale: "cy")
attachment.slug = "foo"
assert_equal "foo", attachment.identifier
end

test "#translated_locales lists only the attachment's locale" do
assert_equal %w[en], HtmlAttachment.new.translated_locales
assert_equal %w[cy], HtmlAttachment.new(locale: "cy").translated_locales
Expand Down

0 comments on commit 7b9d103

Please sign in to comment.