Skip to content

Commit

Permalink
Merge pull request #16858 from yrudman/fixed-expression-evaluation-fo…
Browse files Browse the repository at this point in the history
…r-generic-object

Pass target object when evaluating expression for Generic Object
(cherry picked from commit 20c5a84)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1553836
  • Loading branch information
chessbyte authored and simaishi committed Mar 9, 2018
1 parent 5d84e8f commit 87c54f1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/models/generic_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class GenericObject < ApplicationRecord
:type_cast,
:property_association_defined?,
:property_methods, :property_method_defined?,
:custom_actions, :custom_action_buttons,
:to => :generic_object_definition, :allow_nil => true

delegate :name, :to => :generic_object_definition, :prefix => true, :allow_nil => false
Expand All @@ -27,6 +26,14 @@ def initialize(attributes = {})
super
end

def custom_actions
generic_object_definition&.custom_actions(self)
end

def custom_action_buttons
generic_object_definition&.custom_action_buttons(self)
end

def property_attributes=(options)
raise "generic_object_definition is nil" unless generic_object_definition
options.keys.each do |k|
Expand Down
58 changes: 58 additions & 0 deletions spec/models/generic_object_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,64 @@
}
expect(definition.custom_actions).to match(expected)
end

context "expression evaluation" do
let(:generic) { FactoryGirl.build(:generic_object, :generic_object_definition => definition, :name => 'hello') }
let(:true_expression_on_definition) do
MiqExpression.new("=" => {"field" => "GenericObjectDefinition-name", "value" => "test_definition"})
end
let(:false_expression_on_definition) do
MiqExpression.new("=" => {"field" => "GenericObjectDefinition-name", "value" => "not_test_definition"})
end
let(:true_expression_on_generic) do
MiqExpression.new("=" => {"field" => "GenericObject-name", "value" => "hello"})
end
let(:false_expression_on_generic) do
MiqExpression.new("=" => {"field" => "GenericObject-name", "value" => "not_hello"})
end

before do
FactoryGirl.create(:custom_button,
:name => "visible button on Generic Object",
:applies_to_class => "GenericObject",
:visibility_expression => true_expression_on_generic)
FactoryGirl.create(:custom_button,
:name => "hidden button on Generic Object",
:applies_to_class => "GenericObject",
:visibility_expression => false_expression_on_generic)
FactoryGirl.create(:custom_button,
:name => "visible button on Generic Object Definition",
:applies_to_class => "GenericObjectDefinition",
:applies_to_id => definition.id,
:visibility_expression => true_expression_on_definition)
FactoryGirl.create(:custom_button,
:name => "hidden button on Generic Object Definition",
:applies_to_class => "GenericObjectDefinition",
:applies_to_id => definition.id,
:visibility_expression => false_expression_on_definition)
end

it "uses appropriate object: parameter, which is GenericObject or definition for expression evaluation" do
expected = {
:buttons => a_collection_containing_exactly(
a_hash_including("name" => "visible button on Generic Object"),
a_hash_including("name" => "visible button on Generic Object Definition")
),
:button_groups => []
}
expect(definition.custom_actions(generic)).to match(expected)
end

it "uses GenericObjectDefinition object to evaluate expressionif if no parameter passed" do
expected = {
:buttons => [
a_hash_including("name" => "visible button on Generic Object Definition")
],
:button_groups => []
}
expect(definition.custom_actions).to match(expected)
end
end
end

shared_examples 'AR attribute allowing letters' do |hash|
Expand Down
19 changes: 19 additions & 0 deletions spec/models/generic_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,23 @@
expect(service.generic_objects).to be_blank
end
end

context "custom buttons" do
let(:service_template) { FactoryGirl.create(:service_template) }
let(:service) { FactoryGirl.create(:service, :service_template => service_template) }

describe "#custom_actions" do
it "returns list of custom actions retrived linked GenericObjectDefinition" do
expect(definition).to receive(:custom_actions).with(go)
go.custom_actions
end
end

describe "#custom_action_buttons" do
it "returns list of custom action buttons retrived linked GenericObjectDefinition" do
expect(definition).to receive(:custom_action_buttons).with(go)
go.custom_action_buttons
end
end
end
end

0 comments on commit 87c54f1

Please sign in to comment.