diff --git a/app/helpers/cache_keys_helper.rb b/app/helpers/cache_keys_helper.rb index 6bcd8ca91..be23439dd 100644 --- a/app/helpers/cache_keys_helper.rb +++ b/app/helpers/cache_keys_helper.rb @@ -22,4 +22,9 @@ module CacheKeysHelper user_status end + # when commentable id and type are used but no need to update cache on updated_at changes + def commentable_cache_key(commentable) + "#{commentable.class.name}-#{commentable.id}" + end + end \ No newline at end of file diff --git a/app/models/comment.rb b/app/models/comment.rb index efa9ec7a0..233736824 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -2,7 +2,7 @@ class Comment < ActiveRecord::Base acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases acts_as_votable - has_ancestry + has_ancestry touch: true attr_accessor :as_moderator, :as_administrator diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index d29c19ebe..0ac6b9be1 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,102 +1,104 @@ -
-
+<% cache [locale_and_user_status(comment), comment, commentable_cache_key(@commentable), comment.author, (@comment_flags[comment.id] if @comment_flags)] 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 %> +  •  +
+ + <% 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 %> +
+ <% select_children(@comments, comment).each do |child| %> + <%= render 'comments/comment', comment: child %> <% 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 %> -
- <% select_children(@comments, comment).each do |child| %> - <%= render 'comments/comment', comment: child %> - <% end %> +
-
-
\ No newline at end of file +<% end %> \ No newline at end of file diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb index 5c9f4eab2..3704b2fa1 100644 --- a/app/views/comments/_form.html.erb +++ b/app/views/comments/_form.html.erb @@ -1,26 +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, parent_id, commentable_cache_key(commentable)] do %> + <% 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 %> - <%= 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 %> diff --git a/app/views/debates/_comments.html.erb b/app/views/debates/_comments.html.erb index 5fbea960d..2a2fb518e 100644 --- a/app/views/debates/_comments.html.erb +++ b/app/views/debates/_comments.html.erb @@ -1,28 +1,29 @@ -
-
-
+<% cache [locale_and_user_status, commentable_cache_key(@debate), @all_visible_comments, @all_visible_comments.map(&:author), @debate.comments_count, @comment_flags] do %> +
+
+
+

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

-

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

+ <% if user_signed_in? %> + <%= render 'comments/form', {commentable: @debate, parent_id: nil, toggeable: false} %> + <% else %> +
- <% 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 %> -
- <%= 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 %> + <% @root_comments.each do |comment| %> + <%= render 'comments/comment', comment: comment %> + <% end %> + <%= paginate @root_comments %> +
-
-
\ No newline at end of file + +<% end %> \ No newline at end of file diff --git a/config/locales/rails.en.yml b/config/locales/rails.en.yml index cb5bdc38f..e45dfce7e 100644 --- a/config/locales/rails.en.yml +++ b/config/locales/rails.en.yml @@ -20,3 +20,6 @@ # available at http://guides.rubyonrails.org/i18n.html. en: + time: + formats: + datetime: "%Y-%m-%d %H:%M:%S" diff --git a/config/locales/rails.es.yml b/config/locales/rails.es.yml index 9a6e698d2..31cc02e92 100644 --- a/config/locales/rails.es.yml +++ b/config/locales/rails.es.yml @@ -196,4 +196,5 @@ es: default: "%A, %d de %B de %Y %H:%M:%S %z" long: "%d de %B de %Y %H:%M" short: "%d de %b %H:%M" + datetime: "%d/%m/%Y %H:%M:%S" pm: pm \ No newline at end of file diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index 2bdd380bd..40e75a281 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -14,7 +14,7 @@ feature 'Comments' do comment = Comment.last within first('.comment') do expect(page).to have_content comment.user.name - expect(page).to have_content time_ago_in_words(comment.created_at) + expect(page).to have_content I18n.l(comment.created_at, format: :datetime) expect(page).to have_content comment.body end end