Skip to content

Commit

Permalink
Move claim/fact-check/report from child to parent when creating relat…
Browse files Browse the repository at this point in the history
…ionship if child has a published report but the parent doesn't (#2127)

* Previously: We have items A and B. B has a published report. If we try to add B as similar to A it's going to fail because B has a published report.
* Now: Move the fact-check from B to A if A doesn't have a fact-check. Remain failing if A already has a fact-check.

Reference: CV2-5652.
  • Loading branch information
caiosba authored Nov 12, 2024
1 parent ee9b898 commit a4f9ea8
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
29 changes: 29 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Relationship < ApplicationRecord

before_validation :set_user, on: :create
before_validation :set_confirmed, if: :is_being_confirmed?, on: :update
before_validation :move_fact_check_and_report_to_main, on: :create
validate :relationship_type_is_valid, :items_are_from_the_same_team
validate :target_not_published_report, on: :create
validate :similar_item_exists, on: :create, if: proc { |r| r.is_suggested? }
Expand Down Expand Up @@ -366,4 +367,32 @@ def destroy_same_suggested_item
def cant_be_related_to_itself
errors.add(:base, I18n.t(:item_cant_be_related_to_itself)) if self.source_id == self.target_id
end

def move_fact_check_and_report_to_main
Relationship.transaction do
source = self.source
target = self.target
if source && target && source.team_id == target.team_id # Must verify since this method runs before the validation
target_report = target.get_annotations('report_design').to_a.map(&:load).last

# If the child item has a claim/fact-check and published report but the parent item doesn't, then move the claim/fact-check/report from the child to the parent
if !source.claim_description && target.claim_description && target_report && target_report.get_field_value('state') == 'published'
# Move report
target_report.annotated_id = source.id
target_report.save!

# Move claim/fact-check
claim_description = target.claim_description
claim_description.project_media = source
claim_description.save!

# Clear caches
source.clear_cached_fields
target.clear_cached_fields
source.reload
target.reload
end
end
end
end
end
63 changes: 61 additions & 2 deletions test/models/relationship_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

class Relationship2Test < ActiveSupport::TestCase
def setup
super
Sidekiq::Testing.inline!
Sidekiq::Testing.fake!
@team = create_team
@project = create_project team: @team
end

def teardown
User.current = Team.current = nil
end

test "should create relationship" do
assert_difference 'Relationship.count' do
create_relationship
Expand Down Expand Up @@ -54,6 +57,7 @@ def setup
end

test "should destroy relationships when project media is destroyed" do
Sidekiq::Testing.inline!
pm = create_project_media team: @team
pm2 = create_project_media team: @team
pm3 = create_project_media team: @team
Expand Down Expand Up @@ -140,6 +144,7 @@ def setup
end

test "should archive or restore medias when source is archived or restored" do
Sidekiq::Testing.inline!
RequestStore.store[:skip_delete_for_ever] = true
s = create_project_media project: @project
t1 = create_project_media project: @project
Expand All @@ -159,6 +164,7 @@ def setup
end

test "should delete medias when source is deleted" do
Sidekiq::Testing.inline!
s = create_project_media project: @project
t1 = create_project_media project: @project
t2 = create_project_media project: @project
Expand Down Expand Up @@ -205,6 +211,7 @@ def setup
end

test "should have versions" do
Sidekiq::Testing.inline!
with_versioning do
u = create_user is_admin: true
t = create_team
Expand Down Expand Up @@ -243,6 +250,7 @@ def setup
end

test "should propagate change if source and target are swapped" do
Sidekiq::Testing.inline!
u = create_user is_admin: true
t = create_team
with_current_user_and_team(u, t) do
Expand Down Expand Up @@ -342,6 +350,7 @@ def setup

test "should cache the name of who confirmed a similar item and store confirmation information" do
RequestStore.store[:skip_cached_field_update] = false
Sidekiq::Testing.inline!
t = create_team
u = create_user is_admin: true
pm1 = create_project_media team: t
Expand All @@ -361,4 +370,54 @@ def setup
r.destroy!
assert_queries(0, '=') { assert_nil pm2.confirmed_as_similar_by_name }
end

test "should move fact-check from child to parent when creating relationship if child has a fact-check but parent does not" do
t = create_team
child = create_project_media team: t
create_claim_description project_media: child
parent = create_project_media team: t
relationship = nil

# No report for any of them: No failure
assert_difference 'Relationship.count' do
assert_nothing_raised do
relationship = create_relationship source: parent, target: child, relationship_type: Relationship.confirmed_type
end
end
relationship.destroy!

# Child has a published report, but parent doesn't: No failure; claim/fact-check/report should be moved from the child to the parent
child = ProjectMedia.find(child.id)
parent = ProjectMedia.find(parent.id)
report = publish_report(child)
claim = child.reload.claim_description
assert_not_nil report
assert_not_nil claim
assert_not_nil child.reload.claim_description
assert_not_nil child.reload.get_dynamic_annotation('report_design')
assert_nil parent.reload.claim_description
assert_nil parent.reload.get_dynamic_annotation('report_design')
assert_difference 'Relationship.count' do
assert_nothing_raised do
relationship = create_relationship source: parent, target: child, relationship_type: Relationship.confirmed_type
end
end
assert_not_nil parent.reload.claim_description
assert_not_nil parent.reload.get_dynamic_annotation('report_design')
assert_equal claim, parent.reload.claim_description
assert_equal report, parent.reload.get_dynamic_annotation('report_design')
assert_nil child.reload.claim_description
assert_nil child.reload.get_dynamic_annotation('report_design')
relationship.destroy!

# Child has a published report, and parent has one too: Failure
child = ProjectMedia.find(child.id)
parent = ProjectMedia.find(parent.id)
publish_report(child)
assert_no_difference 'Relationship.count' do
assert_raises 'ActiveRecord::RecordInvalid' do
create_relationship source: parent, target: child, relationship_type: Relationship.confirmed_type
end
end
end
end

0 comments on commit a4f9ea8

Please sign in to comment.