Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix show record after generic object custom button dialog run #2745

Merged
merged 13 commits into from
Jan 3, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefr
miqService.sparkleOn();
var apiData;
if (apiSubmitEndpoint.match(/generic_objects/)) {
apiData = {parameters: vm.dialogData};
apiData = {action: apiAction, parameters: _.omit(vm.dialogData, 'action')};
} else {
apiData = vm.dialogData;
}
Expand All @@ -65,7 +65,7 @@ ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefr
}

function cancelClicked(_event) {
miqService.miqAjaxButton(cancelEndpoint);
miqService.redirectBack(__('Dialog Cancelled'), 'info', cancelEndpoint);
}

function saveable() {
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/application_controller/buttons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def sync_playbook_dialog(button)
end
end

def custom_buttons(ids = nil)
def custom_buttons(ids = nil, display_options = {})
button = CustomButton.find_by_id(params[:button_id])
cls = applies_to_class_model(button.applies_to_class)
@explorer = true if BASE_MODEL_EXPLORER_CLASSES.include?(cls)
Expand Down Expand Up @@ -325,10 +325,8 @@ def custom_buttons(ids = nil)
:target_kls => obj.class.name,
}

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

options[:dialog_locals] = DialogLocalService.new.determine_dialog_locals_for_custom_button(obj, button.name, button.resource_action, display_options)
options.merge!(display_options) unless display_options.empty?
dialog_initialize(button.resource_action, options)

elsif button.options && button.options.key?(:open_url) && button.options[:open_url]
Expand Down
20 changes: 13 additions & 7 deletions app/controllers/service_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ class ServiceController < ApplicationController
}

def button
if params[:pressed] == "custom_button"
ids = if @display == 'generic_objects'
@lastaction == 'generic_object' ? @sb[:rec_id] : 'LIST'
end
custom_buttons(ids)
return
case params[:pressed]
when 'generic_object_tag'
tag(GenericObject)
when "custom_button"
@display == 'generic_objects' ? generic_object_custom_buttons : custom_buttons
end
tag(GenericObject) if @display == 'generic_objects' && params[:pressed] == 'generic_object_tag'
end

def generic_object_custom_buttons
display_options = {}
ids = @lastaction == 'generic_object' ? @sb[:rec_id] : 'LIST'
display_options[:display] = @display
display_options[:display_id] = params[:id]
custom_buttons(ids, display_options)
end

def x_button
Expand Down
35 changes: 23 additions & 12 deletions app/helpers/application_helper/toolbar_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,23 +342,30 @@ def build_custom_toolbar_class(model, record, toolbar_result)
toolbar.button_group(button_group[:name], button_group[:items])
end

custom_button_add_related_buttons(model, record, toolbar)
toolbar
end

def custom_button_add_related_buttons(model, record, toolbar)
# For Service, we include buttons for ServiceTemplate.
# These buttons are added as a single group with multiple buttons
if record.present?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lgalis do we need this check, i see record being passed in from the caller

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, record may not always be present

service_buttons = record_to_service_buttons(record)
unless service_buttons.empty?
buttons = service_buttons.collect { |b| create_custom_button(b, model, record) }
toolbar.button_group("custom_buttons_", buttons)
if record.kind_of?(Service)
service_buttons = record_to_service_buttons(record)
unless service_buttons.empty?
buttons = service_buttons.collect { |b| create_custom_button(b, model, record) }
toolbar.button_group("custom_buttons_", buttons)
end
end

generic_object_buttons = record_to_generic_object_buttons(record)
unless generic_object_buttons.empty?
buttons = generic_object_buttons.collect { |b| create_custom_button(b, model, record) }
toolbar.button_group("custom_buttons_", buttons)
if record.kind_of?(GenericObject)
generic_object_buttons = record_to_generic_object_buttons(record)
unless generic_object_buttons.empty?
buttons = generic_object_buttons.collect { |b| create_custom_button(b, model, record) }
toolbar.button_group("custom_buttons_", buttons)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's time to split the build_custom_toolbar_class into more methods

this is getting too long and complex

please, split it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved adding the related buttons to a separate method: custom_buttons_add_related_buttons

end
end

toolbar
end

def button_class_name(model)
Expand All @@ -374,15 +381,19 @@ def service_template_id(record)
end
end

def create_related_custom_buttons(record, item)
item.custom_buttons.collect { |cb| create_raw_custom_button_hash(cb, record) }
end

def record_to_service_buttons(record)
return [] unless record.kind_of?(Service)
return [] if record.service_template.nil?
record.service_template.custom_buttons.collect { |cb| create_raw_custom_button_hash(cb, record) }
create_related_custom_buttons(record, record.service_template)
end

