Skip to content

Commit 83144e9

Browse files
committed
Add a rake task to reslug an edition
We had a case where a superseded edition had the same slug as a new document. This meant that the new document could not be published under the ideal slug until the superseded edition frees the slug. The new document's slug had had '--2' appended to it, which is not ideal. I've now added a rake task to reslug an edition, which will allow us to fix issues like this. Instead of receiving the old slug as an argument, the task receives the edition's ID, just to be sure we're reslugging the intended edition. Usage: `bundle exec rake reslug:edition[12345,new-slug]`
1 parent d60aea6 commit 83144e9

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

lib/tasks/reslugging.rake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,32 @@ namespace :reslug do
113113
statistics_announcement = StatisticsAnnouncement.find_by!(slug: args.old_slug)
114114
statistics_announcement.update!(slug: args.new_slug)
115115
end
116+
117+
desc "Override the slug of an Edition (DANGER!).\n
118+
Use this to update the slug of an edition.
119+
Usage: `bundle exec rake reslug:edition[edition_id,new-slug]`
120+
121+
It performs the following steps:
122+
- sets slug_override on the edition
123+
- pushes the updated edition to Publishing API"
124+
task :edition, %i[edition_id new_slug] => :environment do |_task, args|
125+
raise "Please provide both the edition ID and the new slug as arguments, e.g. `bundle exec rake reslug:edition[12345,new-slug]`" if args[:edition_id].blank? || args[:new_slug].blank?
126+
127+
edition = Edition.find(args[:edition_id])
128+
user = User.where(name: "Scheduled Publishing Robot", uid: nil).first
129+
130+
puts "Updating edition: #{edition.title}'s slug: '#{edition.slug}' -> '#{args[:new_slug]}'"
131+
132+
edition.slug_override = args[:new_slug]
133+
edition.editorial_remarks.create!(
134+
body: "Slug updated from '#{edition.slug}' to '#{args[:new_slug]}' by publishing support.",
135+
author: user,
136+
)
137+
edition.save!(validate: false)
138+
139+
presenter = PublishingApiPresenters.presenter_for(edition)
140+
Services.publishing_api.put_content(presenter.content_id, presenter.content)
141+
142+
puts "Done."
143+
end
116144
end

test/unit/lib/tasks/reslugging_test.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,45 @@ class ResluggingTest < ActiveSupport::TestCase
8383
end
8484
end
8585
end
86+
87+
describe "for edition" do
88+
let(:task) { Rake::Task["reslug:edition"] }
89+
90+
setup do
91+
create(:scheduled_publishing_robot)
92+
end
93+
94+
test "it creates a slug override" do
95+
edition = create(:draft_edition, slug_override: "old-slug")
96+
97+
task.invoke(edition.id, "new-slug")
98+
99+
edition.reload
100+
assert_equal "new-slug", edition.slug
101+
assert_equal "new-slug", edition.slug_override
102+
end
103+
104+
test "it creates an editorial remark" do
105+
edition = create(:draft_edition, slug_override: "old-slug--2")
106+
107+
task.invoke(edition.id, "new-slug")
108+
109+
remark = edition.editorial_remarks.last
110+
assert_not_nil remark
111+
assert_includes remark.body, "old-slug--2"
112+
assert_includes remark.body, "new-slug"
113+
end
114+
115+
test "it raises when edition_id is missing" do
116+
assert_raises(RuntimeError) do
117+
task.invoke("", "old-slug")
118+
end
119+
end
120+
121+
test "it raises when new_slug is missing" do
122+
assert_raises(RuntimeError) do
123+
task.invoke("12345", "")
124+
end
125+
end
126+
end
86127
end

0 commit comments

Comments
 (0)