From 8486dcf238e9349a4e8a2b30d01649d1246cb7ae Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 15 Jun 2012 10:45:58 +0900 Subject: [PATCH 1/3] add memos --- app/assets/javascripts/books.js.coffee | 3 +++ app/controllers/books_controller.rb | 11 ++++------- app/helpers/books_helper.rb | 1 + app/models/book.rb | 15 +++++++++++---- app/views/books/_form.html.erb | 19 +++++++++++++++---- app/views/books/index.html.erb | 2 -- app/views/books/show.html.erb | 14 +++++++++----- app/views/layouts/application.html.erb | 2 +- config/routes.rb | 2 ++ db/migrate/20120526050801_create_books.rb | 2 +- db/schema.rb | 10 ++++++++-- spec/models/book_spec.rb | 8 +------- 12 files changed, 56 insertions(+), 33 deletions(-) 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..f1b6191 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,14 +11,20 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120526050801) do +ActiveRecord::Schema.define(:version => 20120610054557) do create_table "books", :force => true do |t| t.string "title" - t.text "memo" t.date "purchased_on" t.datetime "created_at", :null => false 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 From c3eaaf4ef8336ea7ed6881d448cdfd126204087c Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 15 Jun 2012 14:12:16 +0900 Subject: [PATCH 2/3] add new files --- app/assets/javascripts/books.js.coffee~ | 22 +++ app/assets/javascripts/memos.js.coffee | 3 + app/assets/stylesheets/memos.css.scss | 3 + app/controllers/books_controller.rb~ | 89 ++++++++++++ app/controllers/memos_controller.rb | 83 +++++++++++ app/helpers/books_helper.rb~ | 9 ++ app/helpers/memos_helper.rb | 2 + app/models/book.rb~ | 18 +++ app/models/memo.rb | 8 ++ app/models/memo.rb~ | 12 ++ app/views/books/_form.html.erb~ | 40 ++++++ app/views/books/_memo.html.erb | 10 ++ app/views/books/_memo.html.erb~ | 10 ++ app/views/books/index.html.erb~ | 25 ++++ app/views/books/show.html.erb~ | 25 ++++ app/views/layouts/application.html.erb~ | 14 ++ app/views/memos/_form.html.erb | 25 ++++ app/views/memos/edit.html.erb | 6 + app/views/memos/index.html.erb | 25 ++++ app/views/memos/new.html.erb | 5 + app/views/memos/show.html.erb | 15 ++ db/migrate/20120612123218_create_memos.rb | 10 ++ spec/controllers/memos_controller_spec.rb | 164 ++++++++++++++++++++++ spec/factories/memos.rb | 8 ++ spec/helpers/memos_helper_spec.rb | 15 ++ spec/models/book_spec.rb~ | 21 +++ spec/models/memo_spec.rb | 13 ++ spec/models/memo_spec.rb~ | 13 ++ spec/requests/memos_spec.rb | 11 ++ spec/routing/memos_routing_spec.rb | 35 +++++ spec/views/memos/edit.html.erb_spec.rb | 20 +++ spec/views/memos/index.html.erb_spec.rb | 23 +++ spec/views/memos/new.html.erb_spec.rb | 20 +++ spec/views/memos/show.html.erb_spec.rb | 17 +++ 34 files changed, 819 insertions(+) create mode 100644 app/assets/javascripts/books.js.coffee~ create mode 100644 app/assets/javascripts/memos.js.coffee create mode 100644 app/assets/stylesheets/memos.css.scss create mode 100644 app/controllers/books_controller.rb~ create mode 100644 app/controllers/memos_controller.rb create mode 100644 app/helpers/books_helper.rb~ create mode 100644 app/helpers/memos_helper.rb create mode 100644 app/models/book.rb~ create mode 100644 app/models/memo.rb create mode 100644 app/models/memo.rb~ create mode 100644 app/views/books/_form.html.erb~ create mode 100644 app/views/books/_memo.html.erb create mode 100644 app/views/books/_memo.html.erb~ create mode 100644 app/views/books/index.html.erb~ create mode 100644 app/views/books/show.html.erb~ create mode 100644 app/views/layouts/application.html.erb~ create mode 100644 app/views/memos/_form.html.erb create mode 100644 app/views/memos/edit.html.erb create mode 100644 app/views/memos/index.html.erb create mode 100644 app/views/memos/new.html.erb create mode 100644 app/views/memos/show.html.erb create mode 100644 db/migrate/20120612123218_create_memos.rb create mode 100644 spec/controllers/memos_controller_spec.rb create mode 100644 spec/factories/memos.rb create mode 100644 spec/helpers/memos_helper_spec.rb create mode 100644 spec/models/book_spec.rb~ create mode 100644 spec/models/memo_spec.rb create mode 100644 spec/models/memo_spec.rb~ create mode 100644 spec/requests/memos_spec.rb create mode 100644 spec/routing/memos_routing_spec.rb create mode 100644 spec/views/memos/edit.html.erb_spec.rb create mode 100644 spec/views/memos/index.html.erb_spec.rb create mode 100644 spec/views/memos/new.html.erb_spec.rb create mode 100644 spec/views/memos/show.html.erb_spec.rb diff --git a/app/assets/javascripts/books.js.coffee~ b/app/assets/javascripts/books.js.coffee~ new file mode 100644 index 0000000..74f97c4 --- /dev/null +++ b/app/assets/javascripts/books.js.coffee~ @@ -0,0 +1,22 @@ +# 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/ + +function addMemo() { + idNumber++; + //这里我们创建一个label控件 + label0 = document.createElement("label"); + label0.setAttribute("id","label"+idNumber); + //创建输入框 + var textField = document.createElement("input"); + textField.setAttribute("type","text"); + textField.setAttribute("name","memo"); + textField.setAttribute("id","memo"+idNumber); + label0.appendChild(textField); + + document.getElementById(id).appendChild(label0); + id= "memo"+idNumber; +} + + + diff --git a/app/assets/javascripts/memos.js.coffee b/app/assets/javascripts/memos.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/memos.js.coffee @@ -0,0 +1,3 @@ +# 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/assets/stylesheets/memos.css.scss b/app/assets/stylesheets/memos.css.scss new file mode 100644 index 0000000..3aaa23b --- /dev/null +++ b/app/assets/stylesheets/memos.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Memos controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/books_controller.rb~ b/app/controllers/books_controller.rb~ new file mode 100644 index 0000000..4821d70 --- /dev/null +++ b/app/controllers/books_controller.rb~ @@ -0,0 +1,89 @@ +class BooksController < ApplicationController + before_filter :find_book, :only => [:show, :destroy, :edit, :update] + + # GET /books + # GET /books.json + def index + @books = Book.all + + respond_to do |format| + format.html # index.html.erb + format.xml { render xml: @books } + format.json { render json: @books } + end + end + + # GET /books/1 + # GET /books/1.json + def show + respond_to do |format| + format.html # show.html.erb + format.xml { render xml: @book } + format.json { render json: @book } + end + end + + # GET /books/new + # GET /books/new.json + def new + @book = Book.new + @book.memos.build + end + + # GET /books/1/edit + def edit + end + + # POST /books + # POST /books.json + def create + @book = Book.new(params[:book]) + params[:memos].each_value do |memo| + @book.memos.build(memo) + end + respond_to do |format| + if @book.save + format.html { redirect_to @book, notice: 'Book was successfully created.' } + 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.xml { render xml: @book.errors, status: :unprocessable_entity } + format.json { render json: @book.errors, status: :unprocessable_entity } + end + end + end + + # PUT /books/1 + # PUT /books/1.json + def update + respond_to do |format| + if @book.update_attributes(params[:book]) + format.html { redirect_to @book, notice: 'Book was successfully updated.' } + format.xml { head :no_content } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.xml { render xml: @book.errors, status: :unprocessable_entity } + format.json { render json: @book.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /books/1 + # DELETE /books/1.json + def destroy + @book.destroy + + respond_to do |format| + format.html { redirect_to books_url } + format.xml { head :no_content } + format.json { head :no_content } + end + end + + private + def find_book + @book = Book.find(params[:id]) + end +end diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb new file mode 100644 index 0000000..74bda4c --- /dev/null +++ b/app/controllers/memos_controller.rb @@ -0,0 +1,83 @@ +class MemosController < ApplicationController + # GET /memos + # GET /memos.json + def index + @memos = Memo.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @memos } + end + end + + # GET /memos/1 + # GET /memos/1.json + def show + @memo = Memo.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @memo } + end + end + + # GET /memos/new + # GET /memos/new.json + def new + @memo = Memo.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @memo } + end + end + + # GET /memos/1/edit + def edit + @memo = Memo.find(params[:id]) + end + + # POST /memos + # POST /memos.json + def create + @memo = Memo.new(params[:memo]) + + respond_to do |format| + if @memo.save + format.html { redirect_to @memo, notice: 'Memo was successfully created.' } + format.json { render json: @memo, status: :created, location: @memo } + else + format.html { render action: "new" } + format.json { render json: @memo.errors, status: :unprocessable_entity } + end + end + end + + # PUT /memos/1 + # PUT /memos/1.json + def update + @memo = Memo.find(params[:id]) + + respond_to do |format| + if @memo.update_attributes(params[:memo]) + format.html { redirect_to @memo, notice: 'Memo was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @memo.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /memos/1 + # DELETE /memos/1.json + def destroy + @memo = Memo.find(params[:id]) + @memo.destroy + + respond_to do |format| + format.html { redirect_to memos_url } + format.json { head :no_content } + end + end +end diff --git a/app/helpers/books_helper.rb~ b/app/helpers/books_helper.rb~ new file mode 100644 index 0000000..3bd05de --- /dev/null +++ b/app/helpers/books_helper.rb~ @@ -0,0 +1,9 @@ +module BooksHelper + def add_task_link name + link_to_function name , nil do |page| + page.insert_html :bottom, :memos, :partial => 'memo', :object => Memo.new + end + end + + +end diff --git a/app/helpers/memos_helper.rb b/app/helpers/memos_helper.rb new file mode 100644 index 0000000..0e732a1 --- /dev/null +++ b/app/helpers/memos_helper.rb @@ -0,0 +1,2 @@ +module MemosHelper +end diff --git a/app/models/book.rb~ b/app/models/book.rb~ new file mode 100644 index 0000000..2167b7a --- /dev/null +++ b/app/models/book.rb~ @@ -0,0 +1,18 @@ +# encoding: UTF-8 +class Book < ActiveRecord::Base + + has_many :memos, :dependent => :destroy + accepts_nested_attributes_for :memos + attr_accessible :purchased_on, :title, :memos, :id + validates :title, :presence => true + + + + def new_memo_attributes=(memo_attributes) + memo_attributes.each do |attributes| + memos.build(attributes) + end + end + + +end diff --git a/app/models/memo.rb b/app/models/memo.rb new file mode 100644 index 0000000..4b0486f --- /dev/null +++ b/app/models/memo.rb @@ -0,0 +1,8 @@ +class Memo < ActiveRecord::Base +belongs_to :Book, :foreign_key => "book_id" + attr_accessible :book_id, :memo +validates_length_of :memo, :maximum => 100 + + +end + diff --git a/app/models/memo.rb~ b/app/models/memo.rb~ new file mode 100644 index 0000000..cb59bfe --- /dev/null +++ b/app/models/memo.rb~ @@ -0,0 +1,12 @@ +class Memo < ActiveRecord::Base +belongs_to :Book, :foreign_key => "book_id" + attr_accessible :book_id, :memo +validates_length_of :memo, :maximum => 100 + + before_create :total_books_count + + def total_books_count + self.memo += "【 累計冊数#{Book.count + 1} 】" + end +end + diff --git a/app/views/books/_form.html.erb~ b/app/views/books/_form.html.erb~ new file mode 100644 index 0000000..3ae743e --- /dev/null +++ b/app/views/books/_form.html.erb~ @@ -0,0 +1,40 @@ +<%= form_for(@book) do |f| %> + <% if @book.errors.any? %> +
+

