Skip to content

Commit

Permalink
g scaffold book title:string price:integer
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Feb 28, 2010
1 parent b775dc5 commit 6244190
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 0 deletions.
83 changes: 83 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class BooksController < ApplicationController
# GET /books
# GET /books.xml
def index
@books = Book.all

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @books }
end
end

# GET /books/1
# GET /books/1.xml
def show
@book = Book.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @book }
end
end

# GET /books/new
# GET /books/new.xml
def new
@book = Book.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @book }
end
end

# GET /books/1/edit
def edit
@book = Book.find(params[:id])
end

# POST /books
# POST /books.xml
def create
@book = Book.new(params[:book])

respond_to do |format|
if @book.save
format.html { redirect_to(@book, :notice => 'Book was successfully created.') }
format.xml { render :xml => @book, :status => :created, :location => @book }
else
format.html { render :action => "new" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end

# PUT /books/1
# PUT /books/1.xml
def update
@book = Book.find(params[:id])

respond_to do |format|
if @book.update_attributes(params[:book])
format.html { redirect_to(@book, :notice => 'Book was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /books/1
# DELETE /books/1.xml
def destroy
@book = Book.find(params[:id])
@book.destroy

respond_to do |format|
format.html { redirect_to(books_url) }
format.xml { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/books_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module BooksHelper
end
2 changes: 2 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Book < ActiveRecord::Base
end
15 changes: 15 additions & 0 deletions app/views/books/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% form_for(@book) do |f| %>
<%= f.error_messages %>

<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :price %><br />
<%= f.text_field :price %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/books/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing book</h1>

<%= render 'form' %>
<%= link_to 'Show', @book %> |
<%= link_to 'Back', books_path %>
25 changes: 25 additions & 0 deletions app/views/books/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h1>Listing books</h1>

<table>
<tr>
<th>Title</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @books.each do |book| %>
<tr>
<td><%= book.title %></td>
<td><%= book.price %></td>
<td><%= link_to 'Show', book %></td>
<td><%= link_to 'Edit', edit_book_path(book) %></td>
<td><%= link_to 'Destroy', book, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New book', new_book_path %>
5 changes: 5 additions & 0 deletions app/views/books/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New book</h1>

<%= render 'form' %>
<%= link_to 'Back', books_path %>
13 changes: 13 additions & 0 deletions app/views/books/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<p>
<b>Title:</b>
<%= @book.title %>
</p>

<p>
<b>Price:</b>
<%= @book.price %>
</p>


<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
16 changes: 16 additions & 0 deletions app/views/layouts/books.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Books: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>

<p class="notice"><%= notice %></p>

<%= yield %>

</body>
</html>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Tork032::Application.routes.draw do |map|
resources :books

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

Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20100228072022_create_books.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.string :title
t.integer :price

t.timestamps
end
end

def self.down
drop_table :books
end
end
61 changes: 61 additions & 0 deletions public/stylesheets/scaffold.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
body { background-color: #fff; color: #333; }

body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

div.field, div.actions {
margin-bottom: 10px;
}

.notice {
color: green;
}

.fieldWithErrors {
padding: 2px;
background-color: red;
display: table;
}

#errorExplanation {
width: 400px;
border: 2px solid red;
padding: 7px;
padding-bottom: 12px;
margin-bottom: 20px;
background-color: #f0f0f0;
}

#errorExplanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
background-color: #c00;
color: #fff;
}

#errorExplanation p {
color: #333;
margin-bottom: 0;
padding: 5px;
}

#errorExplanation ul li {
font-size: 12px;
list-style: square;
}
9 changes: 9 additions & 0 deletions test/fixtures/books.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
title: MyString
price: 1

two:
title: MyString
price: 1
45 changes: 45 additions & 0 deletions test/functional/books_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'test_helper'

class BooksControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:books)
end

test "should get new" do
get :new
assert_response :success
end

test "should create book" do
assert_difference('Book.count') do
post :create, :book => books(:one).attributes
end

assert_redirected_to book_path(assigns(:book))
end

test "should show book" do
get :show, :id => books(:one).to_param
assert_response :success
end

test "should get edit" do
get :edit, :id => books(:one).to_param
assert_response :success
end

test "should update book" do
put :update, :id => books(:one).to_param, :book => books(:one).attributes
assert_redirected_to book_path(assigns(:book))
end

test "should destroy book" do
assert_difference('Book.count', -1) do
delete :destroy, :id => books(:one).to_param
end

assert_redirected_to books_path
end
end
8 changes: 8 additions & 0 deletions test/unit/book_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'test_helper'

class BookTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/books_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class BooksHelperTest < ActionView::TestCase
end

0 comments on commit 6244190

Please sign in to comment.