Skip to content

Commit

Permalink
Update item cached fields when detaching fact-check.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
caiosba committed Nov 21, 2024
1 parent 5770afe commit cf67dd3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
33 changes: 21 additions & 12 deletions app/models/concerns/project_media_cached_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions test/models/fact_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cf67dd3

Please sign in to comment.