def record_to_generic_object_buttons(record)
return [] unless record.kind_of?(GenericObject)
record.generic_object_definition.custom_buttons.collect { |cb| create_raw_custom_button_hash(cb, record) }
create_related_custom_buttons(record, record.generic_object_definition)
end

def get_custom_buttons(model, record, toolbar_result)
Expand Down
12 changes: 8 additions & 4 deletions app/services/dialog_local_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ 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)
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)
submit_endpoint, cancel_endpoint = determine_api_endpoints(obj, display_options)

dialog_locals.merge!(
:resource_action_id => resource_action.id,
Expand All @@ -53,7 +53,7 @@ def determine_dialog_locals_for_custom_button(obj, button_name, resource_action)

private

def determine_api_endpoints(obj)
def determine_api_endpoints(obj, display_options = {})
case obj.class.name.demodulize
when /CloudTenant/
api_collection_name = "cloud_tenants"
Expand All @@ -69,7 +69,11 @@ def determine_api_endpoints(obj)
cancel_endpoint = "/ems_cluster"
when /GenericObject/
api_collection_name = "generic_objects"
cancel_endpoint = "/generic_object/show_list"
cancel_endpoint = if !display_options.empty? && display_options[:display_id]
"/service/show/#{display_options[:display_id]}?display=generic_objects"
else
"/service/explorer"
end
when /Host/
api_collection_name = "hosts"
cancel_endpoint = "/host"
Expand Down
2 changes: 1 addition & 1 deletion app/views/service/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
- if @lastaction == 'generic_object' && @item
= render :partial => "layouts/item", :locals => {:action_url => "show/#{@item}"}
- else
= render :partial => "layouts/gtl", :locals => {:action_url => "show/#{@record.id}", :no_flash_div => true}
= render :partial => "layouts/gtl", :locals => {:action_url => "show/#{@record.id}"}
- else
= render :partial => 'layouts/textual_groups_generic'
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('dialogUserController', function() {

spyOn(dialogFieldRefreshService, 'refreshField');
spyOn(miqService, 'miqAjaxButton');
spyOn(miqService, 'redirectBack');
spyOn(miqService, 'sparkleOn');
spyOn(miqService, 'sparkleOff');

Expand Down Expand Up @@ -88,9 +89,40 @@ describe('dialogUserController', function() {
$controller.setDialogData({data: {field1: 'field1'}});
});

context('when the submit endpoint deals with generic objects', function() {
beforeEach(inject(function(_$controller_) {
$controller = _$controller_('dialogUserController', {
API: API,
dialogFieldRefreshService: dialogFieldRefreshService,
miqService: miqService,
dialogId: '1234',
apiSubmitEndpoint: 'service/explorer',
apiAction: 'custom_action',
cancelEndpoint: 'cancel endpoint',
finishSubmitEndpoint: 'finish submit endpoint',
resourceActionId: '789',
targetId: '987',
targetType: 'targettype',
});

$controller.setDialogData({data: {field1: 'field1'}});

spyOn(API, 'post').and.returnValue(Promise.resolve('awesome'));
}));

it('posts to the API with the right data', function(done) {
$controller.submitButtonClicked();

setTimeout(function() {
expect(API.post).toHaveBeenCalledWith('service/explorer', {
field1: 'field1', action: 'custom_action'}, {skipErrors: [400]});
done();
});
});
});

context('when the API call succeeds', function() {
beforeEach(function() {
spyOn(miqService, 'redirectBack');
spyOn(API, 'post').and.returnValue(Promise.resolve('awesome'));
});

Expand Down Expand Up @@ -129,7 +161,6 @@ describe('dialogUserController', function() {
context('when the API call fails', function() {
beforeEach(function() {
var rejectionData = {data: {error: {message: "Failed! -One,Two"}}};
spyOn(miqService, 'redirectBack');
spyOn(API, 'post').and.returnValue(Promise.reject(rejectionData));
spyOn(window, 'clearFlash');
spyOn(window, 'add_flash');
Expand Down Expand Up @@ -167,7 +198,7 @@ describe('dialogUserController', function() {
describe('cancelClicked', function() {
it('uses the miqService to make a call to catalog/explorer', function() {
$controller.cancelClicked('event');
expect(miqService.miqAjaxButton).toHaveBeenCalledWith('cancel endpoint');
expect(miqService.redirectBack).toHaveBeenCalledWith('Dialog Cancelled', 'info', 'cancel endpoint');
});
});

Expand Down
4 changes: 2 additions & 2 deletions spec/services/dialog_local_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@
: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"
:finish_submit_endpoint => "/service/explorer",
:cancel_endpoint => "/service/explorer"
)
end
end
Expand Down