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

[RFR] Add ability to customize delete error message #982

Merged
merged 1 commit into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 8 additions & 12 deletions src/javascripts/ng-admin/Crud/delete/DeleteController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export default class DeleteController {
constructor($scope, $window, $state, $q, WriteQueries, notification, params, view, entry) {
constructor($scope, $window, $state, $q, WriteQueries, Configuration, notification, params, view, entry) {
this.$scope = $scope;
this.$window = $window;
this.$state = $state;
this.WriteQueries = WriteQueries;
this.config = Configuration();
this.entityLabel = params.entity;
this.entityId = params.id;
this.view = view;
Expand Down Expand Up @@ -42,17 +43,12 @@ export default class DeleteController {

notification.log('Element successfully deleted.', { addnCls: 'humane-flatty-success' });
});
},
response => {
// @TODO: share this method when splitting controllers
var body = response.data;
if (typeof body === 'object') {
body = JSON.stringify(body);
}

notification.log('Oops, an error occured : (code: ' + response.status + ') ' + body, {addnCls: 'humane-flatty-error'});
}
);
)
.catch(error => {
const errorMessage = this.config.getErrorMessageFor(this.view, error);
notification.log(errorMessage, {addnCls: 'humane-flatty-error'});
});
}

back() {
Expand All @@ -67,4 +63,4 @@ export default class DeleteController {
}
}

DeleteController.$inject = ['$scope', '$window', '$state', '$q', 'WriteQueries', 'notification', 'params', 'view', 'entry'];
DeleteController.$inject = ['$scope', '$window', '$state', '$q', 'WriteQueries', 'NgAdminConfiguration', 'notification', 'params', 'view', 'entry'];
13 changes: 9 additions & 4 deletions src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('DeleteController', function () {
Entity = require('admin-config/lib/Entity/Entity'),
humane = require('humane-js');

var $scope, $window, $state, $q, writeQueries, notification, params, view, entry;
var $scope, $window, $state, $q, writeQueries, Configuration, notification, params, view, entry;
beforeEach(inject(function ($controller, $rootScope, _$window_, _$q_) {
$scope = $rootScope.$new();
$window = _$window_;
Expand All @@ -18,6 +18,11 @@ describe('DeleteController', function () {
writeQueries = {
deleteOne: jasmine.createSpy('writeQueries.deleteOne').and.callFake(() => $q.when())
};
Configuration = () => ({
getErrorMessageFor: function (view, response) {
return 'An error occured: ' + response;
}
});
notification = humane;
params = {
id: 3,
Expand Down Expand Up @@ -45,7 +50,7 @@ describe('DeleteController', function () {
getEntity: () => entity
};

let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, notification, {
let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, Configuration, notification, {
id: deletedId,
entity: 'post'
}, view, entry);
Expand All @@ -72,7 +77,7 @@ describe('DeleteController', function () {
getEntity: () => entity
};

let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, notification, {
let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, Configuration, notification, {
id: deletedId,
entity: 'post'
}, view, entry);
Expand Down Expand Up @@ -103,7 +108,7 @@ describe('DeleteController', function () {
};

let $window = { history: { back: jasmine.createSpy('$window.history.back') } };
let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, notification, {
let deleteController = new DeleteController($scope, $window, $state, $q, writeQueries, Configuration, notification, {
id: commentId,
entity: 'comment'
}, view, entry);
Expand Down