Skip to content

Commit

Permalink
Merge pull request #8656 from alphagov/ensure-a-documents-slug-is-not…
Browse files Browse the repository at this point in the history
…-set-to-nil-for-special-characters

Ensure document retains slug when edition title has special chars
  • Loading branch information
davidgisbey authored Dec 19, 2023
2 parents ba8c1f3 + d120258 commit ca06986
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def update_slug_if_possible(new_title)
candidate_slug = normalize_friendly_id(new_title)
unless candidate_slug == slug
update!(sluggable_string: new_title)
# when special characters or scipts are used from the non-latin alphabets
# friendly_id sets the documents slug to nil. This ensures that it
# retains the default behaviour id of the document as the slug rather than being nil
# as implemented in the #ensure_document_has_a_slug after_create callback
ensure_document_has_a_slug
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Document.where(slug: nil).each do |document|
document.update_column(:slug, document.id.to_s)
end
24 changes: 24 additions & 0 deletions test/unit/app/models/document_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,28 @@ class DocumentTest < ActiveSupport::TestCase
assert_nil unpublished.document.live_edition
assert_nil draft.document.live_edition
end

test "#update_slug_if_possible updates the slug to the title passed maps to a diffrent slug" do
document = build(:document)
new_slug = "new_slug"
document.expects(:update!).with(sluggable_string: new_slug).once

document.update_slug_if_possible(new_slug)
end

test "#update_slug_if_possible does nothing to the slug if the title passed in maps to the current slug" do
document = build_stubbed(:document)
document.expects(:update!).never

document.update_slug_if_possible(document.slug)
end

test "#update_slug_if_possible ensures that the slug is set to the documents id if the new title contains special characters" do
document = create(:document, slug: nil)
slug_with_special_characters = "首次中英高级别安全"

document.update_slug_if_possible(slug_with_special_characters)

assert_equal document.id.to_s, document.reload.slug
end
end

0 comments on commit ca06986

Please sign in to comment.