From daa489af843a81170969989aae6754ba1be41a25 Mon Sep 17 00:00:00 2001 From: Takuya Miyamoto Date: Fri, 15 Jun 2012 13:19:20 +0900 Subject: [PATCH 1/4] Add auto test env --- .rspec | 3 ++- Gemfile | 5 +++++ Gemfile.lock | 18 +++++++++++++++++ Guardfile | 31 ++++++++++++++++++++++++++++ spec/spec_helper.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 Guardfile diff --git a/.rspec b/.rspec index 89b89ff..9356066 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1,3 @@ +--drb --colour ---format=d \ No newline at end of file +--format=d diff --git a/Gemfile b/Gemfile index d643000..692d717 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,11 @@ group :test, :development do gem "rspec-rails", "~> 2.0" gem 'capybara' gem 'launchy' + gem 'guard', '0.10.0' + gem 'guard-rspec' + gem 'guard-spork' + gem 'libnotify' + gem 'rb-inotify' end gem 'pry-rails' gem 'factory_girl_rails' diff --git a/Gemfile.lock b/Gemfile.lock index 0f24858..9a3c56c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -58,6 +58,14 @@ GEM factory_girl (~> 3.3.0) railties (>= 3.0.0) ffi (1.0.11) + guard (0.10.0) + ffi (>= 0.5.0) + thor (~> 0.14.6) + guard-rspec (0.7.3) + guard (>= 0.10.0) + guard-spork (0.8.0) + guard (>= 0.10.0) + spork (>= 0.8.4) hike (1.2.1) i18n (0.6.0) journey (1.0.3) @@ -67,6 +75,8 @@ GEM json (1.7.3) launchy (2.1.0) addressable (~> 2.2.6) + libnotify (0.7.2) + ffi (~> 1.0.0) libwebsocket (0.1.3) addressable mail (2.4.4) @@ -107,6 +117,8 @@ GEM rdoc (~> 3.4) thor (~> 0.14.6) rake (0.9.2.2) + rb-inotify (0.8.8) + ffi (>= 0.5.0) rdoc (3.12) json (~> 1.4) rspec (2.10.0) @@ -135,6 +147,7 @@ GEM multi_json (~> 1.0) rubyzip slop (2.4.4) + spork (0.9.2) sprockets (2.1.3) hike (~> 1.2) rack (~> 1.0) @@ -159,10 +172,15 @@ DEPENDENCIES capybara coffee-rails (~> 3.2.1) factory_girl_rails + guard (= 0.10.0) + guard-rspec + guard-spork jquery-rails launchy + libnotify pry-rails rails (= 3.2.3) + rb-inotify rspec-rails (~> 2.0) sass-rails (~> 3.2.3) sqlite3 diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..e5a04fc --- /dev/null +++ b/Guardfile @@ -0,0 +1,31 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do + watch('config/application.rb') + watch('config/environment.rb') + watch(%r{^config/environments/.+\.rb$}) + watch(%r{^config/initializers/.+\.rb$}) + watch('Gemfile') + watch('Gemfile.lock') + watch('spec/spec_helper.rb') { :rspec } + watch('test/test_helper.rb') { :test_unit } + watch(%r{features/support/}) { :cucumber } +end + +guard 'rspec', :version => 2 do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch('spec/spec_helper.rb') { "spec" } + + # Rails example + watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" } + watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] } + watch(%r{^spec/support/(.+)\.rb$}) { "spec" } + watch('config/routes.rb') { "spec/routing" } + watch('app/controllers/application_controller.rb') { "spec/controllers" } + # Capybara request specs + watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" } +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e66d980..ef4d0af 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,52 @@ +require 'rubygems' +require 'spork' +#uncomment the following line to use spork with the debugger +#require 'spork/ext/ruby-debug' + +Spork.prefork do + # Loading more in this block will cause your tests to run faster. However, + # if you change any configuration or code from libraries loaded here, you'll + # need to restart spork for it take effect. + +end + +Spork.each_run do + # This code will be run each time you run your specs. + +end + +# --- Instructions --- +# Sort the contents of this file into a Spork.prefork and a Spork.each_run +# block. +# +# The Spork.prefork block is run only once when the spork server is started. +# You typically want to place most of your (slow) initializer code in here, in +# particular, require'ing any 3rd-party gems that you don't normally modify +# during development. +# +# The Spork.each_run block is run each time you run your specs. In case you +# need to load files that tend to change during development, require them here. +# With Rails, your application modules are loaded automatically, so sometimes +# this block can remain empty. +# +# Note: You can modify files loaded *from* the Spork.each_run block without +# restarting the spork server. However, this file itself will not be reloaded, +# so if you change any of the code inside the each_run block, you still need to +# restart the server. In general, if you have non-trivial code in this file, +# it's advisable to move it into a separate file so you can easily edit it +# without restarting spork. (For example, with RSpec, you could move +# non-trivial code into a file spec/support/my_helper.rb, making sure that the +# spec/support/* files are require'd from inside the each_run block.) +# +# Any code that is left outside the two blocks will be run during preforking +# *and* during each_run -- that's probably not what you want. +# +# These instructions should self-destruct in 10 seconds. If they don't, feel +# free to delete them. + + + + # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) From 8e8bfe137e723397520d9c4c27e02e9c70b871e2 Mon Sep 17 00:00:00 2001 From: Takuya Miyamoto Date: Fri, 15 Jun 2012 16:25:59 +0900 Subject: [PATCH 2/4] Add scaffold stuffs for memo --- app/assets/javascripts/memos.js.coffee | 3 + app/assets/stylesheets/memos.css.scss | 3 + app/controllers/memos_controller.rb | 30 ++++ app/helpers/memos_helper.rb | 2 + app/models/book.rb | 2 + app/models/memo.rb | 5 + app/views/memos/_form.html.erb | 21 +++ app/views/memos/edit.html.erb | 6 + app/views/memos/index.html.erb | 23 +++ app/views/memos/new.html.erb | 5 + app/views/memos/show.html.erb | 10 ++ config/routes.rb | 2 + db/migrate/20120615071239_create_memos.rb | 10 ++ db/schema.rb | 9 +- spec/controllers/memos_controller_spec.rb | 164 ++++++++++++++++++++++ spec/factories/memos.rb | 7 + spec/helpers/memos_helper_spec.rb | 15 ++ spec/models/memo_spec.rb | 5 + spec/requests/memos_spec.rb | 11 ++ spec/routing/memos_routing_spec.rb | 35 +++++ spec/views/memos/edit.html.erb_spec.rb | 18 +++ spec/views/memos/index.html.erb_spec.rb | 20 +++ spec/views/memos/new.html.erb_spec.rb | 18 +++ spec/views/memos/show.html.erb_spec.rb | 15 ++ 24 files changed, 438 insertions(+), 1 deletion(-) 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/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/20120615071239_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/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..2b49bc5 --- /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/memos_controller.rb b/app/controllers/memos_controller.rb new file mode 100644 index 0000000..9842acb --- /dev/null +++ b/app/controllers/memos_controller.rb @@ -0,0 +1,30 @@ +class MemosController < ApplicationController + + # 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 + + # 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/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..83833bb 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -5,6 +5,8 @@ class Book < ActiveRecord::Base before_create :total_books_count + has_many :memo + def total_books_count self.memo += "【 累計冊数#{Book.count + 1} 】" end diff --git a/app/models/memo.rb b/app/models/memo.rb new file mode 100644 index 0000000..28a3396 --- /dev/null +++ b/app/models/memo.rb @@ -0,0 +1,5 @@ +class Memo < ActiveRecord::Base + attr_accessible :value + + belongs_to :book +end diff --git a/app/views/memos/_form.html.erb b/app/views/memos/_form.html.erb new file mode 100644 index 0000000..68659e3 --- /dev/null +++ b/app/views/memos/_form.html.erb @@ -0,0 +1,21 @@ +<%= 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 :value %>
+ <%= f.text_field :value %> +
+
+ <%= 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..3816817 --- /dev/null +++ b/app/views/memos/index.html.erb @@ -0,0 +1,23 @@ +

