From 5200d76be6757e0af3b992cd803b4d8ef6679f96 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 5 Sep 2015 21:54:14 +0200 Subject: [PATCH] caches comments --- app/views/comments/_comment.html.erb | 171 ++++++++++++++------------- app/views/comments/_form.html.erb | 45 +++---- app/views/debates/_comments.html.erb | 30 +++++ 3 files changed, 141 insertions(+), 105 deletions(-) create mode 100644 app/views/debates/_comments.html.erb diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 3ee61e9f4..8bf07a8b3 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,101 +1,104 @@ -
-
+<% cache [locale_and_user_status, comment, @commentable, Flag.flagged?(current_user, comment)] do %> +
+
- <% if comment.hidden? || comment.user.hidden? %> - <% if select_children(@comments, comment).size > 0 %> -
-

<%= t("debates.comment.deleted") %>

-
- <% end %> - <% else %> - <% if comment.as_administrator? %> - <%= image_tag("admin_avatar.png", size: 32, class: "admin-avatar left") %> - <% elsif comment.as_moderator? %> - <%= image_tag("moderator_avatar.png", size: 32, class: "moderator-avatar left") %> + <% if comment.hidden? || comment.user.hidden? %> + <% if select_children(@comments, comment).size > 0 %> +
+

<%= t("debates.comment.deleted") %>

+
+ <% end %> <% else %> - <% if comment.user.organization? %> - <%= image_tag("collective_avatar.png", size: 32, class: "avatar left") %> + <% if comment.as_administrator? %> + <%= image_tag("admin_avatar.png", size: 32, class: "admin-avatar left") %> + <% elsif comment.as_moderator? %> + <%= image_tag("moderator_avatar.png", size: 32, class: "moderator-avatar left") %> <% else %> - <%= avatar_image(comment.user, seed: comment.user_id, size: 32, class: "left") %> - <% end %> - <% if comment.user.hidden? %> - - <% end %> - <% end %> - -
-
- - <% if comment.as_administrator? %> - <%= t("debates.comment.admin") %> #<%= comment.administrator_id%> - <% elsif comment.as_moderator? %> - <%= t("debates.comment.moderator") %> #<%= comment.moderator_id%> + <% if comment.user.organization? %> + <%= image_tag("collective_avatar.png", size: 32, class: "avatar left") %> <% else %> + <%= avatar_image(comment.user, seed: comment.user_id, size: 32, class: "left") %> + <% end %> + <% if comment.user.hidden? %> + + <% end %> + <% end %> - <% if comment.user.hidden? %> - <%= t("debates.comment.user_deleted") %> +
+
+ + <% if comment.as_administrator? %> + <%= t("debates.comment.admin") %> #<%= comment.administrator_id%> + <% elsif comment.as_moderator? %> + <%= t("debates.comment.moderator") %> #<%= comment.moderator_id%> <% else %> - <%= comment.user.name %> - <% if comment.user.official? %> + + <% if comment.user.hidden? %> + <%= t("debates.comment.user_deleted") %> + <% else %> + <%= comment.user.name %> + <% if comment.user.official? %> +  •  + + <%= comment.user.official_position %> + + <% end %> + <% end %> + <% if comment.user.verified_organization? %>  •  - - <%= comment.user.official_position %> + + <%= t("shared.collective") %> <% end %> - <% end %> - <% if comment.user.verified_organization? %> -  •  - - <%= t("shared.collective") %> - - <% end %> - <% if comment.user_id == @commentable.author_id %> -  •  - - <%= t("debates.comment.author") %> - + <% if comment.user_id == @commentable.author_id %> +  •  + + <%= t("debates.comment.author") %> + + <% end %> + <% end %> +  • <%= time_ago_in_words(comment.created_at) %> +
+ + <% if comment.as_administrator? %> +

<%= comment.body %>

+ <% elsif comment.as_moderator? %> +

<%= comment.body %>

+ <% elsif comment.user.official? && comment.user_id == @commentable.author_id %> +

<%= comment.body %>

+ <% elsif comment.user.official? %> +

<%= comment.body %>

+ <% elsif comment.user_id == @commentable.author_id %> +

<%= comment.body %>

+ <% else %> +

<%= comment.body %>

<% end %> + + <%= render 'comments/votes', comment: comment %> + -  • <%= time_ago_in_words(comment.created_at) %> +
+ <%= t("debates.comment.responses", count: select_children(@comments, comment).size) %> + + <% if user_signed_in? %> +  |  + <%= link_to(comment_link_text(comment), "", + class: "js-add-comment-link", data: {'id': dom_id(comment)}) %> + + <%= render 'comments/actions', comment: comment %> + + <%= render 'comments/form', {commentable: @commentable, parent_id: comment.id, toggeable: true} %> + <% end %> +
- - <% if comment.as_administrator? %> -

