adding counter_cache to comments children and WIPing on the issue

This commit is contained in:
David Gil
2015-08-11 18:43:53 +02:00
parent e880cb4f1a
commit 19ef481917
10 changed files with 66 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
class Comment < ActiveRecord::Base class Comment < ActiveRecord::Base
acts_as_nested_set scope: [:commentable_id, :commentable_type] acts_as_nested_set scope: [:commentable_id, :commentable_type], counter_cache: :children_count
acts_as_votable acts_as_votable
validates :body, presence: true validates :body, presence: true

View File

@@ -14,11 +14,12 @@
</span> </span>
<p class="reply"> <p class="reply">
númerototal respuestas <%= t("debates.debate.responses", count: comment.children_count) %>
<% if user_signed_in? %> <% if user_signed_in? %>
&nbsp;|&nbsp; &nbsp;|&nbsp;
<%= render 'comments/form', {parent: comment, toggeable: true} %></p> <%= render 'comments/form', {parent: comment, toggeable: true} %>
<% end %> <% end %>
</p>
</div> </div>
<div class="comment-children"> <div class="comment-children">

View File

@@ -9,7 +9,7 @@
<h3><%= link_to debate.title, debate %></h3> <h3><%= link_to debate.title, debate %></h3>
<p class="debate-info"> <p class="debate-info">
<i class="icon-chat-bubble-two"></i>&nbsp; <i class="icon-chat-bubble-two"></i>&nbsp;
<%= link_to pluralize(debate.comment_threads.count, t("debates.debate.comment"), t("debates.debate.comments")), debate_path(debate, anchor: "comments") %> <%= link_to t("debates.debate.comments", count: debate.comment_threads.count), debate_path(debate, anchor: "comments") %>
</p> </p>
<div class="debate-description"> <div class="debate-description">
<%= link_to debate.description, debate %> <%= link_to debate.description, debate %>

View File

@@ -6,7 +6,13 @@
<div class="debate-info"> <div class="debate-info">
<%= image_tag('user_default.jpg', class: 'author-photo', size: '32x32') %> <%= image_tag('user_default.jpg', class: 'author-photo', size: '32x32') %>
<span class="author"><%= @debate.author.name %></span><span class="bullet">&nbsp;&bullet;&nbsp;</span> <%= l @debate.created_at.to_date %> <span class="bullet">&nbsp;&bullet;&nbsp;</span><i class="icon-chat-bubble-two"></i>&nbsp;<%= link_to pluralize(@debate.comment_threads.count, t("debates.show.comment"), t("debates.show.comments")), "#comments" %> <span class="author">
<%= @debate.author.name %>
</span>
<span class="bullet">&nbsp;&bullet;&nbsp;</span>
<%= l @debate.created_at.to_date %>
<span class="bullet">&nbsp;&bullet;&nbsp;</span>
<i class="icon-chat-bubble-two"></i>&nbsp;<%= link_to t("debates.show.comments", count: @debate.comment_threads.count), "#comments" %>
</div> </div>
<%= @debate.description %> <%= @debate.description %>
@@ -36,7 +42,7 @@
<section class="row-full comments"> <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><%= t("debates.show.comments") %></h2> <h2><%= t("debates.show.comments_title") %></h2>
<% if user_signed_in? %> <% if user_signed_in? %>
<div> <div>
<%= t("debates.show.leave_comment") %> <%= t("debates.show.leave_comment") %>

View File

@@ -8,7 +8,7 @@
<h3><%= link_to featured_debate.title, featured_debate %></h3> <h3><%= link_to featured_debate.title, featured_debate %></h3>
<p class="debate-info"> <p class="debate-info">
<i class="icon-chat-bubble-two"></i>&nbsp; <i class="icon-chat-bubble-two"></i>&nbsp;
<%= link_to pluralize(featured_debate.comment_threads.count, t("debates.show.comment"), t("debates.show.comments")), debate_path(featured_debate, anchor: "comments") %> <%= link_to t("debates.show.comments", count: featured_debate.comment_threads.count), debate_path(featured_debate, anchor: "comments") %>
</p> </p>
<div class="debate-description"> <div class="debate-description">
<%= link_to featured_debate.description, featured_debate %> <%= link_to featured_debate.description, featured_debate %>

