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
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
validates :body, presence: true

View File

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

View File

@@ -9,7 +9,7 @@
<h3><%= link_to debate.title, debate %></h3>
<p class="debate-info">
<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>
<div class="debate-description">
<%= link_to debate.description, debate %>

View File

@@ -6,7 +6,13 @@
<div class="debate-info">
<%= 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>
<%= @debate.description %>
@@ -36,7 +42,7 @@
<section class="row-full comments">
<div class="row">
<div id="comments" class="small-12 column">
<h2><%= t("debates.show.comments") %></h2>
<h2><%= t("debates.show.comments_title") %></h2>
<% if user_signed_in? %>
<div>
<%= t("debates.show.leave_comment") %>

View File

@@ -8,7 +8,7 @@
<h3><%= link_to featured_debate.title, featured_debate %></h3>
<p class="debate-info">
<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>
<div class="debate-description">
<%= link_to featured_debate.description, featured_debate %>

View File

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

View File

@@ -21,8 +21,14 @@ es:
debate: Debate
vote: voto
votes: votos
comment: Comentario
comments: Comentarios
comments:
zero: Sin comentarios
one: 1 Comentario
other: "%{count} Comentarios"
responses:
zero: Sin respuestas
one: 1 Respuesta
other: "%{count} Respuestas"
form:
error: error
errors: errores
@@ -36,8 +42,11 @@ es:
accept_terms: Acepto la política de privacidad y el aviso legal
show:
back_link: Volver
comment: Comentario
comments: Comentarios
comments_title: Comentarios
comments:
zero: Sin comentarios
one: 1 Comentario
other: "%{count} Comentarios"
leave_comment: Deja tu comentario
login_to_comment: Entra para participar
edit_debate_link: Editar
@@ -108,5 +117,3 @@ es:
all: "No tienes permiso para borrar %{subject}"
manage:
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.
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
enable_extension "plpgsql"
@@ -28,12 +28,13 @@ ActiveRecord::Schema.define(version: 20150808141306) do
t.string "title"
t.text "body"
t.string "subject"
t.integer "user_id", null: false
t.integer "user_id", null: false
t.integer "parent_id"
t.integer "lft"
t.integer "rgt"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "children_count", default: 0
end
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)
expect(page).to have_css('.comment', count: 3)
expect(page).to have_content I18n.t('debates.debate.comments', count: 3)
comment = Comment.first
within first('.comment') do
@@ -84,6 +85,11 @@ feature 'Comments' do
visit debate_path(debate)
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