Skip to content
Browse files

adding comment word count to dashboard

  • Loading branch information...
1 parent ce68156 commit 5e6e9671f369dbd6ee2d5f77c9fcdb41769a812e @steveklabnik steveklabnik committed Jan 9, 2012
View
1 app/controllers/dashboard_controller.rb
@@ -6,5 +6,6 @@ def show
@comments = Comment.for_dashboard
@comment_count = Comment.count
+ @comment_word_count = Comment.total_word_count
end
end
View
8 app/models/comment.rb
@@ -6,4 +6,12 @@ class Comment < ActiveRecord::Base
def self.for_dashboard
order('created_at DESC').limit(5).all
end
+
+ def word_count
+ body.split.count
+ end
+
+ def self.total_word_count
+ all.inject(0) {|total, a| total += a.word_count }
+ end
end
View
1 app/views/dashboard/show.html.erb
@@ -11,6 +11,7 @@
<h2>Comments:</h2>
<p>Total comments: <%= @comment_count %></p>
+<p>Total words: <%= @comment_word_count %></p>
<ul>
<% @comments.each do |comment| %>
<li class="comment"><%= comment.body %></li>
View
6 spec/controllers/dashboard_controller_spec.rb
@@ -33,5 +33,11 @@
get :show
assigns(:article_word_count).should eq(150)
end
+
+ it "assigns @comment_word_count" do
+ Comment.should_receive(:total_word_count).and_return(150)
+ get :show
+ assigns(:comment_word_count).should eq(150)
+ end
end
end
View
16 spec/models/comment_spec.rb
@@ -34,4 +34,20 @@
comments.should eq(expected)
end
end
+
+ context ".total_word_count" do
+ it "gives the word count for all comments" do
+ 2.times { Fabricate(:comment) }
+
+ count = Comment.all.inject(0) {|total, a| total += a.word_count }
+
+ Comment.total_word_count.should eq(count)
+ end
+ end
+
+ context "#word_count" do
+ it "gives the total number of words" do
+ @comment.word_count.should eq(@comment.body.split.count)
+ end
+ end
end
View
9 spec/requests/dashboard_spec.rb
@@ -76,7 +76,14 @@
page.should have_content("Total words: #{total_words}")
end
- it "displays the total words of all comments"
+ it "displays the total words of all comments" do
+ Fabricate(:comment)
+ total_words = Comment.total_word_count
+
+ visit "/"
+
+ page.should have_content("Total words: #{total_words}")
+ end
it "displays the time since the most recent article"
it "displays the time since the most recent comment"
it "displays the most active (most comments) article"

0 comments on commit 5e6e967

Please sign in to comment.
Something went wrong with that request. Please try again.