From 19ef4819174be93fa6b2f8c72dd0173d47dbd211 Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 11 Aug 2015 18:43:53 +0200 Subject: [PATCH 01/11] adding counter_cache to comments children and WIPing on the issue --- app/models/comment.rb | 2 +- app/views/comments/_comment.html.erb | 11 ++++++----- app/views/debates/_debate.html.erb | 2 +- app/views/debates/show.html.erb | 10 ++++++++-- app/views/welcome/_featured_debate.html.erb | 4 ++-- config/locales/en.yml | 17 +++++++++++++---- config/locales/es.yml | 19 +++++++++++++------ ...11161459_add_children_count_to_comments.rb | 13 +++++++++++++ db/schema.rb | 5 +++-- spec/features/comments_spec.rb | 6 ++++++ 10 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 db/migrate/20150811161459_add_children_count_to_comments.rb 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 From 0291f574a2ad71c50d49af571bc2ce41c78c332c Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 11 Aug 2015 19:31:52 +0200 Subject: [PATCH 02/11] add vote counters to comments --- app/models/comment.rb | 3 +++ app/views/comments/_comment.html.erb | 2 +- app/views/comments/_votes.html.erb | 5 +++-- app/views/debates/_votes.html.erb | 2 +- config/locales/en.yml | 12 ++++++++++-- config/locales/es.yml | 12 ++++++++++-- 6 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 913dd0780..3508d5593 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -28,4 +28,7 @@ class Comment < ActiveRecord::Base user end + def total_votes + votes_for.size + end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 9707c6462..466752044 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -14,7 +14,7 @@

- <%= t("debates.debate.responses", count: comment.children_count) %> + <%= t("debates.comment.responses", count: comment.children_count) %> <% if user_signed_in? %>  |  <%= render 'comments/form', {parent: comment, toggeable: true} %> diff --git a/app/views/comments/_votes.html.erb b/app/views/comments/_votes.html.erb index 4763388b3..73efa56ba 100644 --- a/app/views/comments/_votes.html.erb +++ b/app/views/comments/_votes.html.erb @@ -1,5 +1,6 @@ - númerototal votos + + <%= t('debates.comment.votes', count: comment.total_votes) %>  |  @@ -16,4 +17,4 @@ <% end %> <%= comment.get_dislikes.size %> - \ No newline at end of file + diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb index a15a66a4d..4c252c8b5 100644 --- a/app/views/debates/_votes.html.erb +++ b/app/views/debates/_votes.html.erb @@ -18,7 +18,7 @@