<%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:

+ +
    + <% @book.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= f.label :title %>
+ <%= f.text_field :title %> +
+
+Memo:
+
+ + +
+ + +
+ <%= f.label :purchased_on %>
+ <%= f.date_select :purchased_on %> +
+
+ <%= f.submit %> +
+<% end %> + + + diff --git a/app/views/books/_memo.html.erb b/app/views/books/_memo.html.erb new file mode 100644 index 0000000..c182516 --- /dev/null +++ b/app/views/books/_memo.html.erb @@ -0,0 +1,10 @@ +
+<% new_or_existing = memo.new_record? ? 'new' : 'existing' %> +<% prefix = "book[#{new_or_existing}_memo_attributes][]" %> +<% fields_for prefix, memo do |memo_form| -%> +

+Memo: <%= memo_form.text_field :memo %> +<%= link_to_function "remove", "$(this).up('.task').remove()" %> +

+<% end -%> +
diff --git a/app/views/books/_memo.html.erb~ b/app/views/books/_memo.html.erb~ new file mode 100644 index 0000000..c182516 --- /dev/null +++ b/app/views/books/_memo.html.erb~ @@ -0,0 +1,10 @@ +
+<% new_or_existing = memo.new_record? ? 'new' : 'existing' %> +<% prefix = "book[#{new_or_existing}_memo_attributes][]" %> +<% fields_for prefix, memo do |memo_form| -%> +

