From d86e00dc28cc72507a4ec05c42a149ec0dae09de Mon Sep 17 00:00:00 2001 From: Manabu Niseki Date: Sun, 14 Jan 2024 11:06:39 +0900 Subject: [PATCH] feat: delete orphaned alert --- frontend/src/components/alert/Alert.vue | 13 +++++++++--- frontend/src/components/alert/AlertDetail.vue | 7 +++++-- .../src/components/artifact/ArtifactTag.vue | 4 +++- .../src/components/artifact/ArtifactTags.vue | 20 ++++++++++++++++++- lib/mihari/models/artifact.rb | 8 ++++++++ spec/models/artifact_spec.rb | 14 +++++++++++++ 6 files changed, 59 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/alert/Alert.vue b/frontend/src/components/alert/Alert.vue index eff098662..f047f3aa2 100644 --- a/frontend/src/components/alert/Alert.vue +++ b/frontend/src/components/alert/Alert.vue @@ -1,5 +1,5 @@ diff --git a/lib/mihari/models/artifact.rb b/lib/mihari/models/artifact.rb index fccf4ac79..e68bdf317 100644 --- a/lib/mihari/models/artifact.rb +++ b/lib/mihari/models/artifact.rb @@ -57,6 +57,14 @@ class Artifact < ActiveRecord::Base # @return [String, nil] attr_accessor :rule_id + before_destroy do + @alert = alert + end + + after_destroy do + @alert.destroy unless @alert.artifacts.any? + end + # # Check uniqueness # diff --git a/spec/models/artifact_spec.rb b/spec/models/artifact_spec.rb index 47782c140..789ddc1a7 100644 --- a/spec/models/artifact_spec.rb +++ b/spec/models/artifact_spec.rb @@ -267,4 +267,18 @@ expect(count).to eq(0) end end + + describe ".after_destroy" do + let(:artifact) { FactoryBot.create(:artifact) } + let(:alert) { artifact.alert } + + it do + expect(Mihari::Models::Alert.exists?(alert.id)).to eq(true) + end + + it do + artifact.destroy + expect(Mihari::Models::Alert.exists?(alert.id)).to eq(false) + end + end end