diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb new file mode 100644 index 0000000..8719588 --- /dev/null +++ b/app/controllers/memos_controller.rb @@ -0,0 +1,23 @@ +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 => "added memo" + else + render :new + 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..566c9cc 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/models/memo.rb b/app/models/memo.rb new file mode 100644 index 0000000..248d358 --- /dev/null +++ b/app/models/memo.rb @@ -0,0 +1,4 @@ +class Memo < ActiveRecord::Base + attr_accessible :book_id, :text + validates_length_of :text , :maximum => 100 +end diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 23005ab..75a0b00 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -7,9 +7,14 @@

Memo: - <%= @book.memo %> +

<%= @book.memo %>
+ <% @book.memos.each do |m| %> +
+ <%= m.text %> | + <%= link_to "delete", book_memo_path(:book_id => @book.id, :id => m.id), :confirm => "OK?", :method => :delete %> +
+ <% end %>

-

Purchased on: <%= @book.purchased_on %> @@ -17,4 +22,5 @@ <%= link_to 'Edit', edit_book_path(@book) %> | -<%= link_to 'Back', books_path %> +<%= link_to 'Back', books_path %> | +<%= link_to 'add memo', new_book_memo_path(@book) %> diff --git a/app/views/memos/new.html.erb b/app/views/memos/new.html.erb new file mode 100644 index 0000000..b90d647 --- /dev/null +++ b/app/views/memos/new.html.erb @@ -0,0 +1,5 @@ +

title <%= @book.title %>
+<%= form_for @memo, :url => book_memos_path do |f| %> + memo <%= f.text_field :text %> + <%= f.submit %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index f3d38ab..32492b6 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/20120615023422_create_memos.rb b/db/migrate/20120615023422_create_memos.rb new file mode 100644 index 0000000..0e64653 --- /dev/null +++ b/db/migrate/20120615023422_create_memos.rb @@ -0,0 +1,10 @@ +class CreateMemos < ActiveRecord::Migration + def change + create_table :memos do |t| + t.integer :book_id + t.text :text + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5fc61c2..5465c6c 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 => 20120615023422) 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 "text" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + end diff --git a/spec/factories/memos.rb b/spec/factories/memos.rb new file mode 100644 index 0000000..d618e74 --- /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 + text "MyText" + end +end diff --git a/spec/models/memo_spec.rb b/spec/models/memo_spec.rb new file mode 100644 index 0000000..14c285e --- /dev/null +++ b/spec/models/memo_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Memo do + describe "validate" do + context "in limit" do + text = "a" * 100 + subject { Memo.new(:text => text).valid? } + it { should be_true } + end + context "limit over" do + text = "a" * (100+1) + subject { Memo.new(:text => text).valid? } + it { should be_false } + end + end +end diff --git a/spec/requests/books_spec.rb b/spec/requests/books_spec.rb index 37522b7..1985836 100644 --- a/spec/requests/books_spec.rb +++ b/spec/requests/books_spec.rb @@ -37,4 +37,40 @@ end end end + + describe '/books/:id/memos/new' do + let!(:book){ FactoryGirl.create :book } + subject { page } + + before { visit "/books/#{book.id}/memos/new" } + context "post memo" do + before do + fill_in "memo_text" , with: "test1" + click_on "Create Memo" + end + + it "transit page" do + current_path.should == book_path(book) + end + + it "さっきのとうこうあること" do + should have_content("test1") + end + end + + context "role2" do + before do + fill_in "memo_text", with: "test2" + click_on "Create Memo" + end + + it "transit page" do + current_path.should == book_path(book) + end + + it "さっきのとうこうあること" do + should have_content("test2") + end + end + end end