Skip to content

Commit

Permalink
Ensure document retains slug when edition title has special chars
Browse files Browse the repository at this point in the history
We've been doing some investigative work into a bunch of sentry errors.
I've gone down a bit of a rabbit hole and came across some fairly strange
behaviour.

There are a subset of documents in our database that don't have a slug.
This is despite their being an after create for document which sets the
slug to the documents id if one is not set based on the title of the
first edition for whatever reason.

This is being overriden by the #update_slug_if_possible method which is
triggered by the #update_document_slug before_validation method.

When the #update_slug_if_possible is passed a special character or characters
from other languages that different alphabets are used friendly_id is unable
to handle it and updates the slug to nil.

We want to retain the default behaviour of the slug being defaulted to the
id of the document. This commit ensures that's the case.
  • Loading branch information
davidgisbey committed Dec 19, 2023
1 parent dafeb49 commit 391f33d
Show file tree
Hide file tree
Showing 2 changed files with 29 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
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 391f33d

Please sign in to comment.