+Memo: <%= memo_form.text_field :memo %> +<%= link_to_function "remove", "$(this).up('.task').remove()" %> +

+<% end -%> +
diff --git a/app/views/books/index.html.erb~ b/app/views/books/index.html.erb~ new file mode 100644 index 0000000..4768cfc --- /dev/null +++ b/app/views/books/index.html.erb~ @@ -0,0 +1,25 @@ +

Listing books

+ + + + + + + + + + + +<% @books.each do |book| %> + + + + + + +<% end %> +
TitleMemoPurchased on
<%= book.title %><%= book.purchased_on %><%= link_to 'Show', book %><%= link_to 'Destroy', book, confirm: 'Are you sure?', method :delete %>
+ +
+ +<%= link_to 'New Book', new_book_path %> diff --git a/app/views/books/show.html.erb~ b/app/views/books/show.html.erb~ new file mode 100644 index 0000000..e2c7407 --- /dev/null +++ b/app/views/books/show.html.erb~ @@ -0,0 +1,25 @@ +

<%= notice %>

+ +

+ Title: + <%= @book.title %> +

+

+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~ new file mode 100644 index 0000000..d2c06cf --- /dev/null +++ b/app/views/layouts/application.html.erb~ @@ -0,0 +1,14 @@ + + + + BookMemo2 + <%= stylesheet_link_tag "application", :media => "all" %> + <%= javascript_include_tag :defaults %> + <%= csrf_meta_tags %> + + + +<%= yield %> + + + diff --git a/app/views/memos/_form.html.erb b/app/views/memos/_form.html.erb new file mode 100644 index 0000000..9319431 --- /dev/null +++ b/app/views/memos/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for(@memo) do |f| %> + <% if @memo.errors.any? %> +
+

