Skip to content

Commit 76f1d0f

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 f6ecf06 commit 76f1d0f

2 files changed

Lines changed: 10 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,17 @@ 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 on a standard edition removes the offsite link association from the edition but retains the offsite link itself" 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
}
164166
end
167+
assert_equal [], edition.reload.offsite_links
165168
assert_response :redirect
166169
end
167170

0 commit comments

Comments
 (0)