diff --git a/.project b/.project new file mode 100644 index 0000000..417f7f4 --- /dev/null +++ b/.project @@ -0,0 +1,12 @@ + + + book_memo2 + + + + + + + org.radrails.rails.core.railsnature + + diff --git a/Gemfile.lock b/Gemfile.lock index 0f24858..8130291 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -77,11 +77,17 @@ GEM mime-types (1.18) multi_json (1.3.5) nokogiri (1.5.2) + nokogiri (1.5.2-x86-mingw32) polyglot (0.3.3) pry (0.9.9.6) coderay (~> 1.0.5) method_source (~> 0.7.1) slop (>= 2.4.4, < 3) + pry (0.9.9.6-x86-mingw32) + coderay (~> 1.0.5) + method_source (~> 0.7.1) + slop (>= 2.4.4, < 3) + win32console (~> 1.3) pry-rails (0.1.6) pry rack (1.4.1) @@ -140,6 +146,7 @@ GEM rack (~> 1.0) tilt (~> 1.1, != 1.3.0) sqlite3 (1.3.6) + sqlite3 (1.3.6-x86-mingw32) thor (0.14.6) tilt (1.3.3) treetop (1.4.10) @@ -149,11 +156,13 @@ GEM uglifier (1.2.4) execjs (>= 0.3.0) multi_json (>= 1.0.2) + win32console (1.3.2-x86-mingw32) xpath (0.1.4) nokogiri (~> 1.3) PLATFORMS ruby + x86-mingw32 DEPENDENCIES capybara diff --git a/app/assets/javascripts/books.js.coffee b/app/assets/javascripts/books.js.coffee index 7615679..242184b 100644 --- a/app/assets/javascripts/books.js.coffee +++ b/app/assets/javascripts/books.js.coffee @@ -1,3 +1,6 @@ # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in application.js. # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ + + + diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index d13ffd9..fa9809b 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -27,12 +27,7 @@ def show # GET /books/new.json def new @book = Book.new - - respond_to do |format| - format.html # new.html.erb - format.xml { render xml: @book } - format.json { render json: @book } - end + @book.memos.build end # GET /books/1/edit @@ -43,7 +38,9 @@ def edit # POST /books.json def create @book = Book.new(params[:book]) - + params[:memos].each do |memo| + @book.memos.build(memo) + end respond_to do |format| if @book.save format.html { redirect_to @book, notice: 'Book was successfully created.' } diff --git a/app/helpers/books_helper.rb b/app/helpers/books_helper.rb index 4b9311e..016684b 100644 --- a/app/helpers/books_helper.rb +++ b/app/helpers/books_helper.rb @@ -1,2 +1,3 @@ module BooksHelper + end diff --git a/app/models/book.rb b/app/models/book.rb index 95c42dd..2167b7a 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -1,11 +1,18 @@ # encoding: UTF-8 class Book < ActiveRecord::Base - attr_accessible :memo, :purchased_on, :title + + has_many :memos, :dependent => :destroy + accepts_nested_attributes_for :memos + attr_accessible :purchased_on, :title, :memos, :id validates :title, :presence => true - before_create :total_books_count - def total_books_count - self.memo += "【 累計冊数#{Book.count + 1} 】" + + def new_memo_attributes=(memo_attributes) + memo_attributes.each do |attributes| + memos.build(attributes) + end end + + end diff --git a/app/views/books/_form.html.erb b/app/views/books/_form.html.erb index da45ee0..9c4c87e 100644 --- a/app/views/books/_form.html.erb +++ b/app/views/books/_form.html.erb @@ -11,14 +11,18 @@ <% end %> -
+
<%= f.label :title %>
<%= f.text_field :title %>
-
- <%= f.label :memo %>
- <%= f.text_area :memo %> +
+Memo:
+
+ +
+ +
<%= f.label :purchased_on %>
<%= f.date_select :purchased_on %> @@ -27,3 +31,10 @@ <%= f.submit %>
<% end %> + + + diff --git a/app/views/books/index.html.erb b/app/views/books/index.html.erb index 5f03ad6..8c40fd6 100644 --- a/app/views/books/index.html.erb +++ b/app/views/books/index.html.erb @@ -13,10 +13,8 @@ <% @books.each do |book| %> <%= book.title %> - <%= book.memo %> <%= book.purchased_on %> <%= link_to 'Show', book %> - <%= link_to 'Edit', edit_book_path(book) %> <%= link_to 'Destroy', book, confirm: 'Are you sure?', method: :delete %> <% end %> diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 23005ab..c71f8fa 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -4,17 +4,21 @@ Title: <%= @book.title %>

-

- Memo: - <%= @book.memo %> +Memos:
+ +<% @book.memos.each do |m| %> + + + + +<% end %> +
<%= m.memo %>

-

Purchased on: <%= @book.purchased_on %>

-<%= link_to 'Edit', edit_book_path(@book) %> | <%= link_to 'Back', books_path %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 3bed8c4..4ae0ba0 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -3,7 +3,7 @@ BookMemo2 <%= stylesheet_link_tag "application", :media => "all" %> - <%= javascript_include_tag "application" %> + <%= javascript_include_tag :application %> <%= csrf_meta_tags %> diff --git a/config/routes.rb b/config/routes.rb index f3d38ab..af79db9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ BookMemo2::Application.routes.draw do + resources :memos + resources :books # The priority is based upon order of creation: diff --git a/db/migrate/20120526050801_create_books.rb b/db/migrate/20120526050801_create_books.rb index b04e895..7ce3c86 100644 --- a/db/migrate/20120526050801_create_books.rb +++ b/db/migrate/20120526050801_create_books.rb @@ -1,8 +1,8 @@ class CreateBooks < ActiveRecord::Migration def change create_table :books do |t| + t.string :id t.string :title - t.text :memo t.date :purchased_on t.timestamps diff --git a/db/schema.rb b/db/schema.rb index 5fc61c2..733f304 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 => 20120612123218) 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.text "memo" + t.string "book_id" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + end diff --git a/spec/models/book_spec.rb b/spec/models/book_spec.rb index f820855..25e6b97 100644 --- a/spec/models/book_spec.rb +++ b/spec/models/book_spec.rb @@ -11,11 +11,5 @@ end end - describe '#total_books_count' do - let(:book){ FactoryGirl.build :book } - before { book.save } - it "memoに累計冊数が表示されること" do - book.memo.should include "【 累計冊数#{Book.count} 】" - end - end + end diff --git a/spec/requests/books_spec.rb b/spec/requests/books_spec.rb index 37522b7..122baaa 100644 --- a/spec/requests/books_spec.rb +++ b/spec/requests/books_spec.rb @@ -10,6 +10,21 @@ end end + describe "POST /books" do + it "creates a book" do + post_via_redirect books_path, + :book=>{:title=>"The road to the future", + :memos=>[{:memo=>"aaa"},{:memo=>"bbb"}], + :purchased_on(1i)=>"2012", + :purchased_on(2i)=>"6", + :purchased_on(3i)=>"13" + } + response.body.should include("mow lawn") + end + end + + + describe '/books/:id/edit' do let!(:book){ FactoryGirl.create :book } subject { page }