<%= pluralize(@memo.errors.count, "error") %> prohibited this memo from being saved:

+ +
    + <% @memo.errors.full_messages.each do |msg| %> +
  • <%= msg %>
  • + <% end %> +
+
+ <% end %> + +
+ <%= f.label :memo %>
+ <%= f.text_area :memo %> +
+
+ <%= f.label :book_id %>
+ <%= f.text_field :book_id %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/memos/edit.html.erb b/app/views/memos/edit.html.erb new file mode 100644 index 0000000..11d58b3 --- /dev/null +++ b/app/views/memos/edit.html.erb @@ -0,0 +1,6 @@ +

Editing memo

+ +<%= render 'form' %> + +<%= link_to 'Show', @memo %> | +<%= link_to 'Back', memos_path %> diff --git a/app/views/memos/index.html.erb b/app/views/memos/index.html.erb new file mode 100644 index 0000000..f8711ba --- /dev/null +++ b/app/views/memos/index.html.erb @@ -0,0 +1,25 @@ +

Listing memos

+ + + + + + + + + + +<% @memos.each do |memo| %> + + + + + + + +<% end %> +
MemoBook
<%= memo.memo %><%= memo.book_id %><%= link_to 'Show', memo %><%= link_to 'Edit', edit_memo_path(memo) %><%= link_to 'Destroy', memo, confirm: 'Are you sure?', method: :delete %>
+ +
+ +<%= link_to 'New Memo', new_memo_path %> diff --git a/app/views/memos/new.html.erb b/app/views/memos/new.html.erb new file mode 100644 index 0000000..cb923d7 --- /dev/null +++ b/app/views/memos/new.html.erb @@ -0,0 +1,5 @@ +

