Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide Driving Event field for Containers. #4324

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/controllers/miq_policy_controller/alerts.rb
@@ -1,6 +1,10 @@
module MiqPolicyController::Alerts
extend ActiveSupport::Concern

included do
helper_method :display_driving_event?
end

def alert_edit_cancel
@edit = nil
@alert = session[:edit][:alert_id] ? MiqAlert.find(session[:edit][:alert_id]) : MiqAlert.new
Expand Down Expand Up @@ -105,6 +109,7 @@ def alert_field_changed
@edit[:expression_options] = MiqAlert.expression_options(@edit[:new][:expression][:eval_method])
alert_build_exp_options_info
end
@edit[:new][:exp_event] = %w(ContainerNode ContainerProject).include?(@edit[:new][:db]) ? nil : @edit[:current][:exp_event]
end

if params.key?(:exp_name)
Expand Down Expand Up @@ -233,6 +238,11 @@ def alert_get_all

private

def display_driving_event?
(@edit[:new][:expression][:eval_method] && @edit[:new][:expression][:eval_method] != "nothing") ||
%w(ContainerNode ContainerProject).include?(@edit[:new][:db])
end

def process_alerts(alerts, task)
process_elements(alerts, MiqAlert, task)
end
Expand Down Expand Up @@ -568,7 +578,7 @@ def alert_valid_record?(alert)
if alert.expression.nil?
add_flash(_("A valid expression must be present"), :error)
end
unless @edit[:new][:expression][:eval_method] && @edit[:new][:expression][:eval_method] != "nothing"
unless display_driving_event?
add_flash(_("A Driving Event must be selected"), :error) if alert.responds_to_events.blank?
end
if alert.options[:notifications][:automate]
Expand Down
4 changes: 2 additions & 2 deletions app/views/miq_policy/_alert_details.html.haml
Expand Up @@ -80,7 +80,7 @@

-# Expression driving event
- if @edit
- unless @edit[:new][:expression][:eval_method] && @edit[:new][:expression][:eval_method] != "nothing"
- unless display_driving_event?
.form-group
%label.control-label.col-md-2
= _("Driving Event")
Expand All @@ -93,7 +93,7 @@
miqInitSelectPicker();
miqSelectPickerEvent('exp_event', '#{url}', {beforeSend: true, complete: true})
- else
- if @alert.expression.kind_of?(MiqExpression) || @alert.expression[:eval_method] == "nothing"
- if (@alert.expression.kind_of?(MiqExpression) || @alert.expression[:eval_method] == "nothing") && !%w(ContainerNode ContainerProject).include?(@alert.db)
.form-group
%label.control-label.col-md-2
= _("Driving Event")
Expand Down
42 changes: 42 additions & 0 deletions spec/controllers/miq_policy_controller/alerts_spec.rb
Expand Up @@ -134,5 +134,47 @@
expect(response).to render_template(:partial => 'miq_policy/_alert_details')
end
end

context "alert_valid_record?" do
before do
login_as FactoryGirl.create(:user, :features => "alert_admin")
end

before :each do
expression = MiqExpression.new("=" => {:tag => "name", :value => "Test"}, :token => 1)
@miq_alert = FactoryGirl.create(
:miq_alert,
:db => "Host",
:options => {:notifications => {:email => {:to => "fred@test.com"}}},
:expression => expression
)
edit = {
:new => {
:name => "New Name",
:expression => {:eval_method => nil},
:db => "Host"
}
}
controller.instance_variable_set(:@edit, edit)
end

it "forces Driving Event to be present for non-container alerts" do
controller.send(:alert_valid_record?, @miq_alert)
expect(assigns(:flash_array).first[:message]).to match("A Driving Event must be selected")
end

it "does not force Driving Event to be present for container alerts" do
edit = {
:new => {
:name => "New Name",
:expression => {:eval_method => nil},
:db => "ContainerNode",
}
}
controller.instance_variable_set(:@edit, edit)
controller.send(:alert_valid_record?, @miq_alert)
expect(assigns(:flash_array)).to be_nil
end
end
end
end