Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:JumpstartLab/jsblogger_advanced

  • Loading branch information...
commit 504c3f2243e86b25fcc18f251575285e0307453b 2 parents 3a909dd + fccf20d
@jcasimir jcasimir authored
View
2  .gitignore
@@ -13,3 +13,5 @@
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
+
+*.swp
View
6 app/controllers/dashboard_controller.rb
@@ -1,5 +1,11 @@
class DashboardController < ApplicationController
def show
+ @articles = Article.for_dashboard
+ @article_count = Article.count
+ @article_word_count = Article.total_word_count
+ @comments = Comment.for_dashboard
+ @comment_count = Comment.count
+ @comment_word_count = Comment.total_word_count
end
end
View
12 app/models/article.rb
@@ -36,4 +36,16 @@ def self.search(params)
[tag.articles, tag]
end
end
+
+ 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
12 app/models/comment.rb
@@ -2,4 +2,16 @@ class Comment < ActiveRecord::Base
belongs_to :article
validates :article_id, :presence => true
+
+ 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
20 app/views/dashboard/show.html.erb
@@ -1 +1,19 @@
-<h1>Dashboard</h1>
+<h1>Dashboard</h1>
+
+<h2>Articles:</h2>
+<p>Total articles: <%= @article_count %></p>
+<p>Total words: <%= @article_word_count %></p>
+<ul>
+ <% @articles.each do |article| %>
+ <li class="article"><%= article.title %></li>
+ <% end %>
+</ul>
+
+<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>
+ <% end %>
+</ul>
View
38 spec/controllers/dashboard_controller_spec.rb
@@ -1,5 +1,43 @@
require 'spec_helper'
describe DashboardController do
+ describe "show" do
+ it "assigns @articles" do
+ articles = stub
+ Article.should_receive(:for_dashboard).and_return(articles)
+ get :show
+ assigns(:articles).should eq(articles)
+ end
+ it "assigns @comments" do
+ comments = stub
+ Comment.should_receive(:for_dashboard).and_return(comments)
+ get :show
+ assigns(:comments).should eq(comments)
+ end
+
+ it "assigns @article_count" do
+ Article.should_receive(:count).and_return(4)
+ get :show
+ assigns(:article_count).should eq(4)
+ end
+
+ it "assigns @comment_count" do
+ Comment.should_receive(:count).and_return(4)
+ get :show
+ assigns(:comment_count).should eq(4)
+ end
+
+ it "assigns @article_word_count" do
+ Article.should_receive(:total_word_count).and_return(150)
+ 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
3  spec/fabricators/comment_fabricator.rb
@@ -1,4 +1,5 @@
Fabricator(:comment) do
author_name { Faker::Name.name }
body { Faker::Lorem.paragraph }
-end
+ article!
+end
View
15 spec/helpers/dashboard_helper_spec.rb
@@ -1,15 +0,0 @@
-require 'spec_helper'
-
-# Specs in this file have access to a helper object that includes
-# the DashboardHelper. For example:
-#
-# describe DashboardHelper do
-# describe "string concat" do
-# it "concats two strings with spaces" do
-# helper.concat_strings("this","that").should == "this that"
-# end
-# end
-# end
-describe DashboardHelper do
- pending "add some examples to (or delete) #{__FILE__}"
-end
View
32 spec/integration/dashboard_spec.rb
@@ -1,32 +0,0 @@
-
-
-describe "front dashboard" do
-
- it "loads from the root path" do
- visit "/"
- page.should have_content("Dashboard")
- end
-
- context "recent articles" do
- it "lists the five most recent articles"
-
- it "lists them in reverse chronological order"
-
- end
-
- context "recent comments" do
- it "lists the five most recent comments"
-
- it "lists them in reverse order"
- end
-
- context "statistics" do
- it "lists the total number of articles"
- it "lists the total number of comments"
- it "displays the total words of all articles"
- it "displays the total words of all comments"
- 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"
- end
-end
View
28 spec/models/articles_spec.rb
@@ -34,4 +34,32 @@
it "responds to .valid_ids with a set of all current article IDs" do
Article.valid_ids.should == Article.select(:id).collect{|a| a.id}
end
+
+ context ".for_dashboard" do
+ it "gives the most recent 5 articles" do
+ 6.times do
+ Fabricate(:article)
+ end
+ expected = Article.order('created_at DESC').limit(5).all
+
+ articles = Article.for_dashboard
+ articles.count.should eq(5)
+ articles.should eq(expected)
+ end
+ end
+
+ context ".total_word_count" do
+ it "gives the word count for all articles" do
+ 2.times { Fabricate(:article, :body => "I think that...") }
+
+ Article.total_word_count.should eq(6)
+ end
+ end
+
+ context "#word_count" do
+ it "gives the total number of words" do
+ article = Fabricate(:article, :body => "Four score and seven years ago...")
+ article.word_count.should eq(6)
+ end
+ end
end
View
43 spec/models/comment_spec.rb
@@ -1,25 +1,46 @@
require 'spec_helper'
describe Comment do
- before(:each) do
- @article = Article.create(:title => "Hello, World",
- :body => "Sample Body.")
- @comment = @article.comments.create(:author_name => "Daffy Duck",
- :body => "My comment goes here.")
- end
+ let(:comment) { Fabricate(:comment) }
it "should be valid with valid attributes" do
- @article.should be_valid
- @comment.should be_valid
+ comment.should be_valid
end
it "should respond to article" do
- @comment.should respond_to(:article)
+ comment.should respond_to(:article)
end
it "must have a value in article_id" do
- @comment.article_id = nil
- @comment.should_not be_valid
+ comment.article_id = nil
+ comment.should_not be_valid
+ end
+
+ context ".for_dashboard" do
+ it "gives the most recent 5 comments" do
+ 6.times do
+ Fabricate(:comment)
+ end
+ expected = Comment.order('created_at DESC').limit(5).all
+
+ comments = Comment.for_dashboard
+ comments.count.should eq(5)
+ comments.should eq(expected)
+ end
end
+ context ".total_word_count" do
+ it "gives the word count for all comments" do
+ 2.times { Fabricate(:comment, :body => "I think that...") }
+
+ Comment.total_word_count.should eq(6)
+ end
+ end
+
+ context "#word_count" do
+ it "gives the total number of words" do
+ comment = Fabricate(:article, :body => "Four score and seven years ago...")
+ comment.word_count.should eq(6)
+ end
+ end
end
View
87 spec/requests/dashboard_spec.rb
@@ -0,0 +1,87 @@
+require 'spec_helper'
+
+describe "front dashboard" do
+
+ it "loads from the root path" do
+ visit "/"
+ page.should have_content("Dashboard")
+ end
+
+ context "recent articles" do
+ before :each do
+ 6.times do |i|
+ Fabricate(:article, :title => "Title #{i}")
+ end
+
+ visit "/"
+ end
+
+ it "lists the five most recent articles" do
+ page.should have_content("Articles:")
+ all(".article").count.should eq(5)
+ end
+
+ it "lists them in reverse chronological order" do
+ titles = page.all('li.article').map(&:text)
+ titles.should eq(["Title 5", "Title 4", "Title 3", "Title 2", "Title 1"])
+ end
+ end
+
+ context "recent comments" do
+ before :each do
+ 6.times do |i|
+ Fabricate(:comment, :body => "Body #{i}")
+ end
+
+ visit "/"
+ end
+
+ it "lists the five most recent comments" do
+ page.should have_content("Comments:")
+ all(".comment").count.should eq(5)
+ end
+
+ it "lists them in reverse order" do
+ bodies = page.all('li.comment').map(&:text)
+ bodies.should eq(["Body 5", "Body 4", "Body 3", "Body 2", "Body 1"])
+ end
+ end
+
+ context "statistics" do
+ it "lists the total number of articles" do
+ number_of_articles = rand(10)
+ number_of_articles.times { Fabricate(:article) }
+
+ visit "/"
+ page.should have_content("Total articles: #{number_of_articles}")
+ end
+
+ it "lists the total number of comments" do
+ number_of_comments = rand(10)
+ number_of_comments.times { Fabricate(:comment) }
+
+ visit "/"
+ page.should have_content("Total comments: #{number_of_comments}")
+ end
+
+
+ it "displays the total words of all articles" do
+ Fabricate(:article, :body => "Four score and seven years...")
+
+ visit "/"
+
+ page.should have_content("Total words: 5")
+ end
+
+ it "displays the total words of all comments" do
+ Fabricate(:comment, :body => "I think that...")
+
+ visit "/"
+
+ page.should have_content("Total words: 3")
+ 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"
+ end
+end
View
1  spec/spec_helper.rb
@@ -3,6 +3,7 @@
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rails'
+require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Please sign in to comment.
Something went wrong with that request. Please try again.