View File

@@ -21,8 +21,14 @@ en:
debate: Debate debate: Debate
vote: vote vote: vote
votes: votes votes: votes
comment: Comment comments:
comments: Comments zero: No comments
one: 1 Comment
other: "%{count} Comments"
responses:
zero: No Responses
one: 1 Response
other: "%{count} Responses"
form: form:
error: error error: error
errors: errors errors: errors
@@ -36,8 +42,11 @@ en:
accept_terms: I accept the privacy policy and the legal terms accept_terms: I accept the privacy policy and the legal terms
show: show:
back_link: Back back_link: Back
comment: Comment comments_title: Comments
comments: Comments comments:
zero: No comments
one: 1 Comment
other: "%{count} Comments"
leave_comment: Write a comment leave_comment: Write a comment
login_to_comment: Log in to participate login_to_comment: Log in to participate
edit_debate_link: Edit edit_debate_link: Edit

View File

@@ -21,8 +21,14 @@ es:
debate: Debate debate: Debate
vote: voto vote: voto
votes: votos votes: votos
comment: Comentario comments:
comments: Comentarios zero: Sin comentarios
one: 1 Comentario
other: "%{count} Comentarios"
responses:
zero: Sin respuestas
one: 1 Respuesta
other: "%{count} Respuestas"
form: form:
error: error error: error
errors: errores errors: errores
@@ -36,8 +42,11 @@ es:
accept_terms: Acepto la política de privacidad y el aviso legal accept_terms: Acepto la política de privacidad y el aviso legal
show: show:
back_link: Volver back_link: Volver
comment: Comentario comments_title: Comentarios
comments: Comentarios comments:
zero: Sin comentarios
one: 1 Comentario
other: "%{count} Comentarios"
leave_comment: Deja tu comentario leave_comment: Deja tu comentario
login_to_comment: Entra para participar login_to_comment: Entra para participar
edit_debate_link: Editar edit_debate_link: Editar
@@ -108,5 +117,3 @@ es:
all: "No tienes permiso para borrar %{subject}" all: "No tienes permiso para borrar %{subject}"
manage: manage:
all: "No tienes permiso para realizar la acción '%{action}' sobre %{subject}." all: "No tienes permiso para realizar la acción '%{action}' sobre %{subject}."

View File

@@ -0,0 +1,13 @@
class AddChildrenCountToComments < ActiveRecord::Migration
def up
add_column :comments, :children_count, :integer, default: 0
Comment.find_each do |comment|
Comment.reset_counters(comment.id, :children)
end
end
def down
remove_column :comments, :children_count
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150808141306) do ActiveRecord::Schema.define(version: 20150811161459) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -34,6 +34,7 @@ ActiveRecord::Schema.define(version: 20150808141306) do
t.integer "rgt" t.integer "rgt"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.integer "children_count", default: 0
end end
add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type", using: :btree add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type", using: :btree

View File

@@ -10,6 +10,7 @@ feature 'Comments' do
visit debate_path(debate) visit debate_path(debate)
expect(page).to have_css('.comment', count: 3) expect(page).to have_css('.comment', count: 3)
expect(page).to have_content I18n.t('debates.debate.comments', count: 3)
comment = Comment.first comment = Comment.first
within first('.comment') do within first('.comment') do
@@ -84,6 +85,11 @@ feature 'Comments' do
visit debate_path(debate) visit debate_path(debate)
expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment") expect(page).to have_css(".comment.comment.comment.comment.comment.comment.comment.comment")
expect(page).to have_content I18n.t('debates.debate.comments', count: 8)
within first('.comment') do
expect(page).to have_content I18n.t('debates.comment.responses', count: 7)
end
end end
end end