Merge pull request #448 from AyuntamientoMadrid/caches

Cache for comments
This commit is contained in:
Raimond Garcia
2015-09-09 17:01:11 +02:00
8 changed files with 148 additions and 134 deletions

View File

@@ -22,4 +22,9 @@ module CacheKeysHelper
user_status user_status
end 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 end

View File

@@ -2,7 +2,7 @@ class Comment < ActiveRecord::Base
acts_as_paranoid column: :hidden_at acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases include ActsAsParanoidAliases
acts_as_votable acts_as_votable
has_ancestry has_ancestry touch: true
attr_accessor :as_moderator, :as_administrator attr_accessor :as_moderator, :as_administrator

View File

@@ -1,4 +1,5 @@
<div class="row"> <% cache [locale_and_user_status(comment), comment, commentable_cache_key(@commentable), comment.author, (@comment_flags[comment.id] if @comment_flags)] do %>
<div class="row">
<div id="<%= dom_id(comment) %>" class="comment small-12 column"> <div id="<%= dom_id(comment) %>" class="comment small-12 column">
<% if comment.hidden? || comment.user.hidden? %> <% if comment.hidden? || comment.user.hidden? %>
@@ -58,7 +59,7 @@
<% end %> <% end %>
&nbsp;&bullet;&nbsp;<%= time_ago_in_words(comment.created_at) %> &nbsp;&bullet;&nbsp;<time><%= l comment.created_at.to_datetime, format: :datetime %></time>
</div> </div>
<% if comment.as_administrator? %> <% if comment.as_administrator? %>
@@ -98,5 +99,6 @@
<%= render 'comments/comment', comment: child %> <%= render 'comments/comment', comment: child %>
<% end %> <% end %>
</div> </div>
</div> </div>
</div> </div>
<% end %>

View File

@@ -1,5 +1,6 @@
<% css_id = parent_or_commentable_dom_id(parent_id, commentable) %> <% cache [locale_and_user_status, parent_id, commentable_cache_key(commentable)] do %>
<div id="js-comment-form-<%= css_id %>" <%= "style='display:none'".html_safe if toggeable %>> <% css_id = parent_or_commentable_dom_id(parent_id, commentable) %>
<div id="js-comment-form-<%= css_id %>" <%= "style='display:none'".html_safe if toggeable %>>
<%= form_for [commentable, Comment.new], remote: true do |f| %> <%= form_for [commentable, Comment.new], remote: true do |f| %>
<%= label_tag "comment-body-#{css_id}", t("comments.form.leave_comment") %> <%= label_tag "comment-body-#{css_id}", t("comments.form.leave_comment") %>
<%= f.text_area :body, id: "comment-body-#{css_id}", label: false %> <%= f.text_area :body, id: "comment-body-#{css_id}", label: false %>
@@ -23,4 +24,5 @@
<% end %> <% end %>
<% end %> <% end %>
</div> </div>
<% end %>

View File

@@ -1,7 +1,7 @@
<section class="row-full comments"> <% cache [locale_and_user_status, commentable_cache_key(@debate), @all_visible_comments, @all_visible_comments.map(&:author), @debate.comments_count, @comment_flags] do %>
<section class="row-full comments">
<div class="row"> <div class="row">
<div id="comments" class="small-12 column"> <div id="comments" class="small-12 column">
<h2> <h2>
<%= t("debates.show.comments_title") %> <%= t("debates.show.comments_title") %>
<span>(<%= @debate.comments_count %>)</span> <span>(<%= @debate.comments_count %>)</span>
@@ -25,4 +25,5 @@
<%= paginate @root_comments %> <%= paginate @root_comments %>
</div> </div>
</div> </div>
</section> </section>
<% end %>

View File

@@ -20,3 +20,6 @@
# available at http://guides.rubyonrails.org/i18n.html. # available at http://guides.rubyonrails.org/i18n.html.
en: en:
time:
formats:
datetime: "%Y-%m-%d %H:%M:%S"

View File

@@ -196,4 +196,5 @@ es:
default: "%A, %d de %B de %Y %H:%M:%S %z" default: "%A, %d de %B de %Y %H:%M:%S %z"
long: "%d de %B de %Y %H:%M" long: "%d de %B de %Y %H:%M"
short: "%d de %b %H:%M" short: "%d de %b %H:%M"
datetime: "%d/%m/%Y %H:%M:%S"
pm: pm pm: pm

View File

@@ -14,7 +14,7 @@ feature 'Comments' do
comment = Comment.last comment = Comment.last
within first('.comment') do within first('.comment') do
expect(page).to have_content comment.user.name 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 expect(page).to have_content comment.body
end end
end end