Listing memos

+ + + + + + + + + +<% @memos.each do |memo| %> + + + + + + +<% end %> +
Value
<%= memo.value %><%= 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..521372e --- /dev/null +++ b/app/views/memos/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +

+ Value: + <%= @memo.value %> +

+ + +<%= link_to 'Edit', edit_memo_path(@memo) %> | +<%= link_to 'Back', memos_path %> diff --git a/config/routes.rb b/config/routes.rb index f3d38ab..0accc5e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ BookMemo2::Application.routes.draw do + resources :memos, only: [:create, :destroy] + resources :books # The priority is based upon order of creation: diff --git a/db/migrate/20120615071239_create_memos.rb b/db/migrate/20120615071239_create_memos.rb new file mode 100644 index 0000000..66f1ff3 --- /dev/null +++ b/db/migrate/20120615071239_create_memos.rb @@ -0,0 +1,10 @@ +class CreateMemos < ActiveRecord::Migration + def change + create_table :memos do |t| + t.integer :book_id + t.string :value + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5fc61c2..73c1ebc 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 => 20120615071239) 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.string "value" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + 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..ee57997 --- /dev/null +++ b/spec/factories/memos.rb @@ -0,0 +1,7 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :memo do + value "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/memo_spec.rb b/spec/models/memo_spec.rb new file mode 100644 index 0000000..c4d756c --- /dev/null +++ b/spec/models/memo_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Memo do + pending "add some examples to (or delete) #{__FILE__}" +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..880b8b3 --- /dev/null +++ b/spec/views/memos/edit.html.erb_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe "memos/edit" do + before(:each) do + @memo = assign(:memo, stub_model(Memo, + :value => "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 "input#memo_value", :name => "memo[value]" + 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..1812284 --- /dev/null +++ b/spec/views/memos/index.html.erb_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe "memos/index" do + before(:each) do + assign(:memos, [ + stub_model(Memo, + :value => "Value" + ), + stub_model(Memo, + :value => "Value" + ) + ]) + 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 => "Value".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..01a420c --- /dev/null +++ b/spec/views/memos/new.html.erb_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe "memos/new" do + before(:each) do + assign(:memo, stub_model(Memo, + :value => "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 "input#memo_value", :name => "memo[value]" + 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..3e0e8dc --- /dev/null +++ b/spec/views/memos/show.html.erb_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe "memos/show" do + before(:each) do + @memo = assign(:memo, stub_model(Memo, + :value => "Value" + )) + 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(/Value/) + end +end From c42dd223253c99949284aedbe96e5fb8b4b5aa39 Mon Sep 17 00:00:00 2001 From: Takuya Miyamoto Date: Fri, 15 Jun 2012 17:16:08 +0900 Subject: [PATCH 3/4] Implmeneted has_many associations --- app/controllers/books_controller.rb | 3 +++ app/models/book.rb | 4 +++- app/models/memo.rb | 2 +- app/views/books/_form.html.erb | 12 +++++++++--- app/views/books/_memo_view.html.erb | 1 + app/views/books/show.html.erb | 2 +- db/migrate/20120526050801_create_books.rb | 1 - db/schema.rb | 1 - 8 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 app/views/books/_memo_view.html.erb diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index d13ffd9..0f320cc 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -42,7 +42,10 @@ def edit # POST /books # POST /books.json def create + memo = params[:book].delete(:memo) + @book = Book.new(params[:book]) + #@book.memo << Memo.new({value: memo}) respond_to do |format| if @book.save diff --git a/app/models/book.rb b/app/models/book.rb index 83833bb..29b2bd6 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -8,6 +8,8 @@ class Book < ActiveRecord::Base has_many :memo def total_books_count - self.memo += "【 累計冊数#{Book.count + 1} 】" + self.memo.each do |m| + m.value += "【 累計冊数#{Book.count + 1} 】" + end end end diff --git a/app/models/memo.rb b/app/models/memo.rb index 28a3396..572ab8b 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -1,5 +1,5 @@ class Memo < ActiveRecord::Base - attr_accessible :value + attr_accessible :value,:book_id belongs_to :book end diff --git a/app/views/books/_form.html.erb b/app/views/books/_form.html.erb index da45ee0..00b654d 100644 --- a/app/views/books/_form.html.erb +++ b/app/views/books/_form.html.erb @@ -15,9 +15,15 @@ <%= f.label :title %>
<%= f.text_field :title %> -