- <%= pluralize(debate.total_votes, t("debates.debate.vote"), t("debates.debate.votes")) %> + <%= t("debates.debate.votes", count: debate.total_votes) %> <% unless user_signed_in? %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 930049b7e..1b64977ff 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -19,16 +19,24 @@ en: create_debate: Create a debate debate: debate: Debate - vote: vote - votes: votes comments: zero: No comments one: 1 Comment other: "%{count} Comments" + votes: + zero: No votes + one: 1 Vote + other: "%{count} Votes" + comment: responses: zero: No Responses one: 1 Response other: "%{count} Responses" + votes: + zero: No votes + one: 1 Vote + other: "%{count} Votes" + votes_weighted_score: "Total: %{score}" form: error: error errors: errors diff --git a/config/locales/es.yml b/config/locales/es.yml index 008c8ce2d..0b8701f18 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -19,16 +19,24 @@ es: create_debate: Crea un debate debate: debate: Debate - vote: voto - votes: votos comments: zero: Sin comentarios one: 1 Comentario other: "%{count} Comentarios" + votes: + zero: Sin votos + one: 1 Voto + other: "%{count} Votos" + comment: responses: zero: Sin respuestas one: 1 Respuesta other: "%{count} Respuestas" + votes: + zero: Sin votos + one: 1 Voto + other: "%{count} Votos" + votes_weighted_score: "Total: %{score}" form: error: error errors: errores From 575c2dd2812519b20836beedcb0d9493d6293d77 Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 11 Aug 2015 19:39:52 +0200 Subject: [PATCH 03/11] faking children_count counter cache due to possible bug with awesome_nested_set --- app/models/comment.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/comment.rb b/app/models/comment.rb index 3508d5593..cac51a482 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -31,4 +31,11 @@ class Comment < ActiveRecord::Base def total_votes votes_for.size end + + # TODO: faking counter cache since there is a bug with acts_as_nested_set :counter_cache + # Remove when https://github.com/collectiveidea/awesome_nested_set/issues/294 is fixed + # There is a test for this, so you should know if it is actually fixed. + def children_count + children.count + end end From caa0c3eaabf5c4889e4e389462b58ff51bd739f4 Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 11 Aug 2015 19:42:07 +0200 Subject: [PATCH 04/11] change background by before(:each) in tests - background is evil! --- spec/features/account_spec.rb | 2 +- spec/features/votes_spec.rb | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/spec/features/account_spec.rb b/spec/features/account_spec.rb index 59ee23f5b..ded1274fe 100644 --- a/spec/features/account_spec.rb +++ b/spec/features/account_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' feature 'Account' do - background do + before(:each) do @user = create(:user, first_name: "Manuela", last_name:"Colau") end diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb index 8deb20c20..606dc3c74 100644 --- a/spec/features/votes_spec.rb +++ b/spec/features/votes_spec.rb @@ -4,7 +4,7 @@ feature 'Votes' do feature 'Debates' do - background do + before(:each) do @manuela = create(:user) @pablo = create(:user) @debate = create(:debate) @@ -236,13 +236,10 @@ feature 'Votes' do expect(page).to have_content "1 vote" end - end - feature 'Comments' do - - background do + before(:each) do @manuela = create(:user) @pablo = create(:user) @debate = create(:debate) @@ -314,4 +311,4 @@ feature 'Votes' do end end -end \ No newline at end of file +end From 1ee337876847495f87baafa7f4627c7d2ed6a185 Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 11 Aug 2015 19:48:11 +0200 Subject: [PATCH 05/11] adding a few tests --- app/models/comment.rb | 4 ++++ config/locales/en.yml | 8 ++++---- config/locales/es.yml | 8 ++++---- spec/features/comments_spec.rb | 2 +- spec/features/votes_spec.rb | 10 +++++++++- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index cac51a482..f298c67c3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -38,4 +38,8 @@ class Comment < ActiveRecord::Base def children_count children.count end + + def descendants_count + descendants.count + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 1b64977ff..5a60019aa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -25,8 +25,8 @@ en: other: "%{count} Comments" votes: zero: No votes - one: 1 Vote - other: "%{count} Votes" + one: 1 vote + other: "%{count} votes" comment: responses: zero: No Responses @@ -34,8 +34,8 @@ en: other: "%{count} Responses" votes: zero: No votes - one: 1 Vote - other: "%{count} Votes" + one: 1 vote + other: "%{count} votes" votes_weighted_score: "Total: %{score}" form: error: error diff --git a/config/locales/es.yml b/config/locales/es.yml index 0b8701f18..32fd051c3 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -25,8 +25,8 @@ es: other: "%{count} Comentarios" votes: zero: Sin votos - one: 1 Voto - other: "%{count} Votos" + one: 1 voto + other: "%{count} votos" comment: responses: zero: Sin respuestas @@ -34,8 +34,8 @@ es: other: "%{count} Respuestas" votes: zero: Sin votos - one: 1 Voto - other: "%{count} Votos" + one: 1 voto + other: "%{count} votos" votes_weighted_score: "Total: %{score}" form: error: error diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index 7025ae57e..d1763b6cd 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -88,7 +88,7 @@ feature 'Comments' do 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) + expect(page).to have_content I18n.t('debates.comment.responses', count: 1) end end diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb index 606dc3c74..3c7bf6fb4 100644 --- a/spec/features/votes_spec.rb +++ b/spec/features/votes_spec.rb @@ -112,7 +112,7 @@ feature 'Votes' do scenario 'Show no votes' do visit debate_path(@debate) - expect(page).to have_content "0 votes" + expect(page).to have_content "No votes" within('.in-favor') do expect(page).to have_content "0%" @@ -263,6 +263,8 @@ feature 'Votes' do within(".against") do expect(page).to have_content "1" end + + expect(page).to have_content "2 votes" end end @@ -277,6 +279,8 @@ feature 'Votes' do within(".against") do expect(page).to have_content "0" end + + expect(page).to have_content "1 vote" end end @@ -292,6 +296,8 @@ feature 'Votes' do within('.against') do expect(page).to have_content "1" end + + expect(page).to have_content "1 vote" end end @@ -307,6 +313,8 @@ feature 'Votes' do within('.against') do expect(page).to have_content "0" end + + expect(page).to have_content "1 vote" end end From 7c8948639fce8e671297eb53300707f7ae449dc1 Mon Sep 17 00:00:00 2001 From: David Gil Date: Wed, 12 Aug 2015 13:46:02 +0200 Subject: [PATCH 06/11] move model code out of migration --- app/models/comment.rb | 3 ++- db/migrate/20150811161459_add_children_count_to_comments.rb | 4 ---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index f298c67c3..696b363ce 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -34,7 +34,8 @@ class Comment < ActiveRecord::Base # TODO: faking counter cache since there is a bug with acts_as_nested_set :counter_cache # Remove when https://github.com/collectiveidea/awesome_nested_set/issues/294 is fixed - # There is a test for this, so you should know if it is actually fixed. + # and reset counters using + # > Comment.find_each { |comment| Comment.reset_counters(comment.id, :children) } def children_count children.count end diff --git a/db/migrate/20150811161459_add_children_count_to_comments.rb b/db/migrate/20150811161459_add_children_count_to_comments.rb index 74c288ff5..ce233542d 100644 --- a/db/migrate/20150811161459_add_children_count_to_comments.rb +++ b/db/migrate/20150811161459_add_children_count_to_comments.rb @@ -1,10 +1,6 @@ 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 From d134284774b6229d0499f019cfd1efc574a819d2 Mon Sep 17 00:00:00 2001 From: David Gil Date: Wed, 12 Aug 2015 13:57:45 +0200 Subject: [PATCH 07/11] background is the new before(:each) --- spec/features/account_spec.rb | 2 +- spec/features/votes_spec.rb | 4 ++-- spec/models/debate_spec.rb | 6 +++--- spec/models/user_spec.rb | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/features/account_spec.rb b/spec/features/account_spec.rb index ded1274fe..59ee23f5b 100644 --- a/spec/features/account_spec.rb +++ b/spec/features/account_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' feature 'Account' do - before(:each) do + background do @user = create(:user, first_name: "Manuela", last_name:"Colau") end diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb index 3c7bf6fb4..4e713b669 100644 --- a/spec/features/votes_spec.rb +++ b/spec/features/votes_spec.rb @@ -4,7 +4,7 @@ feature 'Votes' do feature 'Debates' do - before(:each) do + background do @manuela = create(:user) @pablo = create(:user) @debate = create(:debate) @@ -239,7 +239,7 @@ feature 'Votes' do end feature 'Comments' do - before(:each) do + background do @manuela = create(:user) @pablo = create(:user) @debate = create(:debate) diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 00f6217bd..c79f4990c 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' describe Debate do - before(:each) do + background do @debate = build(:debate) end @@ -50,7 +50,7 @@ describe Debate do end describe "#editable?" do - before(:each) do + background do @debate = create(:debate) end @@ -67,7 +67,7 @@ describe Debate do end describe "#editable_by?" do - before(:each) do + background do @debate = create(:debate) end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4a8fe8fd7..e6c8f617e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' describe User do describe "#votes_on_debates" do - before(:each) do + background do @user = create(:user) end From 6176e148f02b8f29b0be632d03e16f07379b93e1 Mon Sep 17 00:00:00 2001 From: David Gil Date: Wed, 12 Aug 2015 14:54:54 +0200 Subject: [PATCH 08/11] fix non working migration --- spec/factories.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index fc76685d0..6bbe3b68b 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -22,15 +22,11 @@ FactoryGirl.define do end factory :comment do - commentable + association :commentable, factory: :debate user body 'Comment body' end - factory :commentable do - debate - end - factory :administrator do user end From 9a2ef8366eff92a14d471b5139e93488fe168c79 Mon Sep 17 00:00:00 2001 From: David Gil Date: Wed, 12 Aug 2015 14:56:48 +0200 Subject: [PATCH 09/11] change background by let since model tests do not know about background --- spec/models/debate_spec.rb | 66 +++++++++++++++++--------------------- spec/models/user_spec.rb | 16 ++++----- 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index c79f4990c..187bc0dc1 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -1,88 +1,80 @@ require 'rails_helper' describe Debate do - - background do - @debate = build(:debate) - end + let(:debate) { build(:debate) } it "should be valid" do - expect(@debate).to be_valid + expect(debate).to be_valid end it "should not be valid without an author" do - @debate.author = nil - expect(@debate).to_not be_valid + debate.author = nil + expect(debate).to_not be_valid end it "should not be valid without a title" do - @debate.title = nil - expect(@debate).to_not be_valid + debate.title = nil + expect(debate).to_not be_valid end describe "#description" do it "should be mandatory" do - @debate.description = nil - expect(@debate).to_not be_valid + debate.description = nil + expect(debate).to_not be_valid end it "should be sanitized" do - @debate.description = "" - @debate.valid? - expect(@debate.description).to eq("alert('danger');") + debate.description = "" + debate.valid? + expect(debate.description).to eq("alert('danger');") end it "should be html_safe" do - @debate.description = "" - expect(@debate.description).to be_html_safe + debate.description = "" + expect(debate.description).to be_html_safe end end it "should sanitize the tag list" do - @debate.tag_list = "user_id=1" - @debate.valid? - expect(@debate.tag_list).to eq(['user_id1']) + debate.tag_list = "user_id=1" + debate.valid? + expect(debate.tag_list).to eq(['user_id1']) end it "should not be valid without accepting terms of service" do - @debate.terms_of_service = nil - expect(@debate).to_not be_valid + debate.terms_of_service = nil + expect(debate).to_not be_valid end describe "#editable?" do - background do - @debate = create(:debate) - end + let(:debate) { create(:debate) } it "should be true if debate has no votes yet" do - expect(@debate.total_votes).to eq(0) - expect(@debate.editable?).to be true + expect(debate.total_votes).to eq(0) + expect(debate.editable?).to be true end it "should be false if debate has votes" do - create(:vote, votable: @debate) - expect(@debate.total_votes).to eq(1) - expect(@debate.editable?).to be false + create(:vote, votable: debate) + expect(debate.total_votes).to eq(1) + expect(debate.editable?).to be false end end describe "#editable_by?" do - background do - @debate = create(:debate) - end + let(:debate) { create(:debate) } it "should be true if user is the author and debate is editable" do - expect(@debate.editable_by?(@debate.author)).to be true + expect(debate.editable_by?(debate.author)).to be true end it "should be false if debate is not editable" do - create(:vote, votable: @debate) - expect(@debate.editable_by?(@debate.author)).to be false + create(:vote, votable: debate) + expect(debate.editable_by?(debate.author)).to be false end it "should be false if user is not the author" do - expect(@debate.editable_by?(create(:user))).to be false - + expect(debate.editable_by?(create(:user))).to be false end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e6c8f617e..36444a9c5 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -3,24 +3,22 @@ require 'rails_helper' describe User do describe "#votes_on_debates" do - background do - @user = create(:user) - end + let(:user) { create(:user) } it "returns {} if no debate" do - expect(@user.votes_on_debates()).to eq({}) - expect(@user.votes_on_debates([])).to eq({}) - expect(@user.votes_on_debates([nil, nil])).to eq({}) + expect(user.votes_on_debates()).to eq({}) + expect(user.votes_on_debates([])).to eq({}) + expect(user.votes_on_debates([nil, nil])).to eq({}) end it "returns a hash of debates ids and votes" do debate1 = create(:debate) debate2 = create(:debate) debate3 = create(:debate) - create(:vote, voter: @user, votable: debate1, vote_flag: true) - create(:vote, voter: @user, votable: debate3, vote_flag: false) + create(:vote, voter: user, votable: debate1, vote_flag: true) + create(:vote, voter: user, votable: debate3, vote_flag: false) - voted = @user.votes_on_debates([debate1.id, debate2.id, debate3.id]) + voted = user.votes_on_debates([debate1.id, debate2.id, debate3.id]) expect(voted[debate1.id]).to eq(true) expect(voted[debate2.id]).to eq(nil) From a3550a55a5053246f948cd884cb3f283fa797f3e Mon Sep 17 00:00:00 2001 From: David Gil Date: Wed, 12 Aug 2015 14:57:18 +0200 Subject: [PATCH 10/11] create spec/models/comment_spec and add a few tests for children count --- spec/models/comment_spec.rb | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spec/models/comment_spec.rb diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb new file mode 100644 index 000000000..5af7afc1c --- /dev/null +++ b/spec/models/comment_spec.rb @@ -0,0 +1,41 @@ +require 'rails_helper' + +describe Comment do + + let(:comment) { build(:comment) } + + it "should be valid" do + expect(comment).to be_valid + end + + describe "#children_count" do + let(:comment) { create(:comment) } + let(:debate) { comment.debate } + + it "should count first level children" do + parent = comment + + 3.times do + create(:comment, commentable: debate). + move_to_child_of(parent) + parent = parent.children.first + end + + expect(comment.children_count).to eq(1) + expect(debate.comment_threads.count).to eq(4) + end + + it "should increase children count" do + expect do + create(:comment, commentable: debate). + move_to_child_of(comment) + end.to change { comment.children_count }.from(0).to(1) + end + + it "should decrease children count" do + new_comment = create(:comment, commentable: debate).move_to_child_of(comment) + + expect { new_comment.destroy }.to change { comment.children_count }.from(1).to(0) + end + end +end From d70d417c8753e726bb71177788a65c759f513170 Mon Sep 17 00:00:00 2001 From: David Gil Date: Wed, 12 Aug 2015 15:02:59 +0200 Subject: [PATCH 11/11] using explicit texts instead of I18n calls in tests --- spec/features/comments_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index d1763b6cd..5417bb48a 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -10,7 +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) + expect(page).to have_content '3 Comments' comment = Comment.first within first('.comment') do @@ -85,10 +85,10 @@ 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) + expect(page).to have_content '8 Comments' within first('.comment') do - expect(page).to have_content I18n.t('debates.comment.responses', count: 1) + expect(page).to have_content '1 Response' end end