diff --git a/app/services/section/remove_service.rb b/app/services/section/remove_service.rb index f1cc3c047..1fae5bace 100644 --- a/app/services/section/remove_service.rb +++ b/app/services/section/remove_service.rb @@ -20,6 +20,14 @@ def call minor_update: attributes.fetch(:minor_update, "0"), change_note: attributes.fetch(:change_note, ""), } + + # We need to capture the state of the section before assigning attributes. + # The Section#assign_attributes method always creates a new draft section if + # the latest edition is published. + # This causes Adapters.publishing.discard_section(section) to be called which + # blows up as there is no draft section in the Publishing API database. + draft_section = section.draft? + section.assign_attributes(change_note_params) if section.valid? @@ -30,7 +38,7 @@ def call manual.save!(user) Adapters.publishing.save_draft(manual, include_sections: false) - if section.draft? + if draft_section Adapters.publishing.discard_section(section) end end diff --git a/features/removing-a-manual-section.feature b/features/removing-a-manual-section.feature index 000f8203b..f4dcd1367 100644 --- a/features/removing-a-manual-section.feature +++ b/features/removing-a-manual-section.feature @@ -60,8 +60,7 @@ Feature: Removing a section from a manual Given I am logged in as a GDS editor And a published manual exists When I remove one of the sections from the manual - Then the section is removed from the manual - When I publish the manual + And I publish the manual Then the removed section is not published But the removed section is withdrawn with a redirect to the manual And the removed section is archived @@ -70,8 +69,7 @@ Feature: Removing a section from a manual Given I am logged in as an editor And a published manual exists When I remove one of the sections from the manual - Then the section is removed from the manual - When I publish the manual + And I publish the manual Then the removed section is not published But the removed section is withdrawn with a redirect to the manual And the removed section is archived @@ -87,14 +85,12 @@ Feature: Removing a section from a manual When I remove one of the sections from the manual with a major update omitting the note Then I see an error requesting that I provide a change note When I remove one of the sections from the manual with a major update - Then the section is removed from the manual - When I add another section and publish the manual later + And I add another section and publish the manual later Then the removed section change note is included Scenario: Removing a section with a minor update change notes Given I am logged in as a GDS editor And a published manual exists When I remove one of the sections from the manual with a minor update - Then the section is removed from the manual - When I add another section and publish the manual later + And I add another section and publish the manual later Then the removed section change note is not included