Move comments form to component

This commit is contained in:
taitus
2023-12-13 14:39:29 +01:00
parent bca3415aa6
commit 45d82d6e6b
8 changed files with 82 additions and 51 deletions

View File

@@ -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) %> <% if comments_closed_for_commentable?(commentable) %>
<br> <br>
<div data-alert class="callout primary"> <div data-alert class="callout primary">
<%= comments_closed_text(commentable) %> <%= comments_closed_text %>
</div> </div>
<% elsif require_verified_resident_for_commentable?(commentable, current_user) %> <% elsif require_verified_resident_for_commentable?(commentable, current_user) %>
<br> <br>

View File

@@ -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

View File

@@ -4,7 +4,7 @@
<% cache cache_key do %> <% cache cache_key do %>
<% if current_user %> <% if current_user %>
<%= render "comments/form", { commentable: record, parent_id: nil, valuation: valuation } %> <%= render Comments::FormComponent.new(record, valuation: valuation) %>
<% else %> <% else %>
<%= render "shared/login_to_comment" %> <%= render "shared/login_to_comment" %>
<% end %> <% end %>

View File

@@ -74,12 +74,4 @@ module CommentsHelper
def comments_closed_for_commentable?(commentable) def comments_closed_for_commentable?(commentable)
commentable.respond_to?(:comments_closed?) && commentable.comments_closed? commentable.respond_to?(:comments_closed?) && commentable.comments_closed?
end 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 end

View File

@@ -99,9 +99,9 @@
<% end %> <% end %>
<% if !valuation || can?(:comment_valuation, comment.commentable) %> <% if !valuation || can?(:comment_valuation, comment.commentable) %>
<%= render "comments/form", { commentable: comment.commentable, <%= render Comments::FormComponent.new(comment.commentable,
parent_id: comment.id, parent_id: comment.id,
valuation: valuation } %> valuation: valuation) %>
<% end %> <% end %>
<% end %> <% end %>
</div> </div>

View File

@@ -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

View File

@@ -236,25 +236,6 @@ describe "Commenting legislation questions" do
expect(page).to have_content "Can't be blank" expect(page).to have_content "Can't be blank"
end 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 scenario "Reply" do
citizen = create(:user, username: "Ana") citizen = create(:user, username: "Ana")
manuela = create(:user, username: "Manuela") manuela = create(:user, username: "Manuela")

View File

@@ -231,24 +231,6 @@ describe "Commenting legislation questions" do
expect(page).to have_content "Can't be blank" expect(page).to have_content "Can't be blank"
end 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 scenario "Reply" do
citizen = create(:user, username: "Ana") citizen = create(:user, username: "Ana")
manuela = create(:user, :level_two, username: "Manuela") manuela = create(:user, :level_two, username: "Manuela")