From 19ccc03f0a7cc52356cdc12e23851d2b32ef4213 Mon Sep 17 00:00:00 2001 From: "seinan.shu" Date: Fri, 15 Jun 2012 15:31:06 +0900 Subject: [PATCH] commit --- .rvmrc | 1 - app/assets/javascripts/memos.js.coffee | 3 + app/assets/stylesheets/memos.css.scss | 3 + app/controllers/books_controller.rb | 3 + app/controllers/memos_controller.rb | 66 +++++++++ app/helpers/memos_helper.rb | 2 + app/models/book.rb | 1 + app/models/memo.rb | 7 + app/views/books/show.html.erb | 4 +- app/views/memos/_form.html.erb | 21 +++ app/views/memos/_index.html.erb | 8 ++ app/views/memos/edit.html.erb | 6 + app/views/memos/index.html.erb | 26 ++++ app/views/memos/new.html.erb | 5 + app/views/memos/show.html.erb | 15 ++ config/routes.rb | 4 +- db/migrate/20120614103230_create_memos.rb | 11 ++ db/schema.rb | 20 ++- spec/controllers/memos_controller_spec.rb | 164 ++++++++++++++++++++++ spec/factories/memos.rb | 8 ++ spec/helpers/memos_helper_spec.rb | 15 ++ spec/models/memo_spec.rb | 14 ++ spec/requests/books_spec.rb | 11 ++ spec/requests/memos_spec.rb | 42 ++++++ 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 +++ 29 files changed, 571 insertions(+), 4 deletions(-) delete mode 100644 .rvmrc create mode 100644 app/assets/javascripts/memos.js.coffee create mode 100644 app/assets/stylesheets/memos.css.scss create mode 100644 app/controllers/memos_controller.rb create mode 100644 app/helpers/memos_helper.rb create mode 100644 app/models/memo.rb create mode 100644 app/views/memos/_form.html.erb create mode 100644 app/views/memos/_index.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/20120614103230_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/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/.rvmrc b/.rvmrc deleted file mode 100644 index 9905a74..0000000 --- a/.rvmrc +++ /dev/null @@ -1 +0,0 @@ -rvm use 1.9.3@book_memo2 \ No newline at end of file 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 index d13ffd9..dfdbd69 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -16,6 +16,9 @@ def index # GET /books/1 # GET /books/1.json def show + @book = Book.find(params[:id]) + @memo = @book.memos.build + respond_to do |format| format.html # show.html.erb format.xml { render xml: @book } diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb new file mode 100644 index 0000000..5024a70 --- /dev/null +++ b/app/controllers/memos_controller.rb @@ -0,0 +1,66 @@ +class MemosController < ApplicationController + + before_filter :load_book + + def load_book + @book = Book.find(params[:book_id]) + end + + # GET /memos + # GET /memos.json + def index + @memos = @book.memos.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 + end + + # GET /memos/new + # GET /memos/new.json + def new + end + + # GET /memos/1/edit + def edit + end + + # POST /memos + # POST /memos.json + def create + @memo = @book.memos.build(params[:memo]) + + respond_to do |format| + if @memo.save + format.html { redirect_to @book, notice: 'Memo was successfully created.' } + format.json { render json: @memo, status: :created, location: @memo } + else + format.html { render 'books/show' } + format.json { render json: @memo.errors, status: :unprocessable_entity } + end + end + end + + # PUT /memos/1 + # PUT /memos/1.json + def update + end + + # DELETE /memos/1 + # DELETE /memos/1.json + def destroy + @memo = @book.memos.find(params[:id]) + @memo.destroy + + respond_to do |format| + format.html { redirect_to book_url(@book) } + format.json { head :no_content } + 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 index 95c42dd..d654b4c 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 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..8b5e0d2 --- /dev/null +++ b/app/models/memo.rb @@ -0,0 +1,7 @@ +class Memo < ActiveRecord::Base + belongs_to :book + attr_accessible :body + + 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 23005ab..ccc4056 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -15,6 +15,8 @@ <%= @book.purchased_on %>

- <%= link_to 'Edit', edit_book_path(@book) %> | <%= link_to 'Back', books_path %> +
+<%= render 'memos/index' %> +<%= render 'memos/form' %> diff --git a/app/views/memos/_form.html.erb b/app/views/memos/_form.html.erb new file mode 100644 index 0000000..9440697 --- /dev/null +++ b/app/views/memos/_form.html.erb @@ -0,0 +1,21 @@ +<%= form_for([@book, @memo]) do |f| %> + <% if @memo.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= f.label :body %>
+ <%= f.text_area :body %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/memos/_index.html.erb b/app/views/memos/_index.html.erb new file mode 100644 index 0000000..e5051d8 --- /dev/null +++ b/app/views/memos/_index.html.erb @@ -0,0 +1,8 @@ +

Memos

+ diff --git a/app/views/memos/edit.html.erb b/app/views/memos/edit.html.erb new file mode 100644 index 0000000..150397f --- /dev/null +++ b/app/views/memos/edit.html.erb @@ -0,0 +1,6 @@ +

