From 828a7de27c4c8e606fca3655f865161d70e9db7c Mon Sep 17 00:00:00 2001 From: Roman Blanco Date: Mon, 15 Oct 2018 15:39:49 +0200 Subject: [PATCH] Display custom buttons after comming from relationship table The code displays the custom buttons if the previous screen was a provider screen ('@record') and displayed item ('@ display') is from the list of classes supported by custom buttons ('APPLIES_TO_CLASS_BASE_MODELS') Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1635738 --- .../application_controller/buttons.rb | 25 +--------- app/controllers/mixins/custom_buttons.rb | 3 ++ .../mixins/custom_button_toolbar_mixin.rb | 49 +++++++++++++++++++ .../application_helper/toolbar_builder.rb | 3 ++ 4 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 app/helpers/application_helper/toolbar/mixins/custom_button_toolbar_mixin.rb diff --git a/app/controllers/application_controller/buttons.rb b/app/controllers/application_controller/buttons.rb index 5669e6ae7f8..4d352016e14 100644 --- a/app/controllers/application_controller/buttons.rb +++ b/app/controllers/application_controller/buttons.rb @@ -1,5 +1,6 @@ module ApplicationController::Buttons extend ActiveSupport::Concern + include ApplicationHelper::Toolbar::Mixins::CustomButtonToolbarMixin included do include Mixins::PlaybookOptions @@ -262,28 +263,6 @@ def open_url_after_dialog private BASE_MODEL_EXPLORER_CLASSES = [MiqGroup, MiqTemplate, Service, Switch, Tenant, User, Vm].freeze - APPLIES_TO_CLASS_BASE_MODELS = %w(AvailabilityZone CloudNetwork CloudObjectStoreContainer CloudSubnet CloudTenant - CloudVolume ContainerGroup ContainerImage ContainerNode ContainerProject - ContainerTemplate ContainerVolume EmsCluster ExtManagementSystem - GenericObject GenericObjectDefinition Host LoadBalancer - MiqGroup MiqTemp MiqTemplate NetworkRouter OrchestrationStack SecurityGroup Service - ServiceTemplate Storage Switch Tenant User Vm VmOrTemplate).freeze - def applies_to_class_model(applies_to_class) - # TODO: Give a better name for this concept, including ServiceTemplate using Service - # This should probably live in the model once this concept is defined. - unless APPLIES_TO_CLASS_BASE_MODELS.include?(applies_to_class) - raise ArgumentError, "Received: #{applies_to_class}, expected one of #{APPLIES_TO_CLASS_BASE_MODELS}" - end - - case applies_to_class - when "ServiceTemplate" - Service - when "GenericObjectDefinition" - GenericObject - else - applies_to_class.constantize - end - end def custom_button_done url = SystemConsole.find_by(:vm => params[:id]).try(:url) @@ -321,7 +300,7 @@ def sync_playbook_dialog(button) def custom_buttons(ids = nil, display_options = {}) button = CustomButton.find(params[:button_id]) - cls = applies_to_class_model(button.applies_to_class) + cls = custom_button_class_model(button.applies_to_class) @explorer = true if BASE_MODEL_EXPLORER_CLASSES.include?(cls) ids ||= params[:id] if ids.to_s == 'LIST' diff --git a/app/controllers/mixins/custom_buttons.rb b/app/controllers/mixins/custom_buttons.rb index ca218529869..9edeac197a3 100644 --- a/app/controllers/mixins/custom_buttons.rb +++ b/app/controllers/mixins/custom_buttons.rb @@ -1,5 +1,6 @@ module Mixins::CustomButtons extend ActiveSupport::Concern + include ApplicationHelper::Toolbar::Mixins::CustomButtonToolbarMixin def custom_toolbar return nil unless self.class.instance_eval { @custom_buttons } @@ -24,6 +25,8 @@ def custom_toolbar_simple Mixins::CustomButtons::Result.new(:single) elsif @lastaction == "show_list" Mixins::CustomButtons::Result.new(:list) + elsif relationship_table_screen? + Mixins::CustomButtons::Result.new(:list) elsif @display == 'generic_objects' @lastaction == 'generic_object' ? Mixins::CustomButtons::Result.new(:single) : Mixins::CustomButtons::Result.new(:list) end diff --git a/app/helpers/application_helper/toolbar/mixins/custom_button_toolbar_mixin.rb b/app/helpers/application_helper/toolbar/mixins/custom_button_toolbar_mixin.rb new file mode 100644 index 00000000000..5f5574e2f60 --- /dev/null +++ b/app/helpers/application_helper/toolbar/mixins/custom_button_toolbar_mixin.rb @@ -0,0 +1,49 @@ +module ApplicationHelper::Toolbar::Mixins::CustomButtonToolbarMixin + APPLIES_TO_CLASS_BASE_MODELS = %w(AvailabilityZone CloudNetwork CloudObjectStoreContainer CloudSubnet CloudTenant + CloudVolume ContainerGroup ContainerImage ContainerNode ContainerProject + ContainerTemplate ContainerVolume EmsCluster ExtManagementSystem + GenericObject GenericObjectDefinition Host LoadBalancer + MiqGroup MiqTemp MiqTemplate NetworkRouter OrchestrationStack SecurityGroup Service + ServiceTemplate Storage Switch Tenant User Vm VmOrTemplate).freeze + + def custom_button_appliable_class?(model) + APPLIES_TO_CLASS_BASE_MODELS.include?(model) + end + + def custom_button_class_model(applies_to_class) + # TODO: Give a better name for this concept, including ServiceTemplate using Service + # This should probably live in the model once this concept is defined. + unless custom_button_appliable_class?(applies_to_class) + raise ArgumentError, "Received: #{applies_to_class}, expected one of #{APPLIES_TO_CLASS_BASE_MODELS}" + end + + case applies_to_class + when "ServiceTemplate" + Service + when "GenericObjectDefinition" + GenericObject + else + applies_to_class.constantize + end + end + + # Indicates, whether the user has came from providers relationship screen + # or not + # + # Used to indicate if the custom buttons should be rendered + def relationship_table_screen? + return false if @display.nil? || @record.nil? + + display_model = @display.camelize.singularize + providers = (EmsCloud.descendants + + EmsInfra.descendants + + EmsPhysicalInfra.descendants + + ManageIQ::Providers::ContainerManager.descendants) + + custom_button_model = custom_button_appliable_class?(display_model) + provider_screen = providers.any? { |provider| @record.instance_of?(provider) } + show_action = @lastaction == "show" + + custom_button_model && provider_screen && show_action + end +end diff --git a/app/helpers/application_helper/toolbar_builder.rb b/app/helpers/application_helper/toolbar_builder.rb index 371e57bac2e..058b02cc01a 100644 --- a/app/helpers/application_helper/toolbar_builder.rb +++ b/app/helpers/application_helper/toolbar_builder.rb @@ -1,6 +1,7 @@ class ApplicationHelper::ToolbarBuilder include MiqAeClassHelper include RestfulControllerMixin + include ApplicationHelper::Toolbar::Mixins::CustomButtonToolbarMixin def call(toolbar_name) build_toolbar(toolbar_name) @@ -283,6 +284,8 @@ def custom_toolbar_class(toolbar_result) if @display == 'generic_objects' model = GenericObjectDefinition record = GenericObject.find_by(:id => @sb[:rec_id]) + elsif relationship_table_screen? + model = @display.camelize.singularize.constantize else model = @record ? @record.class : model_for_custom_toolbar end