AO3-4693 Fix collection deletion for collections with a challenge#5703
AO3-4693 Fix collection deletion for collections with a challenge#5703ayasayadi1 wants to merge 8 commits intootwcode:masterfrom
Conversation
app/models/collection.rb
Outdated
| end | ||
|
|
||
| belongs_to :challenge, dependent: :destroy, polymorphic: true | ||
| belongs_to :challenge, polymorphic: true |
There was a problem hiding this comment.
I think we can fix this just with callbacks. In collection, you can keep the association as-is:
otwarchive/app/models/collection.rb
Line 34 in e468bce
In both challenge models, you can update the association to use :nullify.
has_one :collection, as: :challenge, dependent: :nullify(which means you can fully remove all references of clear_challenge_references).
Rails 5+ should allow this bi-directional association to work (rails/rails#21350).
There was a problem hiding this comment.
Hi, thank you for reviewing!
dependent: :nullify clears the FK. But since we now bypass ActiveRecord callbacks, clean_up_challenge never fires. As a result, signups, assignments, prompts etc survive the deletion. Some GE and PM feature tests caught this.
Previously, the flow was:
before_destroy :clear_challenge_references-> challenge references cleared ->collection.save!after_save :clean_up_challenge-> associated records deleted- challenge deleted
Now it's:
- challenge deleted
:nullify-> challenge references cleared- associated records remain
I worked around this by moving clean_up_challenge into ChallengeCore and calling it via before_destroy, which restores the original behaviour
That said, I think this workflow is still risky: if the challenge deletion fails after before_destroy runs, we've already lost the associated records. Please lmk what you think :)
spec/models/collection_spec.rb
Outdated
| it "destroys the collection and its gift exchange challenge" do | ||
| challenge = create(:gift_exchange) | ||
| collection = create(:collection, challenge: challenge) | ||
| collection.destroy |
There was a problem hiding this comment.
You should use destroy! in specs, that way if there are errors you can easily see it in test logs.
There was a problem hiding this comment.
Can you also update gift_exchange_spec.rb and prompt_meme_spec.rb to test that a collection's challenge_id and challenge_type fields are nulled when the associated challenge is deleted?
Pull Request Checklist
as the first thing in your pull request title (e.g.
AO3-1234 Fix thing)until they are reviewed and merged before creating new pull requests.
Issue
https://otwarchive.atlassian.net/browse/AO3-4693
Purpose
This PR fixes deletion for collections with an associated challenge (gift exchange or prompt meme) by destroying the challenge first before the collection itself.
Credit
Aya Sayadi