Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def create
format.xml { render xml: @book, status: :created, location: @book }
format.json { render json: @book, status: :created, location: @book }
else
format.html { render action: "new" }
# format.html { render action: "new" }
format.html { render :new }
format.xml { render xml: @book.errors, status: :unprocessable_entity }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
Expand All @@ -66,7 +67,8 @@ def update
format.xml { head :no_content }
format.json { head :no_content }
else
format.html { render action: "edit" }
# format.html { render action: "edit" }
format.html { render :edit }
format.xml { render xml: @book.errors, status: :unprocessable_entity }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
Expand Down
38 changes: 38 additions & 0 deletions app/controllers/memos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class MemosController < ApplicationController
def new
@book = Book.find(params[:book_id])
@memo = @book.memos.new
end

def create
@book = Book.find(params[:book_id])
@memo = @book.memos.new(params[:memo])
if @memo.save
redirect_to book_path(@book), :notice => "Registered the memo."
else
render :new
end
end

def edit
@book = Book.find(params[:book_id])
@memo = @book.memos.find(params[:id])
end

def update
@book = Book.find(params[:book_id])
@memo = @book.memos.find(params[:id])
if @memo.update_attributes(params[:memo])
redirect_to book_path(@book), :notice => "Update a memo."
else
render :action => "Edit"
end
end

def destroy
@book = Book.find(params[:book_id])
@memo = @book.memos.find(params[:id])
@memo.destroy
redirect_to book_path(@book)
end
end
1 change: 1 addition & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: UTF-8
class Book < ActiveRecord::Base
has_many :memos, :dependent => :destroy
attr_accessible :memo, :purchased_on, :title
validates :title, :presence => true

Expand Down
4 changes: 4 additions & 0 deletions app/models/memo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Memo < ActiveRecord::Base
attr_accessible :body, :book_id
validates :body, :presence => true, :length => { :maximum => 100 }
end
9 changes: 9 additions & 0 deletions app/views/books/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
<%= @book.purchased_on %>
</p>

<p>
<%= link_to 'Add a memo', new_book_memo_path(@book) %>
</p>
<% @book.memos.each do |memo| %>
<%= memo.body %> |
<%= link_to "Edit", edit_book_memo_path(:book_id => @book.id, :id => memo.id) %> |
<%= link_to "Delete", book_memo_path(:book_id => @book.id, :id => memo.id),
:confirm => "Delete a memo?", :method => :delete %><br />
<% end %><br />

<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
10 changes: 10 additions & 0 deletions app/views/memos/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Title: <%= @book.title %><br />
Memo: <%= @book.memo %><br />
Purchased on: <%= @book.purchased_on %><br />
<br />

<%= form_for @memo, :url => book_memo_path(:book_id => @book.id,
:id => @memo.id), :method => :put do |f| %>
Additional memo: <%= f.text_field :body %><br />
<%= f.submit %><br />
<% end %>
8 changes: 8 additions & 0 deletions app/views/memos/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Title: <%= @book.title %><br />
Memo: <%= @book.memo %><br />
Purchased on: <%= @book.purchased_on %><br />
<br />
<%= form_for @memo, :url => book_memos_path do |f| %>
Memo: <%= f.text_field :body %><br />
<%= f.submit %><br />
<% end %>
6 changes: 4 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
BookMemo2::Application.routes.draw do
resources :books

resources :books do
resources :memos
end

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

Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20120615012627_create_memos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateMemos < ActiveRecord::Migration
def change
create_table :memos do |t|
t.integer :book_id
t.text :body

t.timestamps
end
end

def self.down
drop_table :memos
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120526050801) do
ActiveRecord::Schema.define(:version => 20120615012627) do

create_table "books", :force => true do |t|
t.string "title"
Expand All @@ -21,4 +21,11 @@
t.datetime "updated_at", :null => false
end

create_table "memos", :force => true do |t|
t.integer "book_id"
t.text "body"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

end
8 changes: 8 additions & 0 deletions spec/factories/memos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryGirl.define do
factory :memo do
book_id 1
body "MyText"
end
end
1 change: 0 additions & 1 deletion spec/helpers/books_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
# end
# end
describe BooksHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
15 changes: 15 additions & 0 deletions spec/models/memo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe Memo do
context "character length is below 100." do
body = "x" * 100
subject { Memo.new(:body => body).valid? }
it { should be_true }
end

context "character length is over 100." do
body = "x" * (100 + 1)
subject { Memo.new(:body => body).valid? }
it { should be_false }
end
end
41 changes: 41 additions & 0 deletions spec/requests/books_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,45 @@
end
end
end

describe '/books/:id/memos/new' do
let!(:book){ FactoryGirl.create :book }
subject { page }

before { visit "/books/#{book.id}/memos/new" }

context "Write a memo first" do
let(:memo_body){ 'first memo!' }

before do
fill_in "memo[body]", with: memo_body
click_on 'Create Memo'
end

it "Page transition check" do
current_path.should == book_path(book)
end

it "Input name check" do
should have_content memo_body
end
end

context "Write a memo second" do
let(:memo_body){ 'second memo!' }

before do
fill_in "memo[body]", with: memo_body
click_on 'Create Memo'
end

it "Page transition check" do
current_path.should == book_path(book)
end

it "Input name check" do
should have_content memo_body
end
end
end
end