From: yasushiito Date: Wed, 18 Jul 2012 10:39:14 +0000 (+0900) Subject: t#29034:create overwrite X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=commitdiff_plain;h=6177371d4dd68313482ca4128593e86cb03472eb t#29034:create overwrite --- diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 530a3ebb..15d48cae 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -47,11 +47,10 @@ class StoriesController < ApplicationController # GET /stories/new.json def new @story = Story.new - @story.supply_default @author - @form_opt = {} + @story.supply_default respond_to do |format| format.html # new.html.erb - format.js { @form_opt = {:remote => true} ; render action: "new" } + format.js format.json { render json: @story } end end @@ -70,8 +69,9 @@ class StoriesController < ApplicationController # POST /stories.json def create @story = Story.new - @story.supply_default @author + @story.supply_default @story.attributes = params[:story] + @story.overwrite @author respond_to do |format| if @story.store @@ -88,9 +88,9 @@ class StoriesController < ApplicationController # PUT /stories/1.json def update @story = Story.show(params[:id], @author) - @story.author_id = @author.id ot = @story.t @story.attributes = params[:story] + @story.overwrite @author respond_to do |format| if @story.store ot format.html { redirect_to action: :show, id: @story.comic_id } diff --git a/app/models/story.rb b/app/models/story.rb index 539e0a94..7c58f08a 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -9,8 +9,11 @@ class Story < ActiveRecord::Base validates :author_id, :presence => true, :numericality => true, :existence => true validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0} - def supply_default au + def supply_default self.t = nil + end + + def overwrite au return false unless au self.author_id = au.id end diff --git a/app/views/stories/_editform.html.erb b/app/views/stories/_editform.html.erb new file mode 100644 index 00000000..9cc6dd41 --- /dev/null +++ b/app/views/stories/_editform.html.erb @@ -0,0 +1,28 @@ +<%= form_for(@story) do |f| %> + <% if @story.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= f.hidden_field :comic_id %> +
+
+ <%= f.number_field :t %> +
+
+ <%= f.hidden_field :panel_id %> +
+ +
+ <%= f.submit %> +
+<% end %> +<%= button_to 'Destroy', @story.panel, confirm: 'Are you sure?', method: :delete %> diff --git a/app/views/stories/_newform.html.erb b/app/views/stories/_newform.html.erb new file mode 100644 index 00000000..f7b57490 --- /dev/null +++ b/app/views/stories/_newform.html.erb @@ -0,0 +1,27 @@ +<%= form_for(@story) do |f| %> + <% if @story.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= f.number_field :comic_id %> +
+
+ <%= f.number_field :t %> +
+
+ <%= f.number_field :panel_id %> +
+ +
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/stories/edit.html.erb b/app/views/stories/edit.html.erb index 9cc6dd41..9f2b8426 100644 --- a/app/views/stories/edit.html.erb +++ b/app/views/stories/edit.html.erb @@ -1,28 +1 @@ -<%= form_for(@story) do |f| %> - <% if @story.errors.any? %> -
-

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

- - -
- <% end %> - -
- <%= f.hidden_field :comic_id %> -
-
- <%= f.number_field :t %> -
-
- <%= f.hidden_field :panel_id %> -
- -
- <%= f.submit %> -
-<% end %> -<%= button_to 'Destroy', @story.panel, confirm: 'Are you sure?', method: :delete %> +<%= render 'editform' %> diff --git a/app/views/stories/new.html.erb b/app/views/stories/new.html.erb index f7b57490..47cd6d96 100644 --- a/app/views/stories/new.html.erb +++ b/app/views/stories/new.html.erb @@ -1,27 +1 @@ -<%= form_for(@story) do |f| %> - <% if @story.errors.any? %> -
-

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