<%= comment.body %>

- <% elsif comment.as_moderator? %> -

<%= comment.body %>

- <% elsif comment.user.official? && comment.user_id == @commentable.author_id %> -

<%= comment.body %>

- <% elsif comment.user.official? %> -

<%= comment.body %>

- <% elsif comment.user_id == @commentable.author_id %> -

<%= comment.body %>

- <% else %> -

<%= comment.body %>

- <% end %> - - <%= render 'comments/votes', comment: comment %> - - -
- <%= t("debates.comment.responses", count: select_children(@comments, comment).size) %> - - <% if user_signed_in? %> -  |  - <%= link_to(comment_link_text(comment), "", - class: "js-add-comment-link", data: {'id': dom_id(comment)}) %> - - <%= render 'comments/actions', comment: comment %> - - <%= render 'comments/form', {commentable: @commentable, parent_id: comment.id, toggeable: true} %> - <% end %> -
-
- <% end %> - + <% end %> +<% end %>
- <%= render select_children(@comments, comment) if @comments.present? %> + <% select_children(@comments, comment).each do |child| %> + <%= render 'comments/comment', comment: child %> + <% end %>
-
+
\ No newline at end of file diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index 9a7d26554..eb2507136 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,25 +1,28 @@ <% css_id = parent_or_commentable_dom_id(parent_id, commentable) %> -
> - <%= form_for [commentable, Comment.new], remote: true do |f| %> - <%= label_tag "comment-body-#{css_id}", t("comments.form.leave_comment") %> - <%= f.text_area :body, id: "comment-body-#{css_id}", label: false %> - <%= f.hidden_field :commentable_type, value: commentable.class.name %> - <%= f.hidden_field :commentable_id, value: commentable.id %> - <%= f.hidden_field :parent_id, value: parent_id %> +<% cache [locale_and_user_status, css_id] do %> +
> + <%= form_for [commentable, Comment.new], remote: true do |f| %> + <%= label_tag "comment-body-#{css_id}", t("comments.form.leave_comment") %> + <%= f.text_area :body, id: "comment-body-#{css_id}", label: false %> + <%= f.hidden_field :commentable_type, value: commentable.class.name %> + <%= f.hidden_field :commentable_id, value: commentable.id %> + <%= f.hidden_field :parent_id, value: parent_id %> - <%= f.submit comment_button_text(parent_id), class: "button radius small inline-block" %> + <%= f.submit comment_button_text(parent_id), class: "button radius small inline-block" %> + + <% if can? :comment_as_moderator, commentable %>º +
+ <%= f.check_box :as_moderator, id: "comment-as-moderator-#{css_id}", label: false %> + <%= label_tag "comment-as-moderator-#{css_id}", t("comments.form.comment_as_moderator"), class: "checkbox" %> +
+ <% end %> + <% if can? :comment_as_administrator, commentable %> +
+ <%= f.check_box :as_administrator, id: "comment-as-administrator-#{css_id}",label: false %> + <%= label_tag "comment-as-administrator-#{css_id}", t("comments.form.comment_as_admin"), class: "checkbox" %> +
+ <% end %> - <% if can? :comment_as_moderator, commentable %>º -
- <%= f.check_box :as_moderator, id: "comment-as-moderator-#{css_id}", label: false %> - <%= label_tag "comment-as-moderator-#{css_id}", t("comments.form.comment_as_moderator"), class: "checkbox" %> -
<% end %> - <% if can? :comment_as_administrator, commentable %> -
- <%= f.check_box :as_administrator, id: "comment-as-administrator-#{css_id}",label: false %> - <%= label_tag "comment-as-administrator-#{css_id}", t("comments.form.comment_as_admin"), class: "checkbox" %> -
- <% end %> - <% end %> -
+
+<% end %> \ No newline at end of file diff --git a/app/views/debates/_comments.html.erb b/app/views/debates/_comments.html.erb new file mode 100644 index 000000000..39ed635ba --- /dev/null +++ b/app/views/debates/_comments.html.erb @@ -0,0 +1,30 @@ +<% cache [locale_and_user_status, @all_visible_comments.map(&:cache_key), @debate.comments_count, @comment_flags.to_a] do %> +
+
+
+ +

+ <%= t("debates.show.comments_title") %> + (<%= @debate.comments_count %>) +

+ + <% if user_signed_in? %> + <%= render 'comments/form', {commentable: @debate, parent_id: nil, toggeable: false} %> + <% else %> +
+ +
+ <%= t("debates.show.login_to_comment", + signin: link_to(t("votes.signin"), new_user_session_path), + signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %> +
+ <% end %> + + <% @root_comments.each do |comment| %> + <%= render 'comments/comment', comment: comment %> + <% end %> + <%= paginate @root_comments %> +
+
+
+<% end %> \ No newline at end of file