diff --git a/app/models/book.rb b/app/models/book.rb index 95c42dd..9b0358a 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -2,6 +2,7 @@ class Book < ActiveRecord::Base attr_accessible :memo, :purchased_on, :title validates :title, :presence => true + has_many :memos, :dependent => :destroy before_create :total_books_count diff --git a/app/models/memo.rb b/app/models/memo.rb new file mode 100644 index 0000000..487736e --- /dev/null +++ b/app/models/memo.rb @@ -0,0 +1,4 @@ +class Memo < ActiveRecord::Base + attr_accessible :book_id, :body + validates_length_of :body, :within => 1..100 +end diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 23005ab..6c96569 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -15,6 +15,14 @@ <%= @book.purchased_on %>

+<%= link_to 'Add New Comment', new_book_memo_path(@book) %>
+<% @book.memos.each do |memo| %> + Comment: + <%= memo.body %> + (<%= link_to "Delete", book_memo_path(:book_id => @book.id, :id => memo.id), + :confirm => "ok?", :method => :delete %>)
+<% end %> +
<%= link_to 'Edit', edit_book_path(@book) %> | <%= link_to 'Back', books_path %> diff --git a/app/views/memos/new.html b/app/views/memos/new.html new file mode 100644 index 0000000..c0c1f16 --- /dev/null +++ b/app/views/memos/new.html @@ -0,0 +1,7 @@ +Title: <%= @book.title %>
+
+<%= form_for @memo, :url => book_memos_path do |f| %> + Add new Memo(with in 100 moji):
+ <%= f.text_field :body %>
+ <%= f.submit %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index f3d38ab..bafdadd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ 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/migrate/20120616050801_create_memos.rb b/db/migrate/20120616050801_create_memos.rb new file mode 100644 index 0000000..f0350d9 --- /dev/null +++ b/db/migrate/20120616050801_create_memos.rb @@ -0,0 +1,10 @@ +class CreateMemos < ActiveRecord::Migration + def change + create_table :memos do |t| + t.integer :book_id, :null =>false + t.text :body + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5fc61c2..a1f1131 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 => 20120616050801) 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", :null => false + t.text "body" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + end diff --git a/spec/models/memo_spec.rb b/spec/models/memo_spec.rb new file mode 100644 index 0000000..7fdbc67 --- /dev/null +++ b/spec/models/memo_spec.rb @@ -0,0 +1,16 @@ +# coding:utf-8 +require 'spec_helper' + +describe Memo do + context "within 100 moji" do + body = "a"*100 + subject {Memo.new(:body => body).valid?} + it { should be_true } + end + + context "more than 100 moji" do + body = "a"*101 + 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..62ad6b2 100644 --- a/spec/requests/books_spec.rb +++ b/spec/requests/books_spec.rb @@ -37,4 +37,34 @@ end end end + + describe '/books/:id/memos/new' do + let!(:book){FactoryGirl.create :book } + subject {page } + + before { visit "/books/#{book.id}/memos/new" } + let(:memo_body){["hoge1","hoge2"]} + + context "add 1memo" do + before do + fill_in "memo[body]", with: memo_body[0] + click_on "Create Memo" + end + + it "resist check" do + should have_content(memo_body[0]) + end + end + + context "add 2memo" do + before do + fill_in "memo[body]", with: memo_body[1] + click_on "Create Memo" + end + + it "resist check" do + should have_content(memo_body[1]) + end + end + end end