Skip to content

Commit

Permalink
Merge pull request consuldemocracy#5369 from consuldemocracy/refactor…
Browse files Browse the repository at this point in the history
…_comments_specs

Make comments specs faster
  • Loading branch information
taitus authored Mar 25, 2024
2 parents 2f59fde + 6987c2e commit d76f954
Show file tree
Hide file tree
Showing 26 changed files with 428 additions and 4,305 deletions.
46 changes: 46 additions & 0 deletions app/components/comments/form_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<% cache cache_key do %>
<% if comments_closed_for_commentable?(commentable) %>
<br>
<div data-alert class="callout primary">
<%= comments_closed_text %>
</div>
<% elsif require_verified_resident_for_commentable?(commentable, current_user) %>
<br>
<div data-alert class="callout primary">
<%= sanitize(t("comments.verified_only", verify_account: link_to_verify_account)) %>
</div>
<% elsif !valuation || can?(:comment_valuation, commentable) %>
<% css_id = parent_or_commentable_dom_id(parent_id, commentable) %>
<div id="js-comment-form-<%= css_id %>" class="comment-form">
<%= form_for Comment.new, remote: true, html: { id: "new_comment_#{css_id}" } do |f| %>
<%= f.text_area :body,
id: "comment-body-#{css_id}",
maxlength: Comment.body_max_length,
label: leave_comment_text(commentable) %>

<%= f.hidden_field :commentable_type, value: commentable.class.name, id: "comment_commentable_type_#{css_id}" %>
<%= f.hidden_field :commentable_id, value: commentable.id, id: "comment_commentable_id_#{css_id}" %>
<%= f.hidden_field :parent_id, value: parent_id, id: "comment_parent_id_#{css_id}" %>
<%= f.hidden_field :valuation, value: valuation, id: "comment_valuation_#{css_id}" %>

<%= f.submit comment_button_text(parent_id, commentable), class: "button", id: "publish_comment_#{css_id}" %>

<% if can? :comment_as_moderator, commentable %>
<div class="float-right">
<%= f.check_box :as_moderator,
label: t("comments.form.comment_as_moderator"),
id: "comment-as-moderator-#{css_id}" %>
</div>
<% end %>
<% if can? :comment_as_administrator, commentable %>
<div class="float-right">
<%= f.check_box :as_administrator,
label: t("comments.form.comment_as_admin"),
id: "comment-as-administrator-#{css_id}" %>
</div>
<% end %>

<% end %>
</div>
<% end %>
<% end %>
32 changes: 32 additions & 0 deletions app/components/comments/form_component.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions app/components/shared/comments_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

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

<%= render Shared::OrderLinksComponent.new("comments", anchor: "comments") %>
<%= render "comments/comment_list", comments: comment_tree.root_comments %>
<%= render "comments/comment_list", comments: comment_tree.root_comments, valuation: valuation %>
<%= paginate comment_tree.root_comments, params: { anchor: "comments" } %>
<% end %>
</div>
Expand Down
5 changes: 3 additions & 2 deletions app/components/shared/comments_component.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Shared::CommentsComponent < ApplicationComponent
attr_reader :record, :comment_tree
attr_reader :record, :comment_tree, :valuation
use_helpers :current_user, :current_order, :locale_and_user_status, :commentable_cache_key

def initialize(record, comment_tree)
def initialize(record, comment_tree, valuation: false)
@record = record
@comment_tree = comment_tree
@valuation = valuation
end

private
Expand Down
16 changes: 0 additions & 16 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
module CommentsHelper
def comment_tree_title_text(commentable)
if commentable.class == Legislation::Question
t("legislation.questions.comments.comments_title")
else
t("comments_helper.comments_title")
end
end

def leave_comment_text(commentable)
if commentable.class == Legislation::Question
t("legislation.questions.comments.form.leave_comment")
Expand Down Expand Up @@ -82,12 +74,4 @@ def require_verified_resident_for_commentable?(commentable, current_user)
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
4 changes: 1 addition & 3 deletions app/views/budgets/investments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
<div class="tabs-content" data-tabs-content="investments_tabs">
<div class="tabs-panel is-active" id="tab-comments">
<%= render MachineLearning::CommentsSummaryComponent.new(@investment) %>

<%= render "/comments/comment_tree", comment_tree: @comment_tree,
display_comments_count: false %>
<%= render Shared::CommentsComponent.new(@investment, @comment_tree) %>
</div>

<%= render "milestones/milestones", milestoneable: @investment %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
Expand Down
40 changes: 0 additions & 40 deletions app/views/comments/_comment_tree.html.erb

This file was deleted.

35 changes: 0 additions & 35 deletions app/views/comments/_form.html.erb

This file was deleted.

8 changes: 6 additions & 2 deletions app/views/legislation/annotations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@
</aside>
</div>

<%= render "/comments/comment_tree", comment_tree: @comment_tree,
display_comments_count: true %>
<%= render Shared::CommentsComponent.new(@annotation, @comment_tree) do %>
<h2>
<%= t("comments_helper.comments_title") %>
<span class="js-comments-count">(<%= @annotation.comments_count %>)</span>
</h2>
<% end %>
</div>
</div>
</div>
Expand Down
9 changes: 6 additions & 3 deletions app/views/legislation/questions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
<%= render "/shared/social_share", title: @question.title, url: legislation_process_question_url(@question.process, @question) %>
</aside>
</div>

<%= render "/comments/comment_tree", comment_tree: @comment_tree,
display_comments_count: true %>
<%= render Shared::CommentsComponent.new(@question, @comment_tree) do %>
<h2>
<%= t("legislation.questions.comments.comments_title") %>
<span class="js-comments-count">(<%= @question.comments_count %>)</span>
</h2>
<% end %>
</section>
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<h2><%= t("valuation.budget_investments.valuation_comments") %></h2>
<% unless @comment_tree.nil? %>
<%= render "/comments/comment_tree", comment_tree: @comment_tree,
display_comments_count: false,
valuation: true %>
<%= render Shared::CommentsComponent.new(@investment, @comment_tree, valuation: true) %>
<% end %>
44 changes: 44 additions & 0 deletions spec/components/comments/form_component_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/factories/classifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
community { create(:proposal).community }
end

trait :with_investment_community do
community { create(:budget_investment).community }
end

factory :topic_with_community, traits: [:with_community]
factory :topic_with_investment_community, traits: [:with_investment_community]
end

factory :related_content do
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
sequence(:body) { |n| "Comment body #{n}" }

%i[budget_investment debate legislation_annotation legislation_question legislation_proposal
poll proposal topic_with_community].each do |model|
poll proposal topic_with_community topic_with_investment_community].each do |model|
factory :"#{model}_comment" do
commentable factory: model
end
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/polls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@
after(:create) { |poll| create(:image, imageable: poll) }
end

trait :with_author do
author factory: :user
end

transient { officers { [] } }

after(:create) do |poll, evaluator|
evaluator.officers.each do |officer|
create(:poll_officer_assignment, poll: poll, officer: officer)
end
end

factory :poll_with_author, traits: [:with_author]
end

factory :poll_question, class: "Poll::Question" do
Expand Down
Loading

0 comments on commit d76f954

Please sign in to comment.