Permalink
Browse files

about to add validation

  • Loading branch information...
1 parent 0a8c575 commit 07a23abe68dd4be746369acf66cb1d18c76c5463 @geelen committed Mar 30, 2012
Showing with 59 additions and 5 deletions.
  1. +1 −0 Gemfile
  2. +2 −0 Gemfile.lock
  3. +5 −2 app/models/blog.rb
  4. +3 −2 app/models/post.rb
  5. +1 −0 app/views/blog/_entry.html.erb
  6. +25 −1 spec/models/blog_speck.rb
  7. +22 −0 spec/models/post_speck.rb
View
@@ -34,4 +34,5 @@ group :test do
# Pretty printed test output
gem 'turn', '~> 0.8.3', :require => false
gem 'minitest'
+ gem 'rr'
end
View
@@ -83,6 +83,7 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
+ rr (1.0.4)
sass (3.1.15)
sass-rails (3.1.6)
actionpack (~> 3.1.0)
@@ -114,6 +115,7 @@ DEPENDENCIES
jquery-rails
minitest
rails (= 3.1.3)
+ rr
sass-rails (~> 3.1.5)
sqlite3
turn (~> 0.8.3)
View
@@ -1,5 +1,4 @@
class Blog
- attr_reader :entries
attr_writer :post_source
def initialize
@@ -14,14 +13,18 @@ def subtitle
"The trusted source for drying paint news & opinion"
end
+ def entries
+ @entries.sort_by { |e| e.pubdate }.reverse.take(10)
+ end
+
def new_post(*args)
post_source.call(*args).tap do |p|
p.blog = self
end
end
def add_entry(entry)
- entries << entry
+ @entries << entry
end
private
View
@@ -2,13 +2,14 @@ class Post
extend ActiveModel::Naming
include ActiveModel::Conversion
- attr_accessor :blog, :title, :body
+ attr_accessor :blog, :title, :body, :pubdate
def initialize(attrs = {})
attrs.each do |k,v| send("#{k}=",v) end
end
- def publish
+ def publish(clock = DateTime)
+ self.pudate = clock.now
blog.add_entry(self)
end
@@ -1,5 +1,6 @@
<article>
<header>
+ <p><time pubdate="pubdate"><%= entry.pubdate %></time></p>
<h3><%= entry.title %></h3>
</header>
<p><%= entry.body %></p>
View
@@ -2,6 +2,7 @@
require 'minitest/autorun'
require 'ostruct'
+require_relative '../spec_helper_lite'
require_relative '../../app/models/blog'
@@ -33,10 +34,33 @@
describe "#add_entry" do
it "adds the entry to the blog" do
- entry = Object.new
+ entry = stub!
subject.add_entry(entry)
subject.entries.must_include(entry)
end
end
+ describe "#entries" do
+ def stub_entry_with_date(date)
+ OpenStruct.new(pubdate: DateTime.parse(date))
+ end
+ it "is sorted in reverse-chronological order" do
+ oldest = stub_entry_with_date("2011-09-09")
+ newest = stub_entry_with_date("2011-09-11")
+ middle = stub_entry_with_date("2011-09-10")
+ subject.add_entry(oldest)
+ subject.add_entry(newest)
+ subject.add_entry(middle)
+ subject.entries.must_equal([newest, middle, oldest])
+ end
+ it "is limited to 10 items" do
+ 10.times do |i|
+ subject.add_entry(stub_entry_with_date("2011-09-#{i+1}"))
+ end
+ oldest = stub_entry_with_date("2011-08-30")
+ subject.add_entry(oldest)
+ subject.entries.size.must_equal(10)
+ subject.entries.wont_include(oldest)
+ end
+ end
end
View
@@ -49,4 +49,26 @@
it.title.must_equal "mytitle"
it.body.must_equal "mybody"
end
+
+ describe "#pubdate" do
+ describe "before publishing" do
+ it "is blank" do
+ subject.pubdate.must_be_nil
+ end
+ end
+
+ describe "after publishing" do
+ before do
+ @clock = stub!
+ @now = DateTime.parse("2011-09-11T02:56")
+ stub(@clock).now(){@now}
+ subject.blog = stub!
+ subject.publish(@clock1)
+ end
+
+ it "is a datetime" do
+ subject.pubdate.must_equal(@now)
+ end
+ end
+ end
end

0 comments on commit 07a23ab

Please sign in to comment.