Permalink
Browse files

more

  • Loading branch information...
1 parent 364a82d commit 15636a3e1ccdd063c08270c220c11a0c08467699 @geelen committed Mar 30, 2012
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+// Place all the styles related to the blog controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,12 @@
+class BlogController < ApplicationController
+ def index
+ @blog = Blog.new
+ post1 = @blog.new_post
+ post1.title = "Paint just applied"
+ post1.body = "Paint just applied. It's a lovely orangey-purple!"
+ post1.publish
+ post2 = @blog.new_post(title: "Still wet")
+ post2.body = "Paint is still quite wet. No bubbling yet!"
+ post2.publish
+ end
+end
@@ -0,0 +1,2 @@
+module BlogHelper
+end
View
@@ -0,0 +1,32 @@
+class Blog
+ attr_reader :entries
+ attr_writer :post_source
+
+ def initialize
+ @entries = []
+ end
+
+ def title
+ "Watching Paint Dry"
+ end
+
+ def subtitle
+ "The trusted source for drying paint news & opinion"
+ end
+
+ def new_post(*args)
+ post_source.call(*args).tap do |p|
+ p.blog = self
+ end
+ end
+
+ def add_entry(entry)
+ entries << entry
+ end
+
+ private
+
+ def post_source
+ @post_source ||= Post.public_method(:new)
+ end
+end
View
@@ -0,0 +1,12 @@
+class Post
+ attr_accessor :blog, :title, :body
+
+ def initialize(attrs = {})
+ attrs.each do |k,v| send("#{k}=",v) end
+ end
+
+ def publish
+ blog.add_entry(self)
+ end
+
+end
@@ -0,0 +1,6 @@
+<article>
+ <header>
+ <h3><%= entry.title %></h3>
+ </header>
+ <p><%= entry.body %></p>
+</article>
@@ -0,0 +1,3 @@
+<h1><%= @blog.title %></h1>
+<h2><%= @blog.subtitle %></h2>
+<%= render partial: "entry", collection: @blog.entries %>
View
@@ -0,0 +1,42 @@
+# encoding: UTF-8
+
+require 'minitest/autorun'
+require 'ostruct'
+
+require_relative '../../app/models/blog'
+
+describe Blog do
+ subject { Blog.new }
+ it "has no entries" do
+ subject.entries.must_be_empty
+ end
+
+ describe "#new_post" do
+ before do
+ @new_post = OpenStruct.new
+ subject.post_source = -> { @new_post }
+ end
+ it "returns a new post" do
+ subject.new_post.must_equal @new_post
+ end
+ it "sets the post’s blog reference to itself" do
+ subject.new_post.blog.must_equal(subject)
+ end
+ it "accepts an attribute hash on behalf of the post maker" do
+ post_source = MiniTest::Mock.new
+ post_source.expect(:call, @new_post, [{x: 42, y: 'z'}])
+ subject.post_source = post_source
+ subject.new_post(x: 42, y: 'z')
+ post_source.verify
+ end
+ end
+
+ describe "#add_entry" do
+ it "adds the entry to the blog" do
+ entry = Object.new
+ subject.add_entry(entry)
+ subject.entries.must_include(entry)
+ end
+ end
+end
+
View
@@ -0,0 +1,49 @@
+# encoding: UTF-8
+
+require 'minitest/autorun'
+require_relative '../../app/models/post'
+
+describe Post do
+ subject { Post.new }
+
+ it "starts with blank attributes" do
+ subject.title.must_be_nil
+ subject.body.must_be_nil
+ end
+
+ it "supports reading and writing a title" do
+ subject.title = "foo"
+ subject.title.must_equal "foo"
+ end
+
+ it "supports reading and writing a post body" do
+ subject.body = "foo"
+ subject.body.must_equal "foo"
+ end
+
+ it "supports reading and writing a blog reference" do
+ blog = Object.new
+ subject.blog = blog
+ subject.blog.must_equal blog
+ end
+
+ describe "#publish" do
+ before do
+ @blog = MiniTest::Mock.new
+ subject.blog = @blog
+ end
+ after do
+ @blog.verify
+ end
+ it "adds the post to the blog" do
+ @blog.expect :add_entry, nil, [subject]
+ subject.publish
+ end
+ end
+
+ it "supports setting attributes in the initializer" do
+ it = Post.new(title: "mytitle", body: "mybody")
+ it.title.must_equal "mytitle"
+ it.body.must_equal "mybody"
+ end
+end
@@ -0,0 +1,9 @@
+require 'test_helper'
+
+class BlogControllerTest < ActionController::TestCase
+ test "should get index" do
+ get :index
+ assert_response :success
+ end
+
+end
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class BlogHelperTest < ActionView::TestCase
+end

0 comments on commit 15636a3

Please sign in to comment.