diff --git a/app/views/comments/_form.html.erb b/app/components/comments/form_component.html.erb similarity index 93% rename from app/views/comments/_form.html.erb rename to app/components/comments/form_component.html.erb index 01b210c79..767dd15d4 100644 --- a/app/views/comments/_form.html.erb +++ b/app/components/comments/form_component.html.erb @@ -1,8 +1,8 @@ -<% cache [locale_and_user_status, parent_id, commentable_cache_key(commentable), valuation] do %> +<% cache cache_key do %> <% if comments_closed_for_commentable?(commentable) %>
- <%= comments_closed_text(commentable) %> + <%= comments_closed_text %>
<% elsif require_verified_resident_for_commentable?(commentable, current_user) %>
diff --git a/app/components/comments/form_component.rb b/app/components/comments/form_component.rb new file mode 100644 index 000000000..e690b770e --- /dev/null +++ b/app/components/comments/form_component.rb @@ -0,0 +1,32 @@ +class Comments::FormComponent < ApplicationComponent + attr_reader :commentable, :parent_id, :valuation + use_helpers :current_user, :locale_and_user_status, :commentable_cache_key, + :comments_closed_for_commentable?, :require_verified_resident_for_commentable?, + :link_to_verify_account, :parent_or_commentable_dom_id, :leave_comment_text, :can?, + :comment_button_text + + def initialize(commentable, parent_id: nil, valuation: false) + @commentable = commentable + @parent_id = parent_id + @valuation = valuation + end + + private + + def cache_key + [ + locale_and_user_status, + parent_id, + commentable_cache_key(commentable), + valuation + ] + end + + def comments_closed_text + if commentable.class == Legislation::Question + t("legislation.questions.comments.comments_closed") + else + t("comments.comments_closed") + end + end +end diff --git a/app/components/shared/comments_component.html.erb b/app/components/shared/comments_component.html.erb index e4eee07b7..864f9cce4 100644 --- a/app/components/shared/comments_component.html.erb +++ b/app/components/shared/comments_component.html.erb @@ -4,7 +4,7 @@ <% cache cache_key do %> <% if current_user %> - <%= render "comments/form", { commentable: record, parent_id: nil, valuation: valuation } %> + <%= render Comments::FormComponent.new(record, valuation: valuation) %> <% else %> <%= render "shared/login_to_comment" %> <% end %> diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb index 225748365..866b1e1a4 100644 --- a/app/helpers/comments_helper.rb +++ b/app/helpers/comments_helper.rb @@ -74,12 +74,4 @@ module CommentsHelper def comments_closed_for_commentable?(commentable) commentable.respond_to?(:comments_closed?) && commentable.comments_closed? end - - def comments_closed_text(commentable) - if commentable.class == Legislation::Question - t("legislation.questions.comments.comments_closed") - else - t("comments.comments_closed") - end - end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 542fa0d71..7b7a67c6e 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -99,9 +99,9 @@ <% end %> <% if !valuation || can?(:comment_valuation, comment.commentable) %> - <%= render "comments/form", { commentable: comment.commentable, - parent_id: comment.id, - valuation: valuation } %> + <%= render Comments::FormComponent.new(comment.commentable, + parent_id: comment.id, + valuation: valuation) %> <% end %> <% end %> diff --git a/spec/components/comments/form_component_spec.rb b/spec/components/comments/form_component_spec.rb new file mode 100644 index 000000000..3300b5f76 --- /dev/null +++ b/spec/components/comments/form_component_spec.rb @@ -0,0 +1,44 @@ +require "rails_helper" + +describe Comments::FormComponent do + context "Legislation annotation" do + it "disables comments 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") + + render_inline Comments::FormComponent.new(annotation) + + 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 + + context "Legislation question" do + let(:process) { create(:legislation_process, :in_debate_phase) } + let(:question) { create(:legislation_question, process: process) } + + it "prevents unverified users from creating comments" do + unverified_user = create(:user) + sign_in unverified_user + + render_inline Comments::FormComponent.new(question) + + expect(page).to have_content "To participate verify your account" + end + + it "blocks comment creation when the debate phase is not open" do + user = create(:user, :level_two) + process.update!(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day) + sign_in(user) + + render_inline Comments::FormComponent.new(question) + + expect(page).to have_content "Closed phase" + end + end +end diff --git a/spec/system/comments/legislation_annotations_spec.rb b/spec/system/comments/legislation_annotations_spec.rb index 0cbfe0997..3e779b7bb 100644 --- a/spec/system/comments/legislation_annotations_spec.rb +++ b/spec/system/comments/legislation_annotations_spec.rb @@ -236,25 +236,6 @@ 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 polymorphic_path(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") diff --git a/spec/system/comments/legislation_questions_spec.rb b/spec/system/comments/legislation_questions_spec.rb index 6412396fe..3e11e573a 100644 --- a/spec/system/comments/legislation_questions_spec.rb +++ b/spec/system/comments/legislation_questions_spec.rb @@ -231,24 +231,6 @@ describe "Commenting legislation questions" do expect(page).to have_content "Can't be blank" end - scenario "Unverified user can't create comments" do - unverified_user = create(:user) - login_as(unverified_user) - - visit legislation_process_question_path(question.process, question) - - expect(page).to have_content "To participate verify your account" - end - - scenario "Can't create comments if debate phase is not open" do - process.update!(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day) - login_as(user) - - visit legislation_process_question_path(question.process, question) - - expect(page).to have_content "Closed phase" - end - scenario "Reply" do citizen = create(:user, username: "Ana") manuela = create(:user, :level_two, username: "Manuela")