Skip to content

Commit

Permalink
Extract dialog local logic and add specs around it
Browse files Browse the repository at this point in the history
  • Loading branch information
eclarizio committed Dec 1, 2017
1 parent d7c7f9c commit bcfee50
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 41 deletions.
34 changes: 3 additions & 31 deletions app/controllers/application_controller/buttons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ def custom_buttons(ids = nil)
:target_kls => obj.class.name,
}

options[:dialog_locals] = determine_dialog_locals_for_custom_button(obj, button.name, button.resource_action.id)
options[:dialog_locals] = DialogLocalService.new.determine_dialog_locals_for_custom_button(
obj, button.name, button.resource_action.id
)

dialog_initialize(button.resource_action, options)

Expand All @@ -356,36 +358,6 @@ def custom_buttons(ids = nil)
end
end

def determine_dialog_locals_for_custom_button(obj, button_name, resource_action_id)
case obj.class.name.demodulize
when /Vm/
api_collection_name = "vms"
cancel_endpoint = "/vm_infra/explorer"
force_old_dialog_use = false
when /Service/
api_collection_name = "services"
cancel_endpoint = "/service/explorer"
force_old_dialog_use = false
when /GenericObject/
api_collection_name = "generic_objects"
cancel_endpoint = "/generic_object/show_list"
force_old_dialog_use = false
else
force_old_dialog_use = true
end

{
:resource_action_id => resource_action_id,
:target_id => obj.id,
:target_type => obj.class.name.underscore,
:force_old_dialog_use => force_old_dialog_use,
:api_submit_endpoint => "/api/#{api_collection_name}/#{obj.id}",
:api_action => button_name,
:finish_submit_endpoint => cancel_endpoint,
:cancel_endpoint => cancel_endpoint
}
end

def get_available_dialogs
@edit[:new][:available_dialogs] = {}
Dialog.all.each do |d|
Expand Down
13 changes: 3 additions & 10 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -543,16 +543,9 @@ def svc_catalog_provision
options[:header] = @right_cell_text
options[:target_id] = st.id
options[:target_kls] = st.class.name
options[:dialog_locals] = {
:resource_action_id => ra.id,
:target_id => st.id,
:target_type => st.class.name.underscore,
:dialog_id => ra.dialog_id,
:api_submit_endpoint => "/api/service_catalogs/#{st.service_template_catalog_id}/service_templates/#{st.id}",
:api_action => "order",
:finish_submit_endpoint => svc_catalog_provision_finish_submit_endpoint,
:cancel_endpoint => "/catalog/explorer"
}
options[:dialog_locals] = DialogLocalService.new.determine_dialog_locals_for_svc_catalog_provision(
ra, st, svc_catalog_provision_finish_submit_endpoint
)

replace_right_cell(:action => "dialog_provision", :dialog_locals => options[:dialog_locals])
else
Expand Down
47 changes: 47 additions & 0 deletions app/services/dialog_local_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class DialogLocalService
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}"

{
:resource_action_id => resource_action.id,
:target_id => target.id,
:target_type => target.class.name.underscore,
:dialog_id => resource_action.dialog_id,
:force_old_dialog_use => false,
:api_submit_endpoint => api_submit_endpoint,
:api_action => "order",
:finish_submit_endpoint => finish_submit_endpoint,
:cancel_endpoint => "/catalog/explorer"
}
end

def determine_dialog_locals_for_custom_button(obj, button_name, resource_action_id)
case obj.class.name.demodulize
when /Vm/
api_collection_name = "vms"
cancel_endpoint = "/vm_infra/explorer"
force_old_dialog_use = false
when /Service/
api_collection_name = "services"
cancel_endpoint = "/service/explorer"
force_old_dialog_use = false
when /GenericObject/
api_collection_name = "generic_objects"
cancel_endpoint = "/generic_object/show_list"
force_old_dialog_use = false
else
force_old_dialog_use = true
end

{
:resource_action_id => resource_action_id,
:target_id => obj.id,
:target_type => obj.class.name.demodulize.underscore,
:force_old_dialog_use => force_old_dialog_use,
:api_submit_endpoint => "/api/#{api_collection_name}/#{obj.id}",
:api_action => button_name,
:finish_submit_endpoint => cancel_endpoint,
:cancel_endpoint => cancel_endpoint
}
end
end
81 changes: 81 additions & 0 deletions spec/services/dialog_local_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
describe DialogLocalService do
let(:service) { described_class.new }

describe "#determine_dialog_locals_for_svc_catalog_provision" do
let(:resource_action) { instance_double("ResourceAction", :id => 456, :dialog_id => 654) }
let(:target) { instance_double("ServiceTemplate", :class => ServiceTemplate, :id => 321, :service_template_catalog_id => 123) }
let(:finish_submit_endpoint) { "finishsubmitendpoint" }

it "returns a hash" do
expect(service.determine_dialog_locals_for_svc_catalog_provision(
resource_action, target, finish_submit_endpoint
)).to eq(
:resource_action_id => 456,
:target_id => 321,
:target_type => 'service_template',
:dialog_id => 654,
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/service_catalogs/123/service_templates/321",
:api_action => "order",
:finish_submit_endpoint => "finishsubmitendpoint",
:cancel_endpoint => "/catalog/explorer"
)
end
end

describe "#determine_dialog_locals_for_custom_button" do
let(:button_name) { "custom-button-name" }
let(:resource_action_id) { 321 }

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name, resource_action_id)).to eq(
:resource_action_id => 321,
:target_id => 123,
:target_type => 'vm',
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/vms/123",
:api_action => "custom-button-name",
:finish_submit_endpoint => "/vm_infra/explorer",
:cancel_endpoint => "/vm_infra/explorer"
)
end
end

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name, resource_action_id)).to eq(
:resource_action_id => 321,
:target_id => 123,
:target_type => 'service',
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/services/123",
:api_action => "custom-button-name",
:finish_submit_endpoint => "/service/explorer",
:cancel_endpoint => "/service/explorer"
)
end
end

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name, resource_action_id)).to eq(
:resource_action_id => 321,
:target_id => 123,
:target_type => 'generic_object',
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/generic_objects/123",
:api_action => "custom-button-name",
:finish_submit_endpoint => "/generic_object/show_list",
:cancel_endpoint => "/generic_object/show_list"
)
end
end
end
end

0 comments on commit bcfee50

Please sign in to comment.