# How to synthetize a blog in 15 seconds
# by Michelangelo Altamore
generate :scaffold, "post", "title:string body:text"
rake "db:migrate"
file "app/models/post.rb", <<-POST
class Post < ActiveRecord::Base
validates_presence_of :title, :body
has_many :comments
end
POST
file "app/views/posts/show.html.erb", <<-POST_SHOW_ACTION
<%= render :partial => @post %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Destroy', @post, :method => :delete, :confirm => "Are you sure?" %>
<%= link_to "See All Posts", posts_path %>
Comments
<% remote_form_for [@post, Comment.new] do |f| %>
<%= f.label :body, "New Comment" %>
<%= f.text_area :body %>
<%= f.submit "Add Comment" %>
<% end %>
POST_SHOW_ACTION
file "app/views/posts/_post.html.erb", <<-POST_PARTIAL
<% div_for post do %>
<%= link_to_unless_current h(post.title), post %>
<%= simple_format h(post.body) %>
<% end %>
POST_PARTIAL
file "app/views/posts/index.html.erb", <<-POST_INDEX_ACTION
Listing posts
<%= render :partial => @posts %>
<%= link_to 'New post', new_post_path %>
POST_INDEX_ACTION
file "app/controllers/posts_controller.rb", <<-POSTS_CONTROLLER
class PostsController < ApplicationController
before_filter :authenticate, :except => [:index, :show]
# GET /posts
# GET /posts.xml
def index
@posts = Post.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.json { render :json => @posts }
format.atom
end
end
# GET /posts/1
# GET /posts/1.xml
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
# GET /posts/new
# GET /posts/new.xml
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.xml
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
flash[:notice] = 'Post was successfully created.'
format.html { redirect_to(@post) }
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.xml
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
flash[:notice] = 'Post was successfully updated.'
format.html { redirect_to(@post) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
private
def authenticate
authenticate_or_request_with_http_basic do |name, password|
name == 'admin' && password = 'password'
end
end
end
POSTS_CONTROLLER
file "app/views/posts/index.atom.builder", <<-ATOM_FEEDS
atom_feed do |feed|
feed.title("My great blog!")
feed.updated(@posts.first.created_at)
@posts.each do |post|
feed.entry(post) do |entry|
entry.title(post.title)
entry.content(post.body, :type => 'html')
entry.author { |author| author.name("Ryan Bates") }
end
end
end
ATOM_FEEDS
file "app/views/layouts/posts.html.erb", <<-POSTS_LAYOUT
Posts: <%= controller.action_name %>
<%= stylesheet_link_tag 'scaffold' %>
<%= auto_discovery_link_tag :atom, formatted_posts_path(:atom) %>
<%= javascript_include_tag :all %>
<%= flash[:notice] %>
<%= yield %>
POSTS_LAYOUT
generate :scaffold, "comments", "post:references body:text"
rake "db:migrate"
file "app/controllers/comments_controller.rb", <<-COMMENTS_CONTROLLER
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create!(params[:comment])
respond_to do |format|
format.html { redirect_to @post }
format.js
end
end
end
COMMENTS_CONTROLLER
file "app/views/comments/_comment.html.erb", <<-COMMENT_PARTIAL
<% div_for comment do %>
Posted <%= time_ago_in_words(comment.created_at) %> ago
<%= h(comment.body)%>
<% end %>
COMMENT_PARTIAL
file "app/views/comments/create.js.rjs", <<-CREATE
page.insert_html :bottom, :comments, :partial => @comment
page[@comment].visual_effect :highlight
page[:new_comment].reset
CREATE
file "config/routes.rb", <<-ROUTES
ActionController::Routing::Routes.draw do |map|
map.resources :posts, :has_many => :comments
end
ROUTES
file "test/functional/comments_controller_test.rb", <<-COMMENTS_TESTS
require 'test_helper'
class CommentsControllerTest < ActionController::TestCase
test "should create comment and redirect to post without javascript" do
p = Post.create!(:title => 'hello', :body => 'world')
post :create, :post_id => p.id, :comment => { :body => 'nice!' }
assert_redirected_to post_url(p)
assert_equal 'nice!', p.comments.first.body
end
test "should create comment and render RJS template for ajax" do
p = Post.create!(:title => 'hello', :body => 'world')
post :create, :format => 'js', :post_id => p.id, :comment => { :body => 'nice!' }
assert_equal 'nice!', p.comments.first.body
end
end
COMMENTS_TESTS
file "test/functional/posts_controller_test.rb",<<-POSTS_TEST
# require 'test_helper'
#
# class PostsControllerTest < ActionController::TestCase
# test "should get index" do
# get :index
# assert_response :success
# assert_not_nil assigns(:posts)
# end
#
# test "should get new" do
# get :new
# assert_response :success
# end
#
# test "should create post" do
# assert_difference('Post.count') do
# post :create, :post => { }
# end
#
# assert_redirected_to post_path(assigns(:post))
# end
#
# test "should show post" do
# get :show, :id => posts(:one).id
# assert_response :success
# end
#
# test "should get edit" do
# get :edit, :id => posts(:one).id
# assert_response :success
# end
#
# test "should update post" do
# put :update, :id => posts(:one).id, :post => { }
# assert_redirected_to post_path(assigns(:post))
# end
#
# test "should destroy post" do
# assert_difference('Post.count', -1) do
# delete :destroy, :id => posts(:one).id
# end
#
# assert_redirected_to posts_path
# end
# end
POSTS_TEST
rake "rails:update:application_controller"