From c139d48960a20a949f001699d21ac57cf4943cc0 Mon Sep 17 00:00:00 2001 From: Roman Blanco Date: Wed, 17 Oct 2018 16:45:02 +0200 Subject: [PATCH] [wip] Do not use '@ record' to indicate custom button screen '@ record' is set while rendering the page, but after button click, the '@ record' is nil. The id in params[:id] is providers id --- .../application_controller/buttons.rb | 15 +++++++++++-- .../mixins/custom_button_toolbar_mixin.rb | 21 +++++++++---------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/app/controllers/application_controller/buttons.rb b/app/controllers/application_controller/buttons.rb index 9d7400b2bd14..61ddb3b432e6 100644 --- a/app/controllers/application_controller/buttons.rb +++ b/app/controllers/application_controller/buttons.rb @@ -296,8 +296,19 @@ def custom_buttons(ids = nil, display_options = {}) button = CustomButton.find(params[:button_id]) 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' + ids ||= params[:id] unless relationship_table_screen? && @record.nil? + ids ||= find_checked_items # FIXME: for some reason, this doesn't work + + + binding.pry + + # FIXME: copypasted from below + if ids.empty? + render_flash(_("Error executing custom button: No item was selected."), :error) + return + end + + if ids.to_s == 'LIST' || !ids.empty? objs = Rbac.filtered(cls.where(:id => find_checked_items)) obj = objs.first else 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 index aae9a2976ad4..85b5ad19e700 100644 --- a/app/helpers/application_helper/toolbar/mixins/custom_button_toolbar_mixin.rb +++ b/app/helpers/application_helper/toolbar/mixins/custom_button_toolbar_mixin.rb @@ -28,22 +28,21 @@ def custom_button_class_model(applies_to_class) end # Indicates, whether the user has came from providers relationship screen - # or not + # or not, before the page is rendered # # Used to indicate if the custom buttons should be rendered def relationship_table_screen? - return false if @display.nil? || @record.nil? + return false if @display.nil? + display_class = @display.camelize.singularize + return false unless custom_button_appliable_class?(display_class) - 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" + display_model = display_class.constantize + # method is accessed twice from a different location - from toolbar builder + # and custom button mixin - and so controller class changes + ctrl = self.class == ApplicationHelper::ToolbarBuilder ? controller : self + controller_model = ctrl.class.model - custom_button_model && provider_screen && show_action + display_model != controller_model && show_action end end