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

Pass target object when evaluating expression for Generic Object #16858

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
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