FIX: Unassign user titles when a badge is deleted (PR #9573)

Taking suggestions for improvements to where the code for clear_titles_for_granted_badge lives.

GitHub

This new constant seems unused and should probably be removed.

I feel like these 3 calls should now be in a Badge.transaction do block now, it would be awkward if we cleared all the titles but then failed to destroy the badge. Should be all-or-nothing.

Functionality looks great + tests too, just a few minor things :+1:

Nitpick, but why pass in title if you are just setting it to ''? SET title = ''

You are not setting the updated_at here

I think this should just be a method on the Badge model rather than putting it in the job, seeing as how the job doesn’t use it. Then you don’t have to pass the id either, so something like:

badge.clear_granted_user_titles

The naming of the method is a little tricky because it’s like “get rid of all the user titles that have been set based on me (the badge) being granted”

user_profiles does not have an updated_at

Ah OK, you can just remove it from the query params then

Also a note … all those .freeze calls are not needed since # frozen_string_literal: true will fix

Done!

This pull request has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/cant-delete-badges-that-users-have-as-a-title/143006/12