Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset item status to default one when claim/fact-check is detached. #2064

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/models/claim_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ClaimDescription < ApplicationRecord
validate :cant_apply_article_to_item_if_article_is_in_the_trash
after_commit :update_fact_check, on: [:update]
after_update :update_report_status
after_update :reset_item_rating_if_removed
after_update :replace_media, unless: proc { |cd| cd.disable_replace_media }
after_update :migrate_claim_and_fact_check_logs, if: proc { |cd| cd.saved_change_to_project_media_id? && !cd.project_media_id.nil? }

Expand Down Expand Up @@ -120,4 +121,18 @@ def migrate_claim_and_fact_check_logs
def cant_apply_article_to_item_if_article_is_in_the_trash
errors.add(:base, I18n.t(:cant_apply_article_to_item_if_article_is_in_the_trash)) if self.project_media && self.fact_check&.trashed
end

# If claim/fact-check is detached from item, reset the item status/rating back to the default one (unstarted, undetermined, etc.)
def reset_item_rating_if_removed
if self.project_media_id.nil? && !self.project_media_id_before_last_save.nil?
old_pm = ProjectMedia.find_by_id(self.project_media_id_before_last_save)
return if old_pm.nil?
status = old_pm.last_status_obj
default_status = old_pm.team.verification_statuses('media')[:default]
if status && status.status != default_status
status.status = default_status
status.save
end
end
end
end
72 changes: 72 additions & 0 deletions test/models/fact_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,76 @@ def setup
assert_equal 1, fc2.tags.count
assert_equal 'one', fc2.tags.first
end

test "should move item to default core status when fact-check is removed from it" do
RequestStore.store[:skip_cached_field_update] = false
create_verification_status_stuff
Sidekiq::Testing.inline! do
pm = create_project_media
assert_equal 'undetermined', pm.reload.status
cd = create_claim_description(project_media: pm)
fc = create_fact_check claim_description: cd, rating: 'false'
assert_equal 'false', pm.reload.status
cd.project_media = nil
cd.save!
assert_equal 'undetermined', pm.reload.status
end
end

test "should move item to default custom status when fact-check is removed from it" do
RequestStore.store[:skip_cached_field_update] = false
create_verification_status_stuff
t = create_team
custom_statuses = {
"label": "Custom Status Label",
"active": "in_progress",
"default": "unstarted",
"statuses": [
{
"id": "unstarted",
"style": {
"color": "blue"
},
"locales": {
"en": {
"label": "Unstarted",
"description": "An item that did not start yet"
},
"pt": {
"label": "Não iniciado ainda",
"description": "Um item que ainda não começou a ser verificado"
}
}
},
{
"id": "in_progress",
"style": {
"color": "yellow"
},
"locales": {
"en": {
"label": "Working on it",
"description": "We are working on it"
},
"pt": {
"label": "Estamos trabalhando nisso",
"description": "Estamos trabalhando nisso"
}
}
}
]
}
t.set_media_verification_statuses(custom_statuses)
t.save!
Sidekiq::Testing.inline! do
pm = create_project_media team: t
assert_equal 'unstarted', pm.reload.status
cd = create_claim_description(project_media: pm)
fc = create_fact_check claim_description: cd, rating: 'in_progress'
assert_equal 'in_progress', pm.reload.status
cd.project_media = nil
cd.save!
assert_equal 'unstarted', pm.reload.status
end
end
end
Loading