From 5b97e85dd75cbb0e171ea1019ae97f7c81b7f6cf Mon Sep 17 00:00:00 2001 From: decabeza Date: Wed, 20 Jul 2022 17:23:41 +0200 Subject: [PATCH] Hide comments when allegations phase is closed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Julian Nicolas Herrero Co-Authored-By: Javi Martín --- app/models/legislation/annotation.rb | 4 ++++ spec/controllers/comments_controller_spec.rb | 22 +++++++++++++++++++ .../comments/legislation_annotations_spec.rb | 19 ++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/app/models/legislation/annotation.rb b/app/models/legislation/annotation.rb index ebe7e0c10..c9fb4d046 100644 --- a/app/models/legislation/annotation.rb +++ b/app/models/legislation/annotation.rb @@ -55,4 +55,8 @@ class Legislation::Annotation < ApplicationRecord def weight comments_count + comments.sum(:cached_votes_total) end + + def comments_closed? + !draft_version.process.allegations_phase.open? + end end diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index 6f9cfc9b2..33aaf7047 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -41,6 +41,28 @@ describe CommentsController do end.not_to change { question.reload.comments_count } end + it "does not create an annotation comment if the allegations phase is closed" do + process = create(:legislation_process, + allegations_start_date: 2.days.from_now, + allegations_end_date: 1.month.from_now) + + version = create(:legislation_draft_version, process: process) + annotation = create(:legislation_annotation, draft_version: version, text: "One annotation") + + sign_in user + + expect do + post :create, xhr: true, + params: { + comment: { + commentable_id: annotation.id, + commentable_type: "Legislation::Annotation", + body: "a comment" + } + } + end.not_to change { annotation.reload.comments_count } + end + it "does not create a comment for unverified users when the commentable requires it" do sign_in unverified_user diff --git a/spec/system/comments/legislation_annotations_spec.rb b/spec/system/comments/legislation_annotations_spec.rb index 07658eafb..a9966442b 100644 --- a/spec/system/comments/legislation_annotations_spec.rb +++ b/spec/system/comments/legislation_annotations_spec.rb @@ -254,6 +254,25 @@ describe "Commenting legislation questions" do expect(page).to have_content "Can't be blank" end + scenario "Comments are disabled when the allegations phase is closed" do + process = create(:legislation_process, + allegations_start_date: 1.month.ago, + allegations_end_date: Date.yesterday) + + version = create(:legislation_draft_version, process: process) + annotation = create(:legislation_annotation, draft_version: version, text: "One annotation") + + login_as(user) + + visit legislation_process_draft_version_annotation_path(version.process, version, annotation) + + within "#comments" do + expect(page).to have_content "Comments are closed" + expect(page).not_to have_content "Leave your comment" + expect(page).not_to have_button "Publish comment" + end + end + scenario "Reply" do citizen = create(:user, username: "Ana") manuela = create(:user, username: "Manuela")