diff --git a/app/models/comment.rb b/app/models/comment.rb index d4ceb9e3e..913dd0780 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 4e375c540..9707c6462 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -14,11 +14,12 @@

- númerototal respuestas - <% if user_signed_in? %> -  |  - <%= render 'comments/form', {parent: comment, toggeable: true} %>

- <% end %> + <%= t("debates.debate.responses", count: comment.children_count) %> + <% if user_signed_in? %> +  |  + <%= render 'comments/form', {parent: comment, toggeable: true} %> + <% end %> +

diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index 4620e1232..f8343e8be 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -9,7 +9,7 @@

<%= link_to debate.title, debate %>

  - <%= 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") %>

<%= link_to debate.description, debate %> diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 28fefd085..63c2528cd 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -6,7 +6,13 @@
<%= image_tag('user_default.jpg', class: 'author-photo', size: '32x32') %> - <%= @debate.author.name %> •  <%= l @debate.created_at.to_date %>  •  <%= link_to pluralize(@debate.comment_threads.count, t("debates.show.comment"), t("debates.show.comments")), "#comments" %> + + <%= @debate.author.name %> + +  •  + <%= l @debate.created_at.to_date %> +  •  +  <%= link_to t("debates.show.comments", count: @debate.comment_threads.count), "#comments" %>
<%= @debate.description %> @@ -36,7 +42,7 @@
-

<%= t("debates.show.comments") %>

+

<%= t("debates.show.comments_title") %>

<% if user_signed_in? %>
<%= t("debates.show.leave_comment") %> diff --git a/app/views/welcome/_featured_debate.html.erb b/app/views/welcome/_featured_debate.html.erb index fc175fff8..93626b4c7 100644 --- a/app/views/welcome/_featured_debate.html.erb +++ b/app/views/welcome/_featured_debate.html.erb @@ -8,7 +8,7 @@

<%= link_to featured_debate.title, featured_debate %>

  - <%= 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") %>

<%= link_to featured_debate.description, featured_debate %> @@ -25,4 +25,4 @@
-
\ No newline at end of file +
diff --git a/config/locales/en.yml b/config/locales/en.yml index b036f8125..930049b7e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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 diff --git a/config/locales/es.yml b/config/locales/es.yml index 7995483d4..008c8ce2d 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -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}." - - diff --git a/db/migrate/20150811161459_add_children_count_to_comments.rb b/db/migrate/20150811161459_add_children_count_to_comments.rb new file mode 100644 index 000000000..74c288ff5 --- /dev/null +++ b/db/migrate/20150811161459_add_children_count_to_comments.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 7dd74d450..8fb58776c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index 0cc64bce1..7025ae57e 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -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