From f8b267dd05439e70c5f4f0c346d95ccc32c5331f Mon Sep 17 00:00:00 2001
From: sawamitsuter01
Date: Thu, 14 Jun 2012 18:30:46 -0700
Subject: [PATCH 1/3] Initial commit
---
app/models/memo.rb | 3 +++
db/migrate/20120615012627_create_memos.rb | 14 ++++++++++++++
spec/factories/memos.rb | 8 ++++++++
spec/models/memo_spec.rb | 5 +++++
4 files changed, 30 insertions(+)
create mode 100644 app/models/memo.rb
create mode 100644 db/migrate/20120615012627_create_memos.rb
create mode 100644 spec/factories/memos.rb
create mode 100644 spec/models/memo_spec.rb
diff --git a/app/models/memo.rb b/app/models/memo.rb
new file mode 100644
index 0000000..f26393c
--- /dev/null
+++ b/app/models/memo.rb
@@ -0,0 +1,3 @@
+class Memo < ActiveRecord::Base
+ attr_accessible :body, :book_id
+end
diff --git a/db/migrate/20120615012627_create_memos.rb b/db/migrate/20120615012627_create_memos.rb
new file mode 100644
index 0000000..6a31f6f
--- /dev/null
+++ b/db/migrate/20120615012627_create_memos.rb
@@ -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
diff --git a/spec/factories/memos.rb b/spec/factories/memos.rb
new file mode 100644
index 0000000..8d3526d
--- /dev/null
+++ b/spec/factories/memos.rb
@@ -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
diff --git a/spec/models/memo_spec.rb b/spec/models/memo_spec.rb
new file mode 100644
index 0000000..c4d756c
--- /dev/null
+++ b/spec/models/memo_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Memo do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
From efb50ca309a87e0807129c37b47192424ea7c726 Mon Sep 17 00:00:00 2001
From: sawamitsuter01
Date: Thu, 14 Jun 2012 19:27:43 -0700
Subject: [PATCH 2/3] Implemented a memo page
---
app/controllers/books_controller.rb | 6 +++--
app/controllers/memos_controller.rb | 38 +++++++++++++++++++++++++++++
app/models/book.rb | 1 +
app/views/books/show.html.erb | 9 +++++++
app/views/memos/edit.html.erb | 9 +++++++
app/views/memos/new.html.erb | 8 ++++++
config/routes.rb | 6 +++--
db/schema.rb | 9 ++++++-
8 files changed, 81 insertions(+), 5 deletions(-)
create mode 100644 app/controllers/memos_controller.rb
create mode 100644 app/views/memos/edit.html.erb
create mode 100644 app/views/memos/new.html.erb
diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb
index d13ffd9..065113f 100644
--- a/app/controllers/books_controller.rb
+++ b/app/controllers/books_controller.rb
@@ -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
@@ -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
diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb
new file mode 100644
index 0000000..fe41343
--- /dev/null
+++ b/app/controllers/memos_controller.rb
@@ -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
diff --git a/app/models/book.rb b/app/models/book.rb
index 95c42dd..c5f099f 100644
--- a/app/models/book.rb
+++ b/app/models/book.rb
@@ -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
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb
index 23005ab..fb554c6 100644
--- a/app/views/books/show.html.erb
+++ b/app/views/books/show.html.erb
@@ -15,6 +15,15 @@
<%= @book.purchased_on %>
+
+ <%= link_to 'Memo', new_book_memo_path(@book) %>
+
+<% @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 %>
+<% end %>
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
diff --git a/app/views/memos/edit.html.erb b/app/views/memos/edit.html.erb
new file mode 100644
index 0000000..b6de417
--- /dev/null
+++ b/app/views/memos/edit.html.erb
@@ -0,0 +1,9 @@
+Title: <%= @book.title %>
+Memo: <%= @book.memo %>
+Purchased on: <%= @book.purchased_on %>
+
+<%= form_for @memo, :url => book_memo_path(:book_id => @book.id,
+ :id => @memo.id), :method => :put do |f| %>
+ Memo: <%= f.text_field :body %>
+ <%= f.submit %>
+<% end %>
diff --git a/app/views/memos/new.html.erb b/app/views/memos/new.html.erb
new file mode 100644
index 0000000..51f820d
--- /dev/null
+++ b/app/views/memos/new.html.erb
@@ -0,0 +1,8 @@
+Title: <%= @book.title %>
+Memo: <%= @book.memo %>
+Purchased on: <%= @book.purchased_on %>
+
+<%= form_for @memo, :url => book_memos_path do |f| %>
+ Memo: <%= f.text_field :body %>
+ <%= f.submit %>
+<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index f3d38ab..17d97c2 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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.
diff --git a/db/schema.rb b/db/schema.rb
index 5fc61c2..2ffe2f9 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -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"
@@ -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
From 6ee383ff1c9b9adeba99c706aab416d27d156e5b Mon Sep 17 00:00:00 2001
From: sawamitsuter01
Date: Thu, 14 Jun 2012 23:48:53 -0700
Subject: [PATCH 3/3] added rpec test cases
---
app/models/memo.rb | 1 +
app/views/books/show.html.erb | 2 +-
app/views/memos/edit.html.erb | 3 ++-
spec/helpers/books_helper_spec.rb | 1 -
spec/models/memo_spec.rb | 12 ++++++++-
spec/requests/books_spec.rb | 41 +++++++++++++++++++++++++++++++
6 files changed, 56 insertions(+), 4 deletions(-)
diff --git a/app/models/memo.rb b/app/models/memo.rb
index f26393c..f186e1f 100644
--- a/app/models/memo.rb
+++ b/app/models/memo.rb
@@ -1,3 +1,4 @@
class Memo < ActiveRecord::Base
attr_accessible :body, :book_id
+ validates :body, :presence => true, :length => { :maximum => 100 }
end
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb
index fb554c6..d076385 100644
--- a/app/views/books/show.html.erb
+++ b/app/views/books/show.html.erb
@@ -16,7 +16,7 @@
- <%= link_to 'Memo', new_book_memo_path(@book) %>
+ <%= link_to 'Add a memo', new_book_memo_path(@book) %>
<% @book.memos.each do |memo| %>
<%= memo.body %> |
diff --git a/app/views/memos/edit.html.erb b/app/views/memos/edit.html.erb
index b6de417..023ebf7 100644
--- a/app/views/memos/edit.html.erb
+++ b/app/views/memos/edit.html.erb
@@ -2,8 +2,9 @@ Title: <%= @book.title %>
Memo: <%= @book.memo %>
Purchased on: <%= @book.purchased_on %>
+
<%= form_for @memo, :url => book_memo_path(:book_id => @book.id,
:id => @memo.id), :method => :put do |f| %>
- Memo: <%= f.text_field :body %>
+ Additional memo: <%= f.text_field :body %>
<%= f.submit %>
<% end %>
diff --git a/spec/helpers/books_helper_spec.rb b/spec/helpers/books_helper_spec.rb
index 24456a3..b3c5992 100644
--- a/spec/helpers/books_helper_spec.rb
+++ b/spec/helpers/books_helper_spec.rb
@@ -11,5 +11,4 @@
# end
# end
describe BooksHelper do
- pending "add some examples to (or delete) #{__FILE__}"
end
diff --git a/spec/models/memo_spec.rb b/spec/models/memo_spec.rb
index c4d756c..85d081e 100644
--- a/spec/models/memo_spec.rb
+++ b/spec/models/memo_spec.rb
@@ -1,5 +1,15 @@
require 'spec_helper'
describe Memo do
- pending "add some examples to (or delete) #{__FILE__}"
+ 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
diff --git a/spec/requests/books_spec.rb b/spec/requests/books_spec.rb
index 37522b7..f4d4a1d 100644
--- a/spec/requests/books_spec.rb
+++ b/spec/requests/books_spec.rb
@@ -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