From d7dd02f81ae557a86b3eb70001e91617af977560 Mon Sep 17 00:00:00 2001 From: Sawy Date: Wed, 26 Feb 2025 10:22:51 +0200 Subject: [PATCH] CV2-6041: update suggested relation if new one is confirmed --- app/models/relationship.rb | 5 ++++- test/models/relationship_test.rb | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/relationship.rb b/app/models/relationship.rb index 5cef94695..641496ca3 100644 --- a/app/models/relationship.rb +++ b/app/models/relationship.rb @@ -161,7 +161,7 @@ def self.create_unless_exists(source_id, target_id, relationship_type, options = r = Relationship.where(target_id: target_id).where.not(source_id: source_id).last return r unless r.nil? # otherwise we should get the existing relationship based on source, target and type - r = Relationship.where(source_id: source_id, target_id: target_id).where('relationship_type = ?', relationship_type.to_yaml).last + r = Relationship.where(source_id: source_id, target_id: target_id).last exception_message = nil exception_class = nil if r.nil? @@ -196,6 +196,9 @@ def self.create_unless_exists(source_id, target_id, relationship_type, options = exception_message = e.message exception_class = e.class.name end + elsif r.relationship_type != relationship_type && relationship_type == Relationship.confirmed_type + r.relationship_type = relationship_type + r.save! end if r.nil? Rails.logger.error("[Relationship::create_unless_exists] returning nil: source_id #{source_id}, target_id #{target_id}, relationship_type #{relationship_type}.") diff --git a/test/models/relationship_test.rb b/test/models/relationship_test.rb index 92b49e047..fb7a4d2a0 100644 --- a/test/models/relationship_test.rb +++ b/test/models/relationship_test.rb @@ -341,7 +341,7 @@ def setup assert_no_difference 'Relationship.count' do r2 = Relationship.create_unless_exists(source.id, target.id, Relationship.confirmed_type) end - assert_nil Relationship.where(id: r.id).last + assert_equal r.id, r2.id assert_equal Relationship.confirmed_type, r2.relationship_type Relationship.any_instance.stubs(:save!).raises(ActiveRecord::RecordNotUnique) target = create_project_media team: t