- - -
- <% end %> - -
- <%= f.number_field :comic_id %> -
-
- <%= f.number_field :t %> -
-
- <%= f.number_field :panel_id %> -
- -
- <%= f.submit %> -
-<% end %> +<%= render 'newform' %> diff --git a/spec/controllers/stories_controller_spec.rb b/spec/controllers/stories_controller_spec.rb index 33e15b09..13985d93 100644 --- a/spec/controllers/stories_controller_spec.rb +++ b/spec/controllers/stories_controller_spec.rb @@ -39,7 +39,7 @@ describe StoriesController do get :new assigns(:story).should be_a_new(Story) end - it 'コマモデルにデフォルト値補充を依頼している' do + it 'モデルにデフォルト値補充を依頼している' do Story.any_instance.should_receive(:supply_default).exactly(1) get :new end @@ -89,6 +89,19 @@ describe StoriesController do sign_in @user end context 'つつがなく終わるとき' do + it 'デフォルト値補充を依頼する' do + Story.any_instance.should_receive(:supply_default).exactly(1) + post :create, :story => @attr + end + it 'POSTデータから、カラム値を復元している' do + Story.any_instance.stub(:store).and_return(true) + Story.any_instance.should_receive(:attributes=).exactly(1) + post :create, :story => @attr + end + it '上書き補充を依頼する' do + Story.any_instance.should_receive(:overwrite).exactly(1) + post :create, :story => @attr + end it 'モデルに保存依頼する' do Story.any_instance.should_receive(:store).exactly(1) post :create, :story => @attr @@ -110,18 +123,6 @@ describe StoriesController do response.should redirect_to(:action => :show, :id => @attr[:comic_id]) end end - context 'js形式' do - it 'ステータスコード302 Foundを返す' do - Story.any_instance.stub(:store).and_return(true) - post :create, :story => @attr, :format => :js - response.status.should eq 302 - end - it 'コミックのストーリー表示へ遷移する' do -# Story.any_instance.stub(:store).and_return(true) - post :create, :story => @attr, :format => :js - response.should redirect_to(:action => :show, :id => @attr[:comic_id]) - end - end context 'json形式' do it 'ステータスコード200 OKを返す' do # Story.any_instance.stub(:store).and_return(true) @@ -135,7 +136,7 @@ describe StoriesController do it 'データがアレになっている' do post :create, :story => @attr, :format => :json json = JSON.parse response.body - json["t"].should eq @story.t + json["t"].should eq 0 end end end @@ -266,6 +267,15 @@ describe StoriesController do Story.should_receive(:show).exactly(1) put :update, :id => @story.id, :story => @attr end + it 'POSTデータから、カラム値を復元している' do + Story.any_instance.stub(:store).and_return(true) + Story.any_instance.should_receive(:attributes=).exactly(1) + put :update, :id => @story.id, :story => @attr + end + it '上書き補充を依頼する' do + Story.any_instance.should_receive(:overwrite).exactly(1) + put :update, :id => @story.id, :story => @attr + end it 'モデルに保存依頼する' do Story.any_instance.should_receive(:store).exactly(1) put :update, :id => @story.id, :story => @attr diff --git a/spec/models/story_spec.rb b/spec/models/story_spec.rb index 34b1987e..f4ae8052 100644 --- a/spec/models/story_spec.rb +++ b/spec/models/story_spec.rb @@ -107,7 +107,22 @@ describe Story do end end - describe 'データ補充に於いて' do + describe 'デフォルト値補充に於いて' do + before do + @comic = Factory :comic, :author_id => @author.id + @panel = Factory :panel, :author_id => @author.id + end + + #dbのデフォルト値が0だから明示的にnilにしないと追加ができない + it 'tをnilにする' do + @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id + @story.supply_default + @story.t.should be_nil + end + + end + + describe '上書き補充に於いて' do before do @comic = Factory :comic, :author_id => @author.id @panel = Factory :panel, :author_id => @author.id @@ -117,7 +132,7 @@ describe Story do it '問答無用でauthor_idを補充する' do @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id @story.author_id = nil - @story.supply_default @author + @story.overwrite @author @story.author_id.should eq @author.id end end