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

Evaluate enablement expressions for custom buttons #15729

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/models/custom_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ def uri_object_name
def get_resource_action
resource_action || build_resource_action
end

def evaluate_enablement_expression_for(object)
return true unless enablement_expression
return false if enablement_expression && !object # list
enablement_expression.lenient_evaluate(object)
end

# End - Helper methods to support moving automate columns to resource_actions table

def self.parse_uri(uri)
Expand Down
28 changes: 28 additions & 0 deletions spec/models/custom_button_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@
@user = FactoryGirl.create(:user, :name => 'Fred Flintstone', :userid => 'fred')
end

describe '#evaluate_enablement_expression_for' do
let(:miq_expression) { MiqExpression.new('EQUAL' => {'field' => 'Vm-name', 'value' => 'vm_1'}) }
let(:vm) { FactoryGirl.create(:vm_vmware, :name => 'vm_1') }
let(:custom_button) do
FactoryGirl.create(:custom_button, :applies_to => vm.class, :name => "foo", :description => "foo foo")
end

it 'evaluates enablement expression when expression is not set' do
expect(custom_button.evaluate_enablement_expression_for(vm)).to be_truthy
end

it 'evaluates enablement expression when expression is set and method is called for list' do
custom_button.enablement_expression = miq_expression
expect(custom_button.evaluate_enablement_expression_for(nil)).to be_falsey
end

it 'evaluates enablement expression when expression is set and evaluated to true' do
custom_button.enablement_expression = miq_expression
expect(custom_button.evaluate_enablement_expression_for(vm)).to be_truthy
end

it 'evaluates enablement expression when expression is set and evaluated to false' do
custom_button.enablement_expression = miq_expression
vm.name = 'XXX'
expect(custom_button.evaluate_enablement_expression_for(vm)).to be_falsey
end
end

it "should validate there are no buttons" do
expect(described_class.count).to eq(0)
end
Expand Down