diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index a3a431e1b51..a0db80bda0f 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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." diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index 9b9ef68d54b..831faa47b43 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -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 diff --git a/spec/components/post_revisor_spec.rb b/spec/components/post_revisor_spec.rb index eafe106c9f9..b32fdcdf8c9 100644 --- a/spec/components/post_revisor_spec.rb +++ b/spec/components/post_revisor_spec.rb @@ -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')