-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create post resource with initial tests
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
Showing
11 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class PostsController < ApplicationController | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module PostsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'spec_helper' | ||
|
||
describe PostsController do | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters