From 0fab2bfd3dd3bb564098b5d71dbff545464a8598 Mon Sep 17 00:00:00 2001 From: Caio Almeida <117518+caiosba@users.noreply.github.com> Date: Fri, 22 Nov 2024 09:14:56 -0300 Subject: [PATCH] Update item cached fields when detaching fact-check. (#2133) Make sure that the `fact_check_*` cached fields for a `ProjectMedia` are refreshed when a fact-check is detached from that item. Fixes: CV2-5600. --- .../concerns/project_media_cached_fields.rb | 33 ++++++++++++------- test/models/fact_check_test.rb | 14 ++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/app/models/concerns/project_media_cached_fields.rb b/app/models/concerns/project_media_cached_fields.rb index c0d82e23f..bfad29395 100644 --- a/app/models/concerns/project_media_cached_fields.rb +++ b/app/models/concerns/project_media_cached_fields.rb @@ -70,14 +70,23 @@ def title_or_description_update } } - FACT_CHECK_EVENT = { - model: FactCheck, - affected_ids: proc { |fc| [fc.claim_description.project_media_id] }, - events: { - save: :recalculate, - destroy: :recalculate + FACT_CHECK_EVENTS = [ + { + model: FactCheck, + affected_ids: proc { |fc| [fc.claim_description.project_media_id] }, + events: { + save: :recalculate, + destroy: :recalculate + } + }, + { + model: ClaimDescription, + affected_ids: proc { |cd| [cd.project_media_id, cd.project_media_id_before_last_save] }, + events: { + save: :recalculate + } } - } + ] { is_suggested: Relationship.suggested_type, is_confirmed: Relationship.confirmed_type }.each do |field_name, _type| cached_field field_name, @@ -181,28 +190,28 @@ def title_or_description_update cached_field :fact_check_id, start_as: nil, recalculate: :recalculate_fact_check_id, - update_on: [FACT_CHECK_EVENT] + update_on: FACT_CHECK_EVENTS cached_field :fact_check_title, start_as: nil, recalculate: :recalculate_fact_check_title, - update_on: [FACT_CHECK_EVENT] + update_on: FACT_CHECK_EVENTS cached_field :fact_check_summary, start_as: nil, recalculate: :recalculate_fact_check_summary, - update_on: [FACT_CHECK_EVENT] + update_on: FACT_CHECK_EVENTS cached_field :fact_check_url, start_as: nil, recalculate: :recalculate_fact_check_url, - update_on: [FACT_CHECK_EVENT] + update_on: FACT_CHECK_EVENTS cached_field :fact_check_published_on, start_as: 0, update_es: true, recalculate: :recalculate_fact_check_published_on, - update_on: [FACT_CHECK_EVENT] + update_on: FACT_CHECK_EVENTS cached_field :description, recalculate: :recalculate_description, diff --git a/test/models/fact_check_test.rb b/test/models/fact_check_test.rb index 14204f746..88b8a70d0 100644 --- a/test/models/fact_check_test.rb +++ b/test/models/fact_check_test.rb @@ -714,4 +714,18 @@ def setup assert_equal 'Foo', pm.reload.report_text_title end end + + test "should reset cached fields when fact-check is detached" do + RequestStore.store[:skip_cached_field_update] = false + Sidekiq::Testing.inline! do + pm = create_project_media + cd = create_claim_description project_media: pm + fc = create_fact_check claim_description: cd, title: 'Foo' + assert_equal fc.id, pm.reload.fact_check_id + + cd.project_media = nil # Remove the claim/fact-check from the item + cd.save! + assert_nil pm.reload.fact_check_id + end + end end