Skip to content

Commit b4cef5f

Browse files
committed
fixed tag name validation & switched to displaying all error messages
1 parent 150aede commit b4cef5f

5 files changed

Lines changed: 35 additions & 24 deletions

File tree

app/controllers/tags_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def rename
155155
render json: { status: 'success', tag: @tag }
156156
else
157157
render json: { status: 'failed',
158-
message: helpers.tag_rename_error_msg(@tag),
158+
errors: @tag.errors.messages.values.flatten,
159159
tag: @tag },
160160
status: :bad_request
161161
end

app/helpers/tags_helper.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,4 @@ def post_ids_for_tags(tag_ids)
3838
sql = "SELECT post_id FROM posts_tags WHERE tag_id IN #{ApplicationRecord.sanitize_sql_in(tag_ids)}"
3939
ActiveRecord::Base.connection.execute(sql).to_a.flatten
4040
end
41-
42-
# Gets a standard tag rename error message for a tag
43-
# @param post [Tag] target tag
44-
# @return [String] error message
45-
def tag_rename_error_msg(tag)
46-
if tag.errors.where(:name, :taken)
47-
I18n.t('tags.errors.rename_taken')
48-
else
49-
I18n.t('tags.errors.rename_generic')
50-
end
51-
end
5241
end

app/models/tag.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,21 @@ class Tag < ApplicationRecord
1212

1313
validates :excerpt, length: { maximum: 600 }, allow_blank: true
1414
validates :wiki_markdown, length: { maximum: 30_000 }, allow_blank: true
15-
validates :name, presence: true, format: { with: /[^ \t]+/, message: 'Tag names may not include spaces' }
15+
validates :name, presence: {
16+
message: I18n.t('tags.errors.no_blank_name')
17+
}
18+
validates :name, format: {
19+
with: /\A[^\s]+\Z/, # https://regex101.com/r/7BxgIn/1
20+
message: I18n.t('tags.errors.no_spaces_in_name')
21+
}
1622
validate :parent_not_self
1723
validate :parent_not_own_child
1824
validate :synonym_unique
19-
validates :name, uniqueness: { scope: [:tag_set_id], case_sensitive: false }
25+
validates :name, uniqueness: {
26+
scope: [:tag_set_id],
27+
case_sensitive: false,
28+
message: I18n.t('tags.errors.no_duplicate_names')
29+
}
2030

2131
# scopes
2232
scope :list_includes, -> { includes(:tag_synonyms) }

config/locales/strings/en.tags.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
en:
22
tags:
33
errors:
4-
rename_taken: >
4+
no_blank_name: >
5+
Tag name should contain at least 1 character.
6+
no_duplicate_names: >
57
A tag with that name already exists.
8+
no_spaces_in_name: >
9+
Tag names may not contain spaces.
610
rename_generic: >
711
Failed to rename the tag.

test/controllers/tags_controller_test.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,23 @@ class TagsControllerTest < ActionController::TestCase
211211
sign_in users(:moderator)
212212

213213
tag = tags(:base)
214-
old_tag_name = tag.name
215-
216-
try_rename_tag(categories(:main), tag, '')
217-
218-
assert_json_failure(:bad_request)
219-
res_body = JSON.parse(response.body)
220-
assert_equal @controller.helpers.tag_rename_error_msg(tag), res_body['message']
221-
tag.reload
222-
assert_equal tag.name, old_tag_name
214+
old_name = tag.name
215+
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+
223+
try_rename_tag(categories(:main), tag, new_name)
224+
225+
assert_json_failure(:bad_request)
226+
res_body = JSON.parse(response.body)
227+
assert res_body['errors'].include?(error_message)
228+
tag.reload
229+
assert_equal tag.name, old_name
230+
end
223231
end
224232

225233
private

0 commit comments

Comments
 (0)