Editing memo

+ +<%= render 'form' %> + +<%= link_to 'Show', [@book, @memo] %> | +<%= link_to 'Back', book_memos_path %> diff --git a/app/views/memos/index.html.erb b/app/views/memos/index.html.erb new file mode 100644 index 0000000..0094c60 --- /dev/null +++ b/app/views/memos/index.html.erb @@ -0,0 +1,26 @@ +

Listing memos

+ + + + + + + + + + +<% @memos.each do |memo| %> + + + + + + + +<% end %> +
BookBody
<%= memo.book %><%= memo.body %><%= link_to 'Show', book_memo_path(@book, memo) %><%= link_to 'Edit', edit_book_memo_path(@book, memo) %><%= link_to 'Destroy', book_memo_path(@book, memo), confirm: 'Are you sure?', method: :delete %>
+ +
+ +<%= link_to 'Back', books_path %> | +<%= link_to 'New Memo', new_book_memo_path %> diff --git a/app/views/memos/new.html.erb b/app/views/memos/new.html.erb new file mode 100644 index 0000000..d40502b --- /dev/null +++ b/app/views/memos/new.html.erb @@ -0,0 +1,5 @@ +

New memo

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

<%= notice %>

+ +

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

+ +

+ Body: + <%= @memo.body %> +

+ + +<%= link_to 'Edit', edit_book_memo_path %> | +<%= link_to 'Back', book_memos_path %> 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/20120614103230_create_memos.rb b/db/migrate/20120614103230_create_memos.rb new file mode 100644 index 0000000..67feaef --- /dev/null +++ b/db/migrate/20120614103230_create_memos.rb @@ -0,0 +1,11 @@ +class CreateMemos < ActiveRecord::Migration + def change + create_table :memos do |t| + t.references :book + t.text :body + + t.timestamps + end + add_index :memos, :book_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 5fc61c2..59a7659 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 => 20120615004432) do create_table "books", :force => true do |t| t.string "title" @@ -21,4 +21,22 @@ t.datetime "updated_at", :null => false end + create_table "comments", :force => true do |t| + t.integer "book_id" + t.text "memo" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "comments", ["book_id"], :name => "index_comments_on_book_id" + + 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 + + add_index "memos", ["book_id"], :name => "index_memos_on_book_id" + 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..e8daacb --- /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 {Factory.create(:book)} + body "create by request spec" + 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/memo_spec.rb b/spec/models/memo_spec.rb new file mode 100644 index 0000000..b5efca7 --- /dev/null +++ b/spec/models/memo_spec.rb @@ -0,0 +1,14 @@ +# coding:utf-8 +require 'spec_helper' + +describe Memo do + describe 'validation' do + describe 'body' do + subject { Memo.new.valid? } + it 'blank and more than 100 chars will error' do + should be_false + end + end + end +end + diff --git a/spec/requests/books_spec.rb b/spec/requests/books_spec.rb index 37522b7..103e009 100644 --- a/spec/requests/books_spec.rb +++ b/spec/requests/books_spec.rb @@ -10,6 +10,17 @@ end end +=begin + describe '/books/:id' do + let!(:book){ FactoryGirl.create :book } + subject { page } + before { visit "/books/#{book.id}" } + it "タイトルが設定されていること" do + find("#book_title").value.should == book.title + end + end +=end + describe '/books/:id/edit' do let!(:book){ FactoryGirl.create :book } subject { page } diff --git a/spec/requests/memos_spec.rb b/spec/requests/memos_spec.rb new file mode 100644 index 0000000..3f3934b --- /dev/null +++ b/spec/requests/memos_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe "Memos" do +=begin + 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 + + describe '/books/:book_id/memos' do + let!(:memo){ FactoryGirl.create :memo } + subject {page} + + before {visit "/books/#{memo.book.id}"} + + context 'create body' do + let(:create_body) {'create by request spec'} + before do + fill_in 'memo[body]', with: create_body + click_on 'Create Memo' + end + + it 'page not move' do + current_path.should == book_path(memo.book) + end + + it 'success to create memo' do + should have_content memo.reload.body + memo.reload.body.should == create_body + end + + 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..d01a69e --- /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, + :book => nil, + :body => "MyText" + )) + 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 "input#memo_book", :name => "memo[book]" + assert_select "textarea#memo_body", :name => "memo[body]" + 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..ea0820f --- /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, + :book => nil, + :body => "MyText" + ), + stub_model(Memo, + :book => nil, + :body => "MyText" + ) + ]) + 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 => nil.to_s, :count => 2 + assert_select "tr>td", :text => "MyText".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..fada45f --- /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, + :book => nil, + :body => "MyText" + ).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 "input#memo_book", :name => "memo[book]" + assert_select "textarea#memo_body", :name => "memo[body]" + 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..4bfed0d --- /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, + :book => nil, + :body => "MyText" + )) + 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(//) + rendered.should match(/MyText/) + end +end