Skip to content

Commit

Permalink
Merge pull request #6230 from ZitaNemeckova/fix_GOD_again
Browse files Browse the repository at this point in the history
Change toolbar delete buttons for Button and Button Group to use generic code
  • Loading branch information
himdel authored Oct 23, 2019
2 parents b59885c + 3073ae1 commit 6c5707c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ function genericObjectDefinitionToolbarController(API, miqService, $window) {
});
} else if (toolbar.action === 'delete' && currentRecordId) {
deleteWithAPI('/api/generic_object_definitions/', currentRecordId);
} else if (toolbar.action === 'delete_custom_button_set' && currentRecordId) {
deleteWithAPI('/api/custom_button_sets/', currentRecordId);
} else if (toolbar.action === 'delete_custom_button' && currentRecordId) {
deleteWithAPI('/api/custom_buttons/', currentRecordId);
}
}

Expand All @@ -70,8 +66,8 @@ function genericObjectDefinitionToolbarController(API, miqService, $window) {
.catch(miqService.handleFailure);
}

function postAction() {
var entityName = toolbar.entityName;
function postAction(response) {
var entityName = response.name;
var saveMsg = sprintf(__('%s: "%s" was successfully deleted'), toolbar.entity, entityName);
if (toolbar.redirectUrl) {
miqService.redirectBack(saveMsg, 'success', toolbar.redirectUrl);
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/application_helper/button/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def initialize(view_context, view_binding, instance_data, props)
end
end

def data(data)
data
end

def role_allows_feature?
# for select buttons RBAC is checked only for nested buttons
return true if self[:type] == :buttonSelect
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ApplicationHelper::Button::GenericObjectDefinitionButtonButtonDelete < ApplicationHelper::Button::Basic
needs :@record

def data(_data)
{
'function' => 'sendDataWithRx',
'function-data' => {
:type => 'delete',
:controller => 'toolbarActions',
:payload => {
:entity => "custom_buttons",
:redirect_url => "/generic_object_definition/",
:name => @record.name,
:labels => { :single => _("Button") }
}
}
}
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
class ApplicationHelper::Button::GenericObjectDefinitionButtonButtonGroupDelete < ApplicationHelper::Button::Basic
needs :@record

def disabled?
!@record.custom_buttons.count.zero?
end

def data(_data)
{
'function' => 'sendDataWithRx',
'function-data' => {
:type => 'delete',
:controller => 'toolbarActions',
:payload => {
:entity => "custom_button_sets",
:redirect_url => "/generic_object_definition/",
:name => @record.name,
:labels => { :single => _("Button Group") }
}
}
}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ class ApplicationHelper::Toolbar::GenericObjectDefinitionButtonCenter < Applicat
:url => 'custom_button_edit',
),
button(
:ab_button_delete,
:custom_button_delete,
'pficon pficon-delete fa-lg',
N_('Remove this Button from Inventory'),
:data => {'function' => 'sendDataWithRx',
'function-data' => '{"type": "delete_custom_button", "controller": "genericObjectDefinitionToolbarController", "entity": "Button"}'},
N_('Remove this Custom Button from Inventory'),
:confirm => N_("Warning: This Button will be permanently removed!"),
:klass => ApplicationHelper::Button::GenericObjectDefinitionButtonButtonDelete
)
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ class ApplicationHelper::Toolbar::GenericObjectDefinitionButtonGroupCenter < App
:url => 'custom_button_new',
),
button(
:ab_button_delete,
:custom_button_set_delete,
'pficon pficon-delete fa-lg',
N_('Remove this Button Group from Inventory'),
:data => {'function' => 'sendDataWithRx',
'function-data' => '{"type": "delete_custom_button_set", "controller": "genericObjectDefinitionToolbarController", "entity": "Button Group"}'},
N_('Remove this Custom Button Group from Inventory'),
:confirm => N_("Warning: This Custom Button Group will be permanently removed!"),
:klass => ApplicationHelper::Button::GenericObjectDefinitionButtonButtonGroupDelete,
:confirm => N_("Warning: This Button Group will be permanently removed!"),
),
]
)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper/toolbar_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def build_select_button(bgi, index)
def apply_common_props(button, input)
button.update(
:color => input[:color],
:data => input[:data],
:data => button.data(input[:data]),
:hidden => button[:hidden] || !!input[:hidden],
:icon => input[:icon],
:name => button[:id],
Expand Down
11 changes: 8 additions & 3 deletions app/javascript/toolbar-actions/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ export function generateMessages(results) {
}, { false: 0, true: 0 });
}

export function APIDelete(entity, resources, labels) {
export function APIDelete(entity, resources, labels = { single: '', multiple: '' }, redirectUrl, name) {
API.post(`/api/${entity}`, {
action: 'delete',
resources,
}).then((data) => {
showMessage(generateMessages(data.results), labels);
if (redirectUrl) {
miqFlashLater({ message: sprintf(__('%s: "%s" was successfully deleted'), labels.single, name) });
window.location.href = redirectUrl;
} else {
showMessage(generateMessages(data.results), labels);
};
return data;
});
}
Expand All @@ -41,6 +46,6 @@ export function onDelete(data, resources) {
if (data.customAction) {
customActionDelete(data, resources);
} else {
APIDelete(data.entity, resources, data.labels);
APIDelete(data.entity, resources, data.labels, data.redirect_url, data.name);
}
}

0 comments on commit 6c5707c

Please sign in to comment.