DEV: Avoid nil custom fields (#154)

Included:

* DEV: Topic#custom_fields is always truthy
* DEV: Delete custom fields instead of setting nil
* DEV: Delete existing nil custom fields
This commit is contained in:
Jarek Radosz 2021-05-05 11:15:50 +02:00 committed by GitHub
parent 8d013f38b2
commit 573c608057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
class RemoveNilCustomFieldsFromAssign < ActiveRecord::Migration[6.0]
def up
execute <<~SQL
DELETE FROM topic_custom_fields
WHERE name = 'assigned_to_id' AND value IS NULL
SQL
execute <<~SQL
DELETE FROM topic_custom_fields
WHERE name = 'assigned_by_id' AND value IS NULL
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -148,7 +148,7 @@ class ::TopicAssigner
return { success: false, reason: reason }
end
return { success: false, reason: :forbidden_assign_to } unless can_be_assigned?(assign_to)
return { success: false, reason: :already_assigned } if @topic.custom_fields && @topic.custom_fields[ASSIGNED_TO_ID] == assign_to.id.to_s
return { success: false, reason: :already_assigned } if @topic.custom_fields[ASSIGNED_TO_ID] == assign_to.id.to_s
return { success: false, reason: :too_many_assigns } unless can_assign_to?(assign_to)
@topic.custom_fields[ASSIGNED_TO_ID] = assign_to.id
@ -279,8 +279,8 @@ class ::TopicAssigner
end
# clean up in memory object
@topic.custom_fields[ASSIGNED_TO_ID] = nil
@topic.custom_fields[ASSIGNED_BY_ID] = nil
@topic.custom_fields.delete(ASSIGNED_TO_ID)
@topic.custom_fields.delete(ASSIGNED_BY_ID)
# nothing to do here
return if !assigned_to_id