Skip to content

Commit

Permalink
Merge branch 'master' of github.com:JumpstartLab/jsblogger_advanced
Browse files Browse the repository at this point in the history
  • Loading branch information
jcasimir committed Jan 10, 2012
2 parents 3a909dd + fccf20d commit 504c3f2
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -13,3 +13,5 @@
# Ignore all logfiles and tempfiles. # Ignore all logfiles and tempfiles.
/log/*.log /log/*.log
/tmp /tmp

*.swp
6 changes: 6 additions & 0 deletions app/controllers/dashboard_controller.rb
@@ -1,5 +1,11 @@
class DashboardController < ApplicationController class DashboardController < ApplicationController
def show 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
end end
12 changes: 12 additions & 0 deletions app/models/article.rb
Expand Up @@ -36,4 +36,16 @@ def self.search(params)
[tag.articles, tag] [tag.articles, tag]
end end
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 end
12 changes: 12 additions & 0 deletions app/models/comment.rb
Expand Up @@ -2,4 +2,16 @@ class Comment < ActiveRecord::Base
belongs_to :article belongs_to :article


validates :article_id, :presence => true 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 end
20 changes: 19 additions & 1 deletion 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>
38 changes: 38 additions & 0 deletions spec/controllers/dashboard_controller_spec.rb
@@ -1,5 +1,43 @@
require 'spec_helper' require 'spec_helper'


describe DashboardController do 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 end
3 changes: 2 additions & 1 deletion spec/fabricators/comment_fabricator.rb
@@ -1,4 +1,5 @@
Fabricator(:comment) do Fabricator(:comment) do
author_name { Faker::Name.name } author_name { Faker::Name.name }
body { Faker::Lorem.paragraph } body { Faker::Lorem.paragraph }
end article!
end
15 changes: 0 additions & 15 deletions spec/helpers/dashboard_helper_spec.rb

This file was deleted.

32 changes: 0 additions & 32 deletions spec/integration/dashboard_spec.rb

This file was deleted.

28 changes: 28 additions & 0 deletions spec/models/articles_spec.rb
Expand Up @@ -34,4 +34,32 @@
it "responds to .valid_ids with a set of all current article IDs" do 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} Article.valid_ids.should == Article.select(:id).collect{|a| a.id}
end 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 end
43 changes: 32 additions & 11 deletions spec/models/comment_spec.rb
@@ -1,25 +1,46 @@
require 'spec_helper' require 'spec_helper'


describe Comment do describe Comment do
before(:each) do let(:comment) { Fabricate(:comment) }
@article = Article.create(:title => "Hello, World",
:body => "Sample Body.")
@comment = @article.comments.create(:author_name => "Daffy Duck",
:body => "My comment goes here.")
end


it "should be valid with valid attributes" do it "should be valid with valid attributes" do
@article.should be_valid comment.should be_valid
@comment.should be_valid
end end


it "should respond to article" do it "should respond to article" do
@comment.should respond_to(:article) comment.should respond_to(:article)
end end


it "must have a value in article_id" do it "must have a value in article_id" do
@comment.article_id = nil comment.article_id = nil
@comment.should_not be_valid 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 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 end
87 changes: 87 additions & 0 deletions 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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -3,6 +3,7 @@
require File.expand_path("../../config/environment", __FILE__) require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails' require 'rspec/rails'
require 'capybara/rails' require 'capybara/rails'
require 'capybara/rspec'


# Requires supporting ruby files with custom matchers and macros, etc, # Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories. # in spec/support/ and its subdirectories.
Expand Down

0 comments on commit 504c3f2

Please sign in to comment.