Skip to content

Commit

Permalink
Do not allow comments on Article if not published
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Feb 13, 2022
1 parent ce515b3 commit 0e6c66a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion publify_core/app/models/article.rb
Expand Up @@ -204,7 +204,7 @@ def notify_user_via_email(user)
end

def comments_closed?
!(allow_comments? && in_feedback_window?)
!(allow_comments? && published? && in_feedback_window?)
end

def html_urls
Expand Down
15 changes: 15 additions & 0 deletions publify_core/spec/controllers/comments_controller_spec.rb
Expand Up @@ -58,6 +58,21 @@
expect(response.body).to have_text "content"
end
end

it "does not allow commenting if article does not allow comments" do
no_comments = create(:article, allow_comments: false)
expect do
post :create, xhr: true, params: { comment: comment_params,
article_id: no_comments.id }
end.not_to change(no_comments.comments, :count)
end

it "does not allow commenting if article is draft" do
draft = create(:article, state: "draft")
expect do
post :create, xhr: true, params: { comment: comment_params, article_id: draft.id }
end.not_to change(draft.comments, :count)
end
end

describe "#preview" do
Expand Down
14 changes: 13 additions & 1 deletion publify_core/spec/models/article_spec.rb
Expand Up @@ -856,8 +856,9 @@
it "returns only published articles" do
article = create(:article)
create(:comment, article: article)
unpublished_article = create(:article, state: "draft")
unpublished_article = create(:article)
create(:comment, article: unpublished_article)
unpublished_article.update!(state: "draft")
expect(described_class.published).to eq([article])
expect(described_class.bestof).to eq([article])
end
Expand Down Expand Up @@ -955,6 +956,17 @@
context "when auto_close setting is zero" do
let(:auto_close_value) { 0 }

it "does not allow comments for a draft article" do
art = build :article, state: "draft", blog: blog
assert art.comments_closed?
end

it "does not allow comments for an article that will be published in the future" do
art = build :article, state: "publication_pending",
published_at: 1.day.from_now, blog: blog
assert art.comments_closed?
end

it "allows comments for a newly published article" do
art = build :article, published_at: 1.second.ago, blog: blog
assert !art.comments_closed?
Expand Down

0 comments on commit 0e6c66a

Please sign in to comment.