From 47f08d5fc52a302685a63e89bfad1fff71cd1277 Mon Sep 17 00:00:00 2001 From: Mooli Tayer Date: Wed, 25 Jan 2017 00:29:17 +0200 Subject: [PATCH] Add hide & show alert status actions --- app/models/miq_alert_status.rb | 5 +++++ app/models/miq_alert_status_action.rb | 7 ++++--- spec/models/miq_alert_status_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/models/miq_alert_status.rb b/app/models/miq_alert_status.rb index c36125ef38b..265befcac95 100644 --- a/app/models/miq_alert_status.rb +++ b/app/models/miq_alert_status.rb @@ -9,6 +9,7 @@ class MiqAlertStatus < ApplicationRecord belongs_to :ext_management_system has_many :miq_alert_status_actions, -> { order "created_at" }, :dependent => :destroy virtual_column :assignee, :type => :string + virtual_column :hidden, :type => :boolean validates :severity, :acceptance => { :accept => SEVERITY_LEVELS } @@ -19,4 +20,8 @@ def assignee def assigned? assignee.present? end + + def hidden? + miq_alert_status_actions.where(:action_type => %w(hide show)).last.try(:action_type) == 'hide' + end end diff --git a/app/models/miq_alert_status_action.rb b/app/models/miq_alert_status_action.rb index c717456d914..9f20ba6bc84 100644 --- a/app/models/miq_alert_status_action.rb +++ b/app/models/miq_alert_status_action.rb @@ -1,5 +1,5 @@ class MiqAlertStatusAction < ApplicationRecord - ACTION_TYPES = %w(assign acknowledge comment unassign unacknowledge).freeze + ACTION_TYPES = %w(assign acknowledge comment unassign unacknowledge hide show).freeze belongs_to :miq_alert_status belongs_to :assignee, :class_name => 'User' @@ -15,8 +15,9 @@ class MiqAlertStatusAction < ApplicationRecord after_save :update_status_acknowledgement def only_assignee_can_acknowledge - if action_type == 'acknowledge' && miq_alert_status.assignee.try(:id) != user.id - errors.add(:user, "that is not assigned cannot acknowledge") + if ['acknowledge', 'unacknowledge', 'hide', 'show'].include?(action_type) && + miq_alert_status.assignee.try(:id) != user.id + errors.add(:user, "that is not assigned cannot #{action_type}") end end diff --git a/spec/models/miq_alert_status_spec.rb b/spec/models/miq_alert_status_spec.rb index 8488d0dba4e..83949883c9b 100644 --- a/spec/models/miq_alert_status_spec.rb +++ b/spec/models/miq_alert_status_spec.rb @@ -12,6 +12,12 @@ FactoryGirl.create(:miq_alert_status_action, :action_type => 'assign', :user => user1, :assignee => user1, :miq_alert_status => alert) end + let(:hide_action) do + FactoryGirl.create(:miq_alert_status_action, :action_type => 'hide', :user => user1, :miq_alert_status => alert) + end + let(:show_action) do + FactoryGirl.create(:miq_alert_status_action, :action_type => 'show', :user => user1, :miq_alert_status => alert) + end describe "Validation" do it "should reject unexpected severities" do @@ -106,4 +112,20 @@ expect(alert.assignee).to be_nil end end + + describe "#hidden?" do + it "returns false for new" do + expect(alert.hidden?).to be_falsey + end + + it "returns true after hide action" do + alert.miq_alert_status_actions = [assignment_action, hide_action] + expect(alert.hidden?).to be_truthy + end + + it "returns false after show action" do + alert.miq_alert_status_actions = [assignment_action, hide_action, show_action] + expect(alert.hidden?).to be_falsey + end + end end