Remove duplication rendering comments

We were using the same code 5 times, with the only slight variation
being the extra heading in the debates section.
This commit is contained in:
Javi Martín
2021-06-26 21:45:20 +02:00
parent 81f274a319
commit c3e0a6b089
8 changed files with 42 additions and 90 deletions

View File

@@ -0,0 +1,19 @@
<% cache [locale_and_user_status, current_order, commentable_cache_key(record), comment_tree.comments, comment_tree.comment_authors, record.comments_count] do %>
<div class="row comments">
<div id="comments" class="small-12 column">
<%= content %>
<%= render "shared/wide_order_selector", i18n_namespace: "comments" %>
<% if current_user %>
<%= render "comments/form", { commentable: record, parent_id: nil } %>
<% else %>
<br>
<%= render "shared/login_to_comment" %>
<% end %>
<%= render "comments/comment_list", comments: comment_tree.root_comments %>
<%= paginate comment_tree.root_comments %>
</div>
</div>
<% end %>

View File

@@ -0,0 +1,9 @@
class Shared::CommentsComponent < ApplicationComponent
attr_reader :record, :comment_tree
delegate :current_user, :current_order, :locale_and_user_status, :commentable_cache_key, to: :helpers
def initialize(record, comment_tree)
@record = record
@comment_tree = comment_tree
end
end

View File

@@ -1,5 +1,10 @@
module HasOrders module HasOrders
extend ActiveSupport::Concern extend ActiveSupport::Concern
attr_reader :valid_orders, :current_order
included do
helper_method :valid_orders, :current_order
end
class_methods do class_methods do
def has_orders(valid_orders, *args) def has_orders(valid_orders, *args)

View File

@@ -1,22 +1,6 @@
<% cache [locale_and_user_status, @current_order, commentable_cache_key(@debate), @comment_tree.comments, @comment_tree.comment_authors, @debate.comments_count] do %> <%= render Shared::CommentsComponent.new(@debate, @comment_tree) do %>
<div class="row comments"> <h3>
<div id="comments" class="small-12 column"> <%= t("debates.show.comments_title") %>
<h3> <span class="js-comments-count">(<%= @debate.comments_count %>)</span>
<%= t("debates.show.comments_title") %> </h3>
<span class="js-comments-count">(<%= @debate.comments_count %>)</span>
</h3>
<%= render "shared/wide_order_selector", i18n_namespace: "comments" %>
<% if user_signed_in? %>
<%= render "comments/form", { commentable: @debate, parent_id: nil } %>
<% else %>
<br>
<%= render "shared/login_to_comment" %>
<% end %>
<%= render "comments/comment_list", comments: @comment_tree.root_comments %>
<%= paginate @comment_tree.root_comments %>
</div>
</div>
<% end %> <% end %>

View File

@@ -1,17 +1 @@
<% cache [locale_and_user_status, @current_order, commentable_cache_key(@proposal), @comment_tree.comments, @comment_tree.comment_authors, @proposal.comments_count] do %> <%= render Shared::CommentsComponent.new(@proposal, @comment_tree) %>
<div class="row comments">
<div id="comments" class="small-12 column">
<%= render "shared/wide_order_selector", i18n_namespace: "comments" %>
<% if user_signed_in? %>
<%= render "comments/form", { commentable: @proposal, parent_id: nil } %>
<% else %>
<br>
<%= render "shared/login_to_comment" %>
<% end %>
<%= render "comments/comment_list", comments: @comment_tree.root_comments %>
<%= paginate @comment_tree.root_comments %>
</div>
</div>
<% end %>

View File

@@ -1,17 +1 @@
<% cache [locale_and_user_status, @current_order, commentable_cache_key(@poll), @comment_tree.comments, @comment_tree.comment_authors, @poll.comments_count] do %> <%= render Shared::CommentsComponent.new(@poll, @comment_tree) %>
<div class="row comments">
<div id="comments" class="small-12 column">
<%= render "shared/wide_order_selector", i18n_namespace: "comments" %>
<% if user_signed_in? %>
<%= render "comments/form", { commentable: @poll, parent_id: nil } %>
<% else %>
<br>
<%= render "shared/login_to_comment" %>
<% end %>
<%= render "comments/comment_list", comments: @comment_tree.root_comments %>
<%= paginate @comment_tree.root_comments %>
</div>
</div>
<% end %>

View File

@@ -1,17 +1 @@
<% cache [locale_and_user_status, @current_order, commentable_cache_key(@proposal), @comment_tree.comments, @comment_tree.comment_authors, @proposal.comments_count] do %> <%= render Shared::CommentsComponent.new(@proposal, @comment_tree) %>
<div class="row comments">
<div id="comments" class="small-12 column">
<%= render "shared/wide_order_selector", i18n_namespace: "comments" %>
<% if user_signed_in? %>
<%= render "comments/form", { commentable: @proposal, parent_id: nil } %>
<% else %>
<br>
<%= render "shared/login_to_comment" %>
<% end %>
<%= render "comments/comment_list", comments: @comment_tree.root_comments %>
<%= paginate @comment_tree.root_comments %>
</div>
</div>
<% end %>

View File

@@ -1,18 +1 @@
<% cache [locale_and_user_status, @current_order, commentable_cache_key(@topic), @comment_tree.comments, @comment_tree.comment_authors, @topic.comments_count] do %> <%= render Shared::CommentsComponent.new(@topic, @comment_tree) %>
<div class="row comments">
<div id="comments" class="small-12 column">
<%= render "shared/wide_order_selector", i18n_namespace: "comments" %>
<% if user_signed_in? %>
<%= render "comments/form", { commentable: @topic, parent_id: nil } %>
<% else %>
<br>
<%= render "shared/login_to_comment" %>
<% end %>
<%= render "comments/comment_list", comments: @comment_tree.root_comments %>
<%= paginate @comment_tree.root_comments %>
</div>
</div>
<% end %>