Skip to content

Commit

Permalink
Create post resource with initial tests
Browse files Browse the repository at this point in the history
This commit also adds support for factory_girl

* Add the factory_girl gem
* Use factory_girl with rSpec
* Create post factory
* Write and satisfy initial tests on post
  • Loading branch information
mrDoktar committed May 11, 2010
1 parent a7bace9 commit a10ebfc
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ gem 'sqlite3-ruby', :require => 'sqlite3'

group :test do
gem "rspec-rails", ">= 2.0.0.beta.8"
gem 'factory_girl', :git => 'git://github.com/thoughtbot/factory_girl.git', :branch => 'rails3'
end

2 changes: 2 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class PostsController < ApplicationController
end
2 changes: 2 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PostsHelper
end
8 changes: 8 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Post < ActiveRecord::Base

validates_presence_of :title, :body

scope :published, where("published_on <= ?", Date.today)
scope :unpublished, where("published_on > ?", Date.today)
scope :latest, published.order("published_on desc")
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
BlogIn15::Application.routes.draw do |map|
resources :posts

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20100511083729_create_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreatePosts < ActiveRecord::Migration
def self.up
create_table :posts do |t|
t.string :title
t.text :body
t.date :published_on
end
end

def self.down
drop_table :posts
end
end
20 changes: 20 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100511083729) do

create_table "posts", :force => true do |t|
t.string "title"
t.text "body"
t.date "published_on"
end

end
5 changes: 5 additions & 0 deletions spec/controllers/posts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe PostsController do

end
5 changes: 5 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Factory.define :post do |p|
p.title "A title"
p.body "An body"
p.published_on Date.today
end
43 changes: 43 additions & 0 deletions spec/models/post_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

describe Post do

before do
@valid_attributes = {
:title => "My blog title",
:body => "Some content",
:published_on => Date.today
}
end

it "should require a title" do
Post.new(@valid_attributes.except(:title)).should_not be_valid
end

it "should require a body" do
Post.new(@valid_attributes.except(:body)).should_not be_valid
end

describe "calling scopes" do
before do
@post1 = Factory :post, :published_on => 2.days.ago
@post2 = Factory :post, :published_on => Date.today
@post3 = Factory :post, :published_on => 2.days.from_now
end

it "should return published posts" do
Post.published.should have(2).posts
end

it "should return unpublished posts" do
Post.unpublished.first.should eql(@post3)
end

it "should return the latest posts sorted by published date" do
posts = Post.latest
posts.first.should eql(@post2)
posts.last.should eql(@post1)
end

end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ENV["RAILS_ENV"] ||= 'test'
require File.dirname(__FILE__) + "/../config/environment" unless defined?(Rails)
require 'rspec/rails'
require 'factory_girl'

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

0 comments on commit a10ebfc

Please sign in to comment.