Skip to content

Commit

Permalink
Use class base_name instead of demodulize
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Jan 28, 2019
1 parent 6bcbfeb commit b74d064
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 62 deletions.
65 changes: 13 additions & 52 deletions app/services/dialog_local_service.rb
@@ -1,40 +1,4 @@
class DialogLocalService
NEW_DIALOG_USERS = %w(
AvailabilityZone
CloudNetwork
CloudObjectStoreContainer
CloudSubnet
CloudTenant
CloudVolume
ContainerGroup
ContainerImage
ContainerNode
ContainerProject
ContainerTemplate
ContainerVolume
EmsCluster
GenericObject
Host
InfraManager
LoadBalancer
MiqGroup
NetworkRouter
OrchestrationStack
SecurityGroup
Service
ServiceAnsiblePlaybook
ServiceAnsibleTower
ServiceContainerTemplate
ServiceGeneric
ServiceOrchestration
Storage
Switch
Template
Tenant
User
Vm
).freeze

def determine_dialog_locals_for_svc_catalog_provision(resource_action, target, finish_submit_endpoint)
api_submit_endpoint = "/api/service_catalogs/#{target.service_template_catalog_id}/service_templates/#{target.id}"

Expand All @@ -53,13 +17,9 @@ def determine_dialog_locals_for_svc_catalog_provision(resource_action, target, f
end

def determine_dialog_locals_for_custom_button(obj, button_name, resource_action, display_options = {})
dialog_locals = {:force_old_dialog_use => true}

return dialog_locals unless NEW_DIALOG_USERS.include?(obj.class.name.demodulize)

submit_endpoint, cancel_endpoint = determine_api_endpoints(obj, display_options)

dialog_locals.merge!(
{
:resource_action_id => resource_action.id,
:target_id => obj.id,
:target_type => determine_target_type(obj),
Expand All @@ -70,27 +30,34 @@ def determine_dialog_locals_for_custom_button(obj, button_name, resource_action,
:finish_submit_endpoint => cancel_endpoint,
:cancel_endpoint => cancel_endpoint,
:open_url => false
)

dialog_locals
}
end

private

def determine_api_endpoints(obj, display_options = {})
base_name = obj.class.name.demodulize
base_name = obj.class.base_model.name
case base_name
when /EmsCluster/
api_collection_name = "clusters"
cancel_endpoint = "/ems_cluster"
when /MiqTemplate/
if obj.class.name.demodulize == "Vm"
api_collection_name = "vms"
cancel_endpoint = display_options[:cancel_endpoint] || "/vm_infra/explorer"
else
api_collection_name = "templates"
cancel_endpoint = display_options[:cancel_endpoint] || "/vm_or_template/explorer"
end

when /GenericObject/
api_collection_name = "generic_objects"
cancel_endpoint = if !display_options.empty? && display_options[:display_id]
"/service/show/#{display_options[:display_id]}?display=generic_objects"
else
"/service/explorer"
end
when /InfraManager/
when /ExtManagementSystem/
api_collection_name = "providers"
cancel_endpoint = "/ems_infra"
when /MiqGroup/
Expand All @@ -105,12 +72,6 @@ def determine_api_endpoints(obj, display_options = {})
when /Switch/
api_collection_name = "switches"
cancel_endpoint = "/infra_networking/explorer"

# ^ is necessary otherwise we match on ContainerTemplates
when /^Template/
api_collection_name = "templates"
cancel_endpoint = display_options[:cancel_endpoint] || "/vm_or_template/explorer"

# ^ is necessary otherwise we match CloudTenant
when /^Tenant/
api_collection_name = "tenants"
Expand Down
24 changes: 14 additions & 10 deletions spec/services/dialog_local_service_spec.rb
Expand Up @@ -77,6 +77,13 @@
"cloud_network", "cloud_networks", "/cloud_network"
end

context "when the object is a private CloudNetwork" do
let(:obj) { double(:class => ManageIQ::Providers::Openstack::NetworkManager::CloudNetwork::Private, :id => 123) }

include_examples "DialogLocalService#determine_dialog_locals_for_custom_button return value",
"cloud_network", "cloud_networks", "/cloud_network"
end

context "when the object is a CloudObjectStoreContainer" do
let(:obj) { double(:class => ManageIQ::Providers::Amazon::StorageManager::S3::CloudObjectStoreContainer, :id => 123) }

Expand Down Expand Up @@ -168,6 +175,13 @@
"host", "hosts", "/host"
end

context "when the object is a HostEsx" do
let(:obj) { double(:class => ManageIQ::Providers::Vmware::InfraManager::HostEsx, :id => 123) }

include_examples "DialogLocalService#determine_dialog_locals_for_custom_button return value",
"host_esx", "hosts", "/host"
end

context "when the object is an InfraManager" do
let(:obj) { double(:class => ManageIQ::Providers::Vmware::InfraManager, :id => 123) }

Expand Down Expand Up @@ -292,15 +306,5 @@
"vm", "vms", "/vm_infra/explorer"
end
end

context "when the object does not support new dialogs" do
let(:obj) { double(:id => 123) }

it "returns a hash with 'force_old_dialog_use' set to true" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name, resource_action)).to eq(
:force_old_dialog_use => true
)
end
end
end
end

0 comments on commit b74d064

Please sign in to comment.