Skip to content

Commit f9514ea

Browse files
authored
Merge pull request metabrainz#1931 from reosarevok/MBS-10076
MBS-10076 / MBS-11403: Friendlier error when deleting attribute that has children
2 parents 9527e20 + 159e5e2 commit f9514ea

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

lib/MusicBrainz/Server/Controller/Admin/Attributes.pm

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package MusicBrainz::Server::Controller::Admin::Attributes;
22
use Moose;
33

4+
use MusicBrainz::Server::Translation qw( l ln );
5+
46
no if $] >= 5.018, warnings => "experimental::smartmatch";
57

68
BEGIN { extends 'MusicBrainz::Server::Controller' };
@@ -115,10 +117,28 @@ sub delete : Chained('attribute_base') Args(1) RequireAuth(account_admin) Secure
115117
$c->stash->{attribute} = $attr;
116118

117119
if ($c->model($model)->in_use($id)) {
118-
$c->stash->{template} = 'admin/attributes/in_use.tt';
120+
my $error_message = l('You cannot remove the attribute "{name}" because it is still in use.', { name => $attr->name });
121+
122+
$c->stash(
123+
current_view => 'Node',
124+
component_path => 'admin/attributes/CannotRemoveAttribute',
125+
component_props => {message => $error_message}
126+
);
127+
119128
$c->detach;
120129
}
121130

131+
if ($c->model($model)->has_children($id)) {
132+
my $error_message = l('You cannot remove the attribute “{name}” because it is the parent of other attributes.', { name => $attr->name });
133+
134+
$c->stash(
135+
current_view => 'Node',
136+
component_path => 'admin/attributes/CannotRemoveAttribute',
137+
component_props => {message => $error_message}
138+
);
139+
140+
$c->detach;
141+
}
122142
if ($c->form_posted_and_valid($form)) {
123143
$c->model('MB')->with_transaction(sub {
124144
$c->model($model)->delete($id);

lib/MusicBrainz/Server/Data/Role/Attribute.pm

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ sub _column_mapping {
1919
};
2020
}
2121

22+
sub has_children {
23+
my ($self, $id) = @_;
24+
return $self->sql->select_single_value(
25+
'SELECT 1 FROM ' . $self->_table . ' WHERE parent = ? LIMIT 1',
26+
$id);
27+
}
28+
2229
no Moose;
2330
1;
2431

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @flow strict-local
3+
* Copyright (C) 2021 MetaBrainz Foundation
4+
*
5+
* This file is part of MusicBrainz, the open internet music database,
6+
* and is licensed under the GPL version 2, or (at your option) any
7+
* later version: http://www.gnu.org/licenses/gpl-2.0.txt
8+
*/
9+
10+
import * as React from 'react';
11+
12+
import Layout from '../../layout';
13+
14+
type Props = {
15+
+$c: CatalystContextT,
16+
+message: string,
17+
};
18+
19+
const CannotRemoveAttribute = ({
20+
$c,
21+
message,
22+
}: Props): React.Element<typeof Layout> => (
23+
<Layout $c={$c} fullWidth title={l('Cannot Remove Attribute')}>
24+
<h1>{l('Cannot Remove Attribute')}</h1>
25+
<p>
26+
{message}
27+
</p>
28+
</Layout>
29+
);
30+
31+
export default CannotRemoveAttribute;

root/admin/attributes/in_use.tt

-4
This file was deleted.

root/server/components.js

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = {
3939
'admin/EmailSearch': require('../admin/EmailSearch'),
4040
'admin/IpLookup': require('../admin/IpLookup'),
4141
'admin/attributes/Attribute': require('../admin/attributes/Attribute'),
42+
'admin/attributes/CannotRemoveAttribute': require('../admin/attributes/CannotRemoveAttribute'),
4243
'admin/attributes/Index': require('../admin/attributes/Index'),
4344
'admin/attributes/Language': require('../admin/attributes/Language'),
4445
'admin/attributes/Script': require('../admin/attributes/Script'),

0 commit comments

Comments
 (0)