New memo

+ +<%= render 'form' %> + +<%= link_to 'Back', memos_path %> diff --git a/app/views/memos/show.html.erb b/app/views/memos/show.html.erb new file mode 100644 index 0000000..b41f3ae --- /dev/null +++ b/app/views/memos/show.html.erb @@ -0,0 +1,15 @@ +

<%= notice %>

+ +

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

+ +

+ Book: + <%= @memo.book_id %> +

+ + +<%= link_to 'Edit', edit_memo_path(@memo) %> | +<%= link_to 'Back', memos_path %> diff --git a/db/migrate/20120612123218_create_memos.rb b/db/migrate/20120612123218_create_memos.rb new file mode 100644 index 0000000..4da1e7c --- /dev/null +++ b/db/migrate/20120612123218_create_memos.rb @@ -0,0 +1,10 @@ +class CreateMemos < ActiveRecord::Migration + def change + create_table :memos do |t| + t.text :memo + t.string :book_id + + t.timestamps + end + end +end diff --git a/spec/controllers/memos_controller_spec.rb b/spec/controllers/memos_controller_spec.rb new file mode 100644 index 0000000..f3781ca --- /dev/null +++ b/spec/controllers/memos_controller_spec.rb @@ -0,0 +1,164 @@ +require 'spec_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +describe MemosController do + + # This should return the minimal set of attributes required to create a valid + # Memo. As you add validations to Memo, be sure to + # update the return value of this method accordingly. + def valid_attributes + {} + end + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # MemosController. Be sure to keep this updated too. + def valid_session + {} + end + + describe "GET index" do + it "assigns all memos as @memos" do + memo = Memo.create! valid_attributes + get :index, {}, valid_session + assigns(:memos).should eq([memo]) + end + end + + describe "GET show" do + it "assigns the requested memo as @memo" do + memo = Memo.create! valid_attributes + get :show, {:id => memo.to_param}, valid_session + assigns(:memo).should eq(memo) + end + end + + describe "GET new" do + it "assigns a new memo as @memo" do + get :new, {}, valid_session + assigns(:memo).should be_a_new(Memo) + end + end + + describe "GET edit" do + it "assigns the requested memo as @memo" do + memo = Memo.create! valid_attributes + get :edit, {:id => memo.to_param}, valid_session + assigns(:memo).should eq(memo) + end + end + + describe "POST create" do + describe "with valid params" do + it "creates a new Memo" do + expect { + post :create, {:memo => valid_attributes}, valid_session + }.to change(Memo, :count).by(1) + end + + it "assigns a newly created memo as @memo" do + post :create, {:memo => valid_attributes}, valid_session + assigns(:memo).should be_a(Memo) + assigns(:memo).should be_persisted + end + + it "redirects to the created memo" do + post :create, {:memo => valid_attributes}, valid_session + response.should redirect_to(Memo.last) + end + end + + describe "with invalid params" do + it "assigns a newly created but unsaved memo as @memo" do + # Trigger the behavior that occurs when invalid params are submitted + Memo.any_instance.stub(:save).and_return(false) + post :create, {:memo => {}}, valid_session + assigns(:memo).should be_a_new(Memo) + end + + it "re-renders the 'new' template" do + # Trigger the behavior that occurs when invalid params are submitted + Memo.any_instance.stub(:save).and_return(false) + post :create, {:memo => {}}, valid_session + response.should render_template("new") + end + end + end + + describe "PUT update" do + describe "with valid params" do + it "updates the requested memo" do + memo = Memo.create! valid_attributes + # Assuming there are no other memos in the database, this + # specifies that the Memo created on the previous line + # receives the :update_attributes message with whatever params are + # submitted in the request. + Memo.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, {:id => memo.to_param, :memo => {'these' => 'params'}}, valid_session + end + + it "assigns the requested memo as @memo" do + memo = Memo.create! valid_attributes + put :update, {:id => memo.to_param, :memo => valid_attributes}, valid_session + assigns(:memo).should eq(memo) + end + + it "redirects to the memo" do + memo = Memo.create! valid_attributes + put :update, {:id => memo.to_param, :memo => valid_attributes}, valid_session + response.should redirect_to(memo) + end + end + + describe "with invalid params" do + it "assigns the memo as @memo" do + memo = Memo.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Memo.any_instance.stub(:save).and_return(false) + put :update, {:id => memo.to_param, :memo => {}}, valid_session + assigns(:memo).should eq(memo) + end + + it "re-renders the 'edit' template" do + memo = Memo.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Memo.any_instance.stub(:save).and_return(false) + put :update, {:id => memo.to_param, :memo => {}}, valid_session + response.should render_template("edit") + end + end + end + + describe "DELETE destroy" do + it "destroys the requested memo" do + memo = Memo.create! valid_attributes + expect { + delete :destroy, {:id => memo.to_param}, valid_session + }.to change(Memo, :count).by(-1) + end + + it "redirects to the memos list" do + memo = Memo.create! valid_attributes + delete :destroy, {:id => memo.to_param}, valid_session + response.should redirect_to(memos_url) + end + end + +end diff --git a/spec/factories/memos.rb b/spec/factories/memos.rb new file mode 100644 index 0000000..92a691a --- /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 + memo "MyText" + book_id "MyString" + end +end diff --git a/spec/helpers/memos_helper_spec.rb b/spec/helpers/memos_helper_spec.rb new file mode 100644 index 0000000..48b8f0a --- /dev/null +++ b/spec/helpers/memos_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the MemosHelper. For example: +# +# describe MemosHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe MemosHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/book_spec.rb~ b/spec/models/book_spec.rb~ new file mode 100644 index 0000000..9e73370 --- /dev/null +++ b/spec/models/book_spec.rb~ @@ -0,0 +1,21 @@ +# coding:utf-8 +require 'spec_helper' + +describe Book do + describe 'validation' do + describe 'title' do + subject { Book.new.valid? } + it '未入力の場合はエラー' do + should be_false + end + end + end + + describe '#total_books_count' do + let(:book){ FactoryGirl.build :book } + before { book.save } + it "memoに累計冊数が表示されること" do + book.memos[].should include "【 累計冊数#{Book.count} 】" + end + end +end diff --git a/spec/models/memo_spec.rb b/spec/models/memo_spec.rb new file mode 100644 index 0000000..f8620a9 --- /dev/null +++ b/spec/models/memo_spec.rb @@ -0,0 +1,13 @@ +# coding:utf-8 +require 'spec_helper' + +describe Memo do + describe 'validation' do + let(:memo){FactoryGirl.build :memo} + before {memo.save} + it "メモの長さは100を超えるとエラー" do + memo.memo.size.should < 100 + + end + end +end diff --git a/spec/models/memo_spec.rb~ b/spec/models/memo_spec.rb~ new file mode 100644 index 0000000..f8620a9 --- /dev/null +++ b/spec/models/memo_spec.rb~ @@ -0,0 +1,13 @@ +# coding:utf-8 +require 'spec_helper' + +describe Memo do + describe 'validation' do + let(:memo){FactoryGirl.build :memo} + before {memo.save} + it "メモの長さは100を超えるとエラー" do + memo.memo.size.should < 100 + + end + end +end diff --git a/spec/requests/memos_spec.rb b/spec/requests/memos_spec.rb new file mode 100644 index 0000000..a086909 --- /dev/null +++ b/spec/requests/memos_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe "Memos" do + describe "GET /memos" do + it "works! (now write some real specs)" do + # Run the generator again with the --webrat flag if you want to use webrat methods/matchers + get memos_path + response.status.should be(200) + end + end +end diff --git a/spec/routing/memos_routing_spec.rb b/spec/routing/memos_routing_spec.rb new file mode 100644 index 0000000..b0f68f9 --- /dev/null +++ b/spec/routing/memos_routing_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe MemosController do + describe "routing" do + + it "routes to #index" do + get("/memos").should route_to("memos#index") + end + + it "routes to #new" do + get("/memos/new").should route_to("memos#new") + end + + it "routes to #show" do + get("/memos/1").should route_to("memos#show", :id => "1") + end + + it "routes to #edit" do + get("/memos/1/edit").should route_to("memos#edit", :id => "1") + end + + it "routes to #create" do + post("/memos").should route_to("memos#create") + end + + it "routes to #update" do + put("/memos/1").should route_to("memos#update", :id => "1") + end + + it "routes to #destroy" do + delete("/memos/1").should route_to("memos#destroy", :id => "1") + end + + end +end diff --git a/spec/views/memos/edit.html.erb_spec.rb b/spec/views/memos/edit.html.erb_spec.rb new file mode 100644 index 0000000..c6550c5 --- /dev/null +++ b/spec/views/memos/edit.html.erb_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe "memos/edit" do + before(:each) do + @memo = assign(:memo, stub_model(Memo, + :memo => "MyText", + :book_id => "MyString" + )) + end + + it "renders the edit memo form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => memos_path(@memo), :method => "post" do + assert_select "textarea#memo_memo", :name => "memo[memo]" + assert_select "input#memo_book_id", :name => "memo[book_id]" + end + end +end diff --git a/spec/views/memos/index.html.erb_spec.rb b/spec/views/memos/index.html.erb_spec.rb new file mode 100644 index 0000000..64775ed --- /dev/null +++ b/spec/views/memos/index.html.erb_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe "memos/index" do + before(:each) do + assign(:memos, [ + stub_model(Memo, + :memo => "MyText", + :book_id => "Book" + ), + stub_model(Memo, + :memo => "MyText", + :book_id => "Book" + ) + ]) + end + + it "renders a list of memos" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => "MyText".to_s, :count => 2 + assert_select "tr>td", :text => "Book".to_s, :count => 2 + end +end diff --git a/spec/views/memos/new.html.erb_spec.rb b/spec/views/memos/new.html.erb_spec.rb new file mode 100644 index 0000000..4f2c50f --- /dev/null +++ b/spec/views/memos/new.html.erb_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe "memos/new" do + before(:each) do + assign(:memo, stub_model(Memo, + :memo => "MyText", + :book_id => "MyString" + ).as_new_record) + end + + it "renders new memo form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => memos_path, :method => "post" do + assert_select "textarea#memo_memo", :name => "memo[memo]" + assert_select "input#memo_book_id", :name => "memo[book_id]" + end + end +end diff --git a/spec/views/memos/show.html.erb_spec.rb b/spec/views/memos/show.html.erb_spec.rb new file mode 100644 index 0000000..fd9450b --- /dev/null +++ b/spec/views/memos/show.html.erb_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe "memos/show" do + before(:each) do + @memo = assign(:memo, stub_model(Memo, + :memo => "MyText", + :book_id => "Book" + )) + end + + it "renders attributes in

" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + rendered.should match(/MyText/) + rendered.should match(/Book/) + end +end From fd4e5698a8eb583a2f0cd3f07d53ecc888a9cee5 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 15 Jun 2012 15:02:23 +0900 Subject: [PATCH 3/3] add request spec --- spec/requests/books_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 }