Merge pull request #4960 from discourse/category-description-error

FIX: Explicit error when category description post is bad
This commit is contained in:
Guo Xiang Tan 2017-07-26 08:54:22 +09:00 committed by GitHub
commit 96267f0845
3 changed files with 24 additions and 5 deletions

View File

@ -485,6 +485,7 @@ en:
invalid_email_in: "'%{email}' is not a valid email address."
email_already_used_in_group: "'%{email}' is already used by the group '%{group_name}'."
email_already_used_in_category: "'%{email}' is already used by the category '%{category_name}'."
description_incomplete: "The category description post must have at least one paragraph."
cannot_delete:
uncategorized: "Can't delete Uncategorized"
has_subcategories: "Can't delete this category because it has sub-categories."

View File

@ -442,11 +442,13 @@ class PostRevisor
doc = Nokogiri::HTML.fragment(@post.cooked)
doc.css("img").remove
html = doc.css("p").first.inner_html.strip
new_description = html unless html.starts_with?(Category.post_template[0..50])
category.update_column(:description, new_description)
@category_changed = category
if html = doc.css("p").first&.inner_html&.strip
new_description = html unless html.starts_with?(Category.post_template[0..50])
category.update_column(:description, new_description)
@category_changed = category
else
@post.errors[:base] << I18n.t("category.errors.description_incomplete")
end
end
def advance_draft_sequence

View File

@ -213,6 +213,22 @@ describe PostRevisor do
end
end
context "invalid description without paragraphs" do
before do
subject.revise!(post.user, { raw: "# This is a title" })
category.reload
end
it "returns a error for the user" do
expect(post.errors.present?).to eq(true)
expect(post.errors.messages[:base].first).to be I18n.t("category.errors.description_incomplete")
end
it "doesn't update the description of the category" do
expect(category.description).to eq(nil)
end
end
context 'when updating back to the original paragraph' do
before do
category.update_column(:description, 'this is my description')