From 916630712060ed9f07e3a534f3414d38b96d5c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 9 Sep 2015 14:45:19 +0200 Subject: [PATCH 1/7] adds cache key for commendable without updated_at --- app/helpers/cache_keys_helper.rb | 5 +++++ 1 file changed, 5 insertions(+) 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 From 92bff4b4c36db013b1aa12ba001576ec31f44f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 9 Sep 2015 14:46:09 +0200 Subject: [PATCH 2/7] adds :touch to ancestry now nested comments caches invalidates automagically --- app/models/comment.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From e4e1c9039f7150140da954eceb6109572bb1ce98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 9 Sep 2015 14:54:01 +0200 Subject: [PATCH 3/7] cache for comment/_form uses info for commentable but without need for invalidation after commentable updates --- app/views/comments/_form.html.erb | 48 ++++++++++++++++--------------- 1 file changed, 25 insertions(+), 23 deletions(-) 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 %> From e24e9e7be8e38065baddeda931a485361ec7d997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 9 Sep 2015 14:56:15 +0200 Subject: [PATCH 4/7] cache for _comment partial not need to invalidates on commentable updates of comment_count because now ancestry touches parent comments so if there is children comment invalidation is automatic --- app/views/comments/_comment.html.erb | 172 ++++++++++++++------------- 1 file changed, 87 insertions(+), 85 deletions(-) diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index d29c19ebe..d8856f315 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 %> +  • <%= 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 %> +
+ <% 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 From 811c329199917a5e41c596baff5fdaabd9d2510b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 9 Sep 2015 14:57:27 +0200 Subject: [PATCH 5/7] changes how comment.created_at is shown time_ago_in_words is not cache friendly --- app/views/comments/_comment.html.erb | 2 +- config/locales/rails.en.yml | 3 +++ config/locales/rails.es.yml | 1 + spec/features/comments_spec.rb | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index d8856f315..3b0ad67e0 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -59,7 +59,7 @@ <% end %> -  • <%= time_ago_in_words(comment.created_at) %> +  • <%= l comment.created_at.to_datetime, format: :datetime %>
<% if comment.as_administrator? %> 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 From 30bf5023b3508a2e82981f7fc29f6b166d711659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 9 Sep 2015 15:00:18 +0200 Subject: [PATCH 6/7] adds cache to debates/_comments partial cache does not invalidate on updates of debate other than comments_count --- app/views/debates/_comments.html.erb | 49 ++++++++++++++-------------- 1 file changed, 25 insertions(+), 24 deletions(-) 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 From cda54b005062a3471092b1bc0bd10f805e5b8366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 9 Sep 2015 16:53:17 +0200 Subject: [PATCH 7/7] adds time tag to comment --- app/views/comments/_comment.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 3b0ad67e0..0ac6b9be1 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -59,7 +59,7 @@ <% end %> -  • <%= l comment.created_at.to_datetime, format: :datetime %> +  • 
<% if comment.as_administrator? %>