- <%= f.label :memo %>
- <%= f.text_area :memo %> + +
+ <% @book.memo.each do |m| %> + <%= fields_for m do |mm| %> +
+ <%= mm.text_area :value %> +
+ <% end %> + <% end %>
<%= f.label :purchased_on %>
diff --git a/app/views/books/_memo_view.html.erb b/app/views/books/_memo_view.html.erb new file mode 100644 index 0000000..33fd9bb --- /dev/null +++ b/app/views/books/_memo_view.html.erb @@ -0,0 +1 @@ +<%= memo%>
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 23005ab..60a6ae8 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -7,7 +7,7 @@

Memo: - <%= @book.memo %> + <%= render partial: 'memo_view', colleciton: @book.memo, as: :memo %>

diff --git a/db/migrate/20120526050801_create_books.rb b/db/migrate/20120526050801_create_books.rb index b04e895..921ed00 100644 --- a/db/migrate/20120526050801_create_books.rb +++ b/db/migrate/20120526050801_create_books.rb @@ -2,7 +2,6 @@ class CreateBooks < ActiveRecord::Migration def change create_table :books do |t| t.string :title - t.text :memo t.date :purchased_on t.timestamps diff --git a/db/schema.rb b/db/schema.rb index 73c1ebc..364f324 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,7 +15,6 @@ 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 From 25c144c7da3e2bc0c21ddea5c64ea2a16594fb98 Mon Sep 17 00:00:00 2001 From: tmiyamon Date: Mon, 18 Jun 2012 00:40:39 +0900 Subject: [PATCH 4/4] Add the spec case of book creation with memo --- app/assets/javascripts/books.js.coffee | 11 ++ app/controllers/books_controller.rb | 3 - app/models/book.rb | 7 +- app/views/books/_form.html.erb | 2 +- app/views/books/_memo_view.html.erb | 2 +- app/views/books/index.html.erb | 2 +- app/views/books/show.html.erb | 4 +- app/views/layouts/application.html.erb | 2 +- spec/controllers/books_controller_spec.rb | 20 +-- spec/controllers/memos_controller_spec.rb | 164 ---------------------- spec/factories/books.rb | 12 +- spec/factories/memos.rb | 7 - spec/models/book_spec.rb | 2 + spec/requests/books_spec.rb | 21 +++ spec/requests/memos_spec.rb | 11 -- spec/routing/memos_routing_spec.rb | 35 ----- spec/views/books/edit.html.erb_spec.rb | 16 +-- spec/views/books/index.html.erb_spec.rb | 13 +- spec/views/books/new.html.erb_spec.rb | 5 +- spec/views/books/show.html.erb_spec.rb | 17 +-- spec/views/memos/edit.html.erb_spec.rb | 18 --- spec/views/memos/index.html.erb_spec.rb | 20 --- spec/views/memos/new.html.erb_spec.rb | 18 --- spec/views/memos/show.html.erb_spec.rb | 15 -- 24 files changed, 93 insertions(+), 334 deletions(-) delete mode 100644 spec/controllers/memos_controller_spec.rb delete mode 100644 spec/factories/memos.rb delete mode 100644 spec/requests/memos_spec.rb delete mode 100644 spec/routing/memos_routing_spec.rb delete mode 100644 spec/views/memos/edit.html.erb_spec.rb delete mode 100644 spec/views/memos/index.html.erb_spec.rb delete mode 100644 spec/views/memos/new.html.erb_spec.rb delete 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 index 7615679..a56ff9c 100644 --- a/app/assets/javascripts/books.js.coffee +++ b/app/assets/javascripts/books.js.coffee @@ -1,3 +1,14 @@ # 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/ + +count = 0 +$("#add-memo").on("click", () -> + $('#memo').append($('

').addClass('field')).append($('