Skip to content

Commit 0bec68b

Browse files
committed
reworked tag rename errors & improved QPixel#handleJSONResponse's error parsing
1 parent 7b3c75a commit 0bec68b

5 files changed

Lines changed: 41 additions & 24 deletions

File tree

app/assets/javascripts/qpixel_api.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,22 @@ window.QPixel = {
433433
const isFailed = data.status === 'failed';
434434

435435
if(isFailed) {
436-
if (data.message) {
437-
QPixel.createNotification('danger', data.message);
438-
}
436+
const { errors = [], message } = data;
437+
438+
if (message) {
439+
const fullMessage =
440+
errors.length > 1
441+
? `${message}:<ul>${errors.map((e) => `<li>${e.trim()}</li>`).join('')}</ul>`
442+
: errors.length === 1
443+
? `${message} (${errors[0].toLowerCase().trim()})`
444+
: message;
439445

440-
for (const error of data.errors ?? []) {
441-
QPixel.createNotification('danger', error);
446+
QPixel.createNotification('danger', fullMessage);
447+
}
448+
else {
449+
for (const error of errors) {
450+
QPixel.createNotification('danger', error);
451+
}
442452
}
443453
}
444454
else {

app/controllers/tags_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ def rename
155155
render json: { status: 'success', tag: @tag }
156156
else
157157
render json: { status: 'failed',
158-
errors: @tag.errors.messages.values.flatten,
158+
message: I18n.t('tags.errors.rename_generic'),
159+
errors: @tag.errors.full_messages,
159160
tag: @tag },
160161
status: :bad_request
161162
end

app/models/tag.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ class Tag < ApplicationRecord
1313
validates :excerpt, length: { maximum: 600 }, allow_blank: true
1414
validates :wiki_markdown, length: { maximum: 30_000 }, allow_blank: true
1515
validates :name, presence: {
16-
message: I18n.t('tags.errors.no_blank_name')
16+
message: I18n.t('tags.validation.errors.no_blank_name')
1717
}
1818
validates :name, format: {
1919
with: /\A[^\s]+\Z/, # https://regex101.com/r/7BxgIn/1
20-
message: I18n.t('tags.errors.no_spaces_in_name')
20+
message: I18n.t('tags.validation.errors.no_spaces_in_name')
2121
}
2222
validate :parent_not_self
2323
validate :parent_not_own_child
2424
validate :synonym_unique
2525
validates :name, uniqueness: {
2626
scope: [:tag_set_id],
2727
case_sensitive: false,
28-
message: I18n.t('tags.errors.no_duplicate_names')
28+
message: I18n.t('tags.validation.errors.no_duplicate_names')
2929
}
3030

3131
# scopes

config/locales/strings/en.tags.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
en:
22
tags:
3+
validation:
4+
errors:
5+
no_blank_name: >
6+
should contain at least 1 character
7+
no_duplicate_names: >
8+
cannot match an already existing tag
9+
no_spaces_in_name: >
10+
should not contain spaces
311
errors:
4-
no_blank_name: >
5-
Tag name should contain at least 1 character.
6-
no_duplicate_names: >
7-
A tag with that name already exists.
8-
no_spaces_in_name: >
9-
Tag names may not contain spaces.
1012
rename_generic: >
11-
Failed to rename the tag.
13+
Failed to rename the tag

test/controllers/tags_controller_test.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,22 @@ class TagsControllerTest < ActionController::TestCase
213213
tag = tags(:base)
214214
old_name = tag.name
215215

216-
[
217-
['', I18n.t('tags.errors.no_blank_name')],
218-
['name with spaces', I18n.t('tags.errors.no_spaces_in_name')],
219-
[tags(:discussion).name, I18n.t('tags.errors.no_duplicate_names')]
220-
].each do |test_case|
221-
new_name, error_message = test_case
222-
216+
{
217+
'' => 'no_blank_name',
218+
'name with spaces' => 'no_spaces_in_name',
219+
tags(:discussion).name => 'no_duplicate_names'
220+
}.each do |new_name, error_key|
223221
try_rename_tag(categories(:main), tag, new_name)
224222

225223
assert_json_failure(:bad_request)
226224
res_body = JSON.parse(response.body)
227-
assert res_body['errors'].include?(error_message)
225+
error_message = I18n.t("tags.validation.errors.#{error_key}")
226+
227+
Rails.logger.warn(res_body['errors'])
228+
229+
assert res_body['errors'].any? { |msg| msg.include?(error_message) },
230+
"Expected '#{error_message}' to be among the errors"
231+
228232
tag.reload
229233
assert_equal tag.name, old_name
230234
end

0 commit comments

Comments
 (0)