From 18db19b1d8145fc13a07ddc827c83da816d7f255 Mon Sep 17 00:00:00 2001 From: lpichler Date: Tue, 21 Aug 2018 10:21:48 +0200 Subject: [PATCH 1/3] Remove double negation --- app/models/mixins/assignment_mixin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/mixins/assignment_mixin.rb b/app/models/mixins/assignment_mixin.rb index 0fd775488d8..51d8ec4ac1a 100644 --- a/app/models/mixins/assignment_mixin.rb +++ b/app/models/mixins/assignment_mixin.rb @@ -100,7 +100,7 @@ def get_assigned_tos result[:objects] << object unless object.nil? when :tag tag = Tag.find_by(:name => "/" + parts.join("/")) - result[:tags] << [Classification.find_by(:tag_id => tag.id), klass] unless tag.nil? + result[:tags] << [Classification.find_by(:tag_id => tag.id), klass] if tag when :label label = if AssignmentMixin.escaped?(parts[1]) name = AssignmentMixin.unescape(parts[1]) From 27a9e120af647c8635c41dbb6579d590676ac824 Mon Sep 17 00:00:00 2001 From: lpichler Date: Tue, 21 Aug 2018 10:24:10 +0200 Subject: [PATCH 2/3] Require also classification to get assigments in AssignmentMixin --- app/models/mixins/assignment_mixin.rb | 3 ++- spec/models/mixins/assignment_mixin_spec.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/mixins/assignment_mixin.rb b/app/models/mixins/assignment_mixin.rb index 51d8ec4ac1a..fdbeb531a29 100644 --- a/app/models/mixins/assignment_mixin.rb +++ b/app/models/mixins/assignment_mixin.rb @@ -100,7 +100,8 @@ def get_assigned_tos result[:objects] << object unless object.nil? when :tag tag = Tag.find_by(:name => "/" + parts.join("/")) - result[:tags] << [Classification.find_by(:tag_id => tag.id), klass] if tag + classification = Classification.find_by(:tag_id => tag.id) if tag + result[:tags] << [classification, klass] if classification when :label label = if AssignmentMixin.escaped?(parts[1]) name = AssignmentMixin.unescape(parts[1]) diff --git a/spec/models/mixins/assignment_mixin_spec.rb b/spec/models/mixins/assignment_mixin_spec.rb index 80ffbe2d08a..05c696eafdb 100644 --- a/spec/models/mixins/assignment_mixin_spec.rb +++ b/spec/models/mixins/assignment_mixin_spec.rb @@ -92,6 +92,20 @@ expect(assigned_tos[:objects]).to include(vm) expect(assigned_tos[:tags]).to include([classification_tag, "vms"]) end + + it "doesn't return tags when classification is missing" do + cc_classification = FactoryGirl.create(:classification_cost_center) + classification_tag = FactoryGirl.create(:classification_tag, :parent => cc_classification) + vm = FactoryGirl.create(:vm) + alert_set = FactoryGirl.create(:miq_alert_set) + classification_tag.destroy + alert_set.assign_to_objects([vm]) + alert_set.assign_to_tags([classification_tag], "vms") + + assigned_tos = alert_set.get_assigned_tos + expect(assigned_tos[:objects]).to include(vm) + expect(alert_set.get_assigned_tos[:tags]).to be_empty + end end describe ".all_assignments" do From b461b65662a28a427a27b267abe0f82aaffe45ef Mon Sep 17 00:00:00 2001 From: lpichler Date: Tue, 21 Aug 2018 12:38:02 +0200 Subject: [PATCH 3/3] DRY --- spec/models/mixins/assignment_mixin_spec.rb | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/spec/models/mixins/assignment_mixin_spec.rb b/spec/models/mixins/assignment_mixin_spec.rb index 05c696eafdb..496bdd08964 100644 --- a/spec/models/mixins/assignment_mixin_spec.rb +++ b/spec/models/mixins/assignment_mixin_spec.rb @@ -79,28 +79,24 @@ end describe "#get_assigned_tos" do - it "returns objects and tags" do - cc_classification = FactoryGirl.create(:classification_cost_center) - classification_tag = FactoryGirl.create(:classification_tag, :parent => cc_classification) - vm = FactoryGirl.create(:vm) - alert_set = FactoryGirl.create(:miq_alert_set) + let(:cc_classification) { FactoryGirl.create(:classification_cost_center) } + let(:classification_tag) { FactoryGirl.create(:classification_tag, :parent => cc_classification) } + let(:vm) { FactoryGirl.create(:vm) } + let(:alert_set) { FactoryGirl.create(:miq_alert_set) } + before do alert_set.assign_to_objects([vm]) alert_set.assign_to_tags([classification_tag], "vms") + end + it "returns objects and tags" do assigned_tos = alert_set.get_assigned_tos expect(assigned_tos[:objects]).to include(vm) expect(assigned_tos[:tags]).to include([classification_tag, "vms"]) end it "doesn't return tags when classification is missing" do - cc_classification = FactoryGirl.create(:classification_cost_center) - classification_tag = FactoryGirl.create(:classification_tag, :parent => cc_classification) - vm = FactoryGirl.create(:vm) - alert_set = FactoryGirl.create(:miq_alert_set) classification_tag.destroy - alert_set.assign_to_objects([vm]) - alert_set.assign_to_tags([classification_tag], "vms") assigned_tos = alert_set.get_assigned_tos expect(assigned_tos[:objects]).to include(vm)