Skip to content

Commit

Permalink
Display custom buttons after comming from relationship table
Browse files Browse the repository at this point in the history
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
  • Loading branch information
romanblanco committed Oct 23, 2018
1 parent 6f1bdbc commit 828a7de
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
25 changes: 2 additions & 23 deletions app/controllers/application_controller/buttons.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module ApplicationController::Buttons
extend ActiveSupport::Concern
include ApplicationHelper::Toolbar::Mixins::CustomButtonToolbarMixin

included do
include Mixins::PlaybookOptions
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/mixins/custom_buttons.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/helpers/application_helper/toolbar_builder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ApplicationHelper::ToolbarBuilder
include MiqAeClassHelper
include RestfulControllerMixin
include ApplicationHelper::Toolbar::Mixins::CustomButtonToolbarMixin

def call(toolbar_name)
build_toolbar(toolbar_name)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 828a7de

Please sign in to comment.