From 0378482d9f90600e3614cf65573b87f372bf04c2 Mon Sep 17 00:00:00 2001 From: Greg McCullough Date: Tue, 17 Jul 2018 19:01:41 -0400 Subject: [PATCH] Merge pull request #17703 from d-m-u/bz1595051 Return custom buttons for service having nil service template (cherry picked from commit 2d8f3e83bcc4e9ea519596957e99d9e69baf5041) Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1603031 --- app/models/service.rb | 7 +++++-- spec/models/service_spec.rb | 35 +++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/app/models/service.rb b/app/models/service.rb index af4502e6963..792f23795e8 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -59,6 +59,7 @@ class Service < ApplicationRecord delegate :provision_dialog, :to => :miq_request, :allow_nil => true delegate :user, :to => :miq_request, :allow_nil => true + include CustomActionsMixin include ServiceMixin include OwnershipMixin include CustomAttributeMixin @@ -100,12 +101,14 @@ def power_states vms.map(&:power_state) end + # renaming method from custom_actions_mixin + alias_method :custom_service_actions, :custom_actions def custom_actions - service_template&.custom_actions(self) + service_template ? service_template.custom_actions(self) : custom_service_actions(self) end def custom_action_buttons - service_template&.custom_action_buttons(self) + service_template ? service_template.custom_action_buttons(self) : generic_custom_buttons end def power_state diff --git a/spec/models/service_spec.rb b/spec/models/service_spec.rb index 75e48280d04..2fc2931e1d4 100644 --- a/spec/models/service_spec.rb +++ b/spec/models/service_spec.rb @@ -813,17 +813,36 @@ def create_deep_tree let(:service_template) { FactoryGirl.create(:service_template) } let(:service) { FactoryGirl.create(:service, :service_template => service_template) } - describe "#custom_actions" do - it "get list of custom actions from linked service template" do - expect(service_template).to receive(:custom_actions) - service.custom_actions + context "with template" do + describe "#custom_actions" do + it "get list of custom actions from linked service template" do + expect(service_template).to receive(:custom_actions) + service.custom_actions + end + end + + describe "#custom_action_buttons" do + it "get list of custom action buttons from linked service template" do + expect(service_template).to receive(:custom_action_buttons) + service.custom_action_buttons + end end end - describe "#custom_action_buttons" do - it "get list of custom action buttons from linked service template" do - expect(service_template).to receive(:custom_action_buttons) - service.custom_action_buttons + context "without template" do + let!(:custom_button) { FactoryGirl.create(:custom_button, :applies_to_class => "Service") } + let(:service) { FactoryGirl.create(:service, :service_template_id => -1) } + + describe "#custom_action_buttons" do + it "get list of custom action buttons on services" do + expect(service.custom_action_buttons).to include(custom_button) + end + end + + describe "#custom_actions" do + it "get list of custom actions on services" do + expect(service.custom_actions).to include(:buttons => [a_hash_including("id" => custom_button.id)], :button_groups => []) + end end end end