Skip to content

Commit 0919225

Browse files
committed
Ensure that deleting an offsite link from a standard edition only removes the association, not the offsite link itself
In the existing implementation, deleting an offsite link from a standard edition would delete the offsite link record itself, which would affect other editions that are associated with the same offsite link. I've made sure only the association between the edition and the offsite link is removed, while retaining the offsite link record or any other editions that may be using it. This follows the established design from #11001
1 parent c056638 commit 0919225

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

app/controllers/admin/offsite_links_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ def update
3030
def confirm_destroy; end
3131

3232
def destroy
33-
@offsite_link.destroy!
33+
if @parent.is_a?(Edition)
34+
@parent.offsite_link_parents.find_by(offsite_link: @offsite_link)&.destroy!
35+
else
36+
@offsite_link.destroy!
37+
end
3438
flash[:notice] = "#{@offsite_link.title} has been deleted"
3539
redirect_to offsite_links_path
3640
end

test/functional/admin/offsite_links_controller_test.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,12 @@ class Admin::OffsiteLinksControllerTest < ActionController::TestCase
154154
assert_select "p", text: "Are you sure you want to delete \"#{offsite_link.title}\" from \"#{edition.title}\"?"
155155
end
156156

157-
view_test "DELETE :destroy removes offsite link for standard edition parent" do
157+
view_test "DELETE :destroy removes the association between an offsite link and an edition but does not delete the offsite link" do
158158
edition = create(:standard_edition, :published)
159159
offsite_link = create(:offsite_link, editions: [edition])
160-
assert_difference("OffsiteLink.count", -1) do
160+
161+
assert_equal [offsite_link], edition.offsite_links
162+
assert_no_difference("OffsiteLink.count") do
161163
delete :destroy, params: {
162164
standard_edition_id: edition, id: offsite_link.id
163165
}

0 commit comments

Comments
 (0)