diff --git a/app/models/generic_object.rb b/app/models/generic_object.rb index c3058c9b9ae..55dce880181 100644 --- a/app/models/generic_object.rb +++ b/app/models/generic_object.rb @@ -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 @@ -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| diff --git a/spec/models/generic_object_definition_spec.rb b/spec/models/generic_object_definition_spec.rb index d5ab3090af7..77da9626991 100644 --- a/spec/models/generic_object_definition_spec.rb +++ b/spec/models/generic_object_definition_spec.rb @@ -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| diff --git a/spec/models/generic_object_spec.rb b/spec/models/generic_object_spec.rb index c3d1e975e99..c3a6a3ebece 100644 --- a/spec/models/generic_object_spec.rb +++ b/spec/models/generic_object_spec.rb @@ -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