From d726362c6e72d9f387975b3b1054790867ee5162 Mon Sep 17 00:00:00 2001 From: manu vasconcelos Date: Thu, 19 Dec 2024 14:58:48 -0300 Subject: [PATCH 1/2] replace old_pm.id with old_pm_id Sometimes by the time we get to the line where we try to access old_pm.id, old_pm does not exist anymore. Because that line is just for auditing, versioning, we can just add old_pm_id (since we have access to that information) --- app/models/project_media.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/project_media.rb b/app/models/project_media.rb index 9e8b2e3b4..8cfc39e82 100644 --- a/app/models/project_media.rb +++ b/app/models/project_media.rb @@ -334,7 +334,7 @@ def self.apply_replace_by(old_pm_id, new_pm_id, options_json) item_id: new_pm.id.to_s, event: 'replace', whodunnit: options['author_id'].to_s, - object_changes: { pm_id: [old_pm.id, new_pm.id] }.to_json, + object_changes: { pm_id: [old_pm_id, new_pm.id] }.to_json, associated_id: new_pm.id, associated_type: 'ProjectMedia', team_id: new_pm.team_id, From 817c080a44cb624b8f2a6fbd54618d4701cd2ca2 Mon Sep 17 00:00:00 2001 From: manu vasconcelos Date: Thu, 19 Dec 2024 15:12:28 -0300 Subject: [PATCH 2/2] add test --- test/models/project_media_5_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/models/project_media_5_test.rb b/test/models/project_media_5_test.rb index ef8ba5a0d..8ef306782 100644 --- a/test/models/project_media_5_test.rb +++ b/test/models/project_media_5_test.rb @@ -885,6 +885,16 @@ def setup end end + test "should save history version even if the original project media does not exist anymore" do + t = create_team + old_pm_id = 123456 # something that does not exist anymore + new = create_project_media team: t + ProjectMedia.apply_replace_by(old_pm_id, new.id, "{\"author_id\":1234,\"assignments_ids\":[],\"skip_send_report\":true}") + + history = new.versions.first.object_changes + assert_equal history, { pm_id: [123456,new.id]}.to_json + end + test "should replace a blank project media by another project media" do setup_elasticsearch t = create_team