Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-slug HTML attachments if they are unpublished #5942

Merged
merged 4 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ def publishing_api_details_for_format
end

def deep_clone
dup
dup.tap do |clone|
clone.safely_resluggable = false
end
end

def external?
Expand Down
2 changes: 1 addition & 1 deletion app/models/html_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def url(options = {})
def should_generate_new_friendly_id?
return false unless sluggable_locale?

slug.nil? || attachable.nil? || !attachable.document.published?
slug.nil? || attachable.nil? || safely_resluggable?
end

def deep_clone
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddSafelyResluggableToAttachment < ActiveRecord::Migration[5.1]
def change
add_column :attachments, :safely_resluggable, :boolean, default: true
Attachment.update_all(safely_resluggable: false)
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20201130161652) do
ActiveRecord::Schema.define(version: 20210111161500) do

create_table "about_pages", id: :integer, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci" do |t|
t.integer "topical_event_id"
Expand Down Expand Up @@ -73,6 +73,7 @@
t.string "external_url"
t.string "content_id"
t.boolean "deleted", default: false, null: false
t.boolean "safely_resluggable", default: true
t.index ["attachable_id", "attachable_type"], name: "index_attachments_on_attachable_id_and_attachable_type"
t.index ["attachable_type", "attachable_id", "ordering"], name: "no_duplicate_attachment_orderings", unique: true
t.index ["attachment_data_id"], name: "index_attachments_on_attachment_data_id"
Expand Down
19 changes: 17 additions & 2 deletions test/unit/html_attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class HtmlAttachmentTest < ActiveSupport::TestCase
assert_equal "an-html-attachment", draft.attachments.first.slug
end

test "slug is updated when the title is changed if edition is unpublished" do
test "slug is updated when the title is changed if document has never been published" do
attachment = build(:html_attachment, title: "an-html-attachment")

create(:draft_publication, attachments: [attachment])
Expand All @@ -97,7 +97,7 @@ class HtmlAttachmentTest < ActiveSupport::TestCase
assert_equal "a-new-title", attachment.slug
end

test "slug is not updated when the title is changed if edition is published" do
test "slug on old attachment is not updated when the title is changed if document is published" do
edition = create(
:published_publication,
attachments: [
Expand All @@ -114,6 +114,21 @@ class HtmlAttachmentTest < ActiveSupport::TestCase
assert_equal "an-html-attachment", attachment.slug
end

test "slug on new attachment is updated when the title is changed if document is published" do
edition = create(
:published_publication,
)
draft = edition.create_draft(create(:writer))
attachment = build(:html_attachment, title: "an-html-attachment")
draft.attachments = [attachment]

attachment.title = "a-new-title"
attachment.save!
attachment.reload

assert_equal "a-new-title", attachment.slug
end

test "slug is not updated when the title has been changed in a prior published edition" do
edition = create(
:published_publication,
Expand Down