OSDN Git Service

t#29034:create overwrite
authoryasushiito <yas@pen-chan.jp>
Wed, 18 Jul 2012 10:39:14 +0000 (19:39 +0900)
committeryasushiito <yas@pen-chan.jp>
Wed, 18 Jul 2012 10:39:14 +0000 (19:39 +0900)
app/controllers/stories_controller.rb
app/models/story.rb
app/views/stories/_editform.html.erb [new file with mode: 0644]
app/views/stories/_newform.html.erb [new file with mode: 0644]
app/views/stories/edit.html.erb
app/views/stories/new.html.erb
spec/controllers/stories_controller_spec.rb
spec/models/story_spec.rb

index 530a3eb..15d48ca 100644 (file)
@@ -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 }
index 539e0a9..7c58f08 100644 (file)
@@ -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 (file)
index 0000000..9cc6dd4
--- /dev/null
@@ -0,0 +1,28 @@
+<%= form_for(@story) do |f| %>
+  <% if @story.errors.any? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@story.errors.count, "error") %> prohibited this comic from being saved:</h2>
+
+      <ul>
+      <% @story.errors.full_messages.each do |msg| %>
+        <li><%= msg %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.hidden_field :comic_id %>
+  </div>
+  <div class="field">
+    <%= f.number_field :t %>
+  </div>
+  <div class="field">
+    <%= f.hidden_field :panel_id %>
+  </div>
+
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% 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 (file)
index 0000000..f7b5749
--- /dev/null
@@ -0,0 +1,27 @@
+<%= form_for(@story) do |f| %>
+  <% if @story.errors.any? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@story.errors.count, "error") %> prohibited this comic from being saved:</h2>
+
+      <ul>
+      <% @story.errors.full_messages.each do |msg| %>
+        <li><%= msg %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.number_field :comic_id %>
+  </div>
+  <div class="field">
+    <%= f.number_field :t %>
+  </div>
+  <div class="field">
+    <%= f.number_field :panel_id %>
+  </div>
+
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>
index 9cc6dd4..9f2b842 100644 (file)
@@ -1,28 +1 @@
-<%= form_for(@story) do |f| %>
-  <% if @story.errors.any? %>
-    <div id="error_explanation">
-      <h2><%= pluralize(@story.errors.count, "error") %> prohibited this comic from being saved:</h2>
-
-      <ul>
-      <% @story.errors.full_messages.each do |msg| %>
-        <li><%= msg %></li>
-      <% end %>
-      </ul>
-    </div>
-  <% end %>
-
-  <div class="field">
-    <%= f.hidden_field :comic_id %>
-  </div>
-  <div class="field">
-    <%= f.number_field :t %>
-  </div>
-  <div class="field">
-    <%= f.hidden_field :panel_id %>
-  </div>
-
-  <div class="actions">
-    <%= f.submit %>
-  </div>
-<% end %>
-<%= button_to 'Destroy', @story.panel, confirm: 'Are you sure?', method: :delete %>
+<%= render 'editform' %>
index f7b5749..47cd6d9 100644 (file)
@@ -1,27 +1 @@
-<%= form_for(@story) do |f| %>
-  <% if @story.errors.any? %>
-    <div id="error_explanation">
-      <h2><%= pluralize(@story.errors.count, "error") %> prohibited this comic from being saved:</h2>
-
-      <ul>
-      <% @story.errors.full_messages.each do |msg| %>
-        <li><%= msg %></li>
-      <% end %>
-      </ul>
-    </div>
-  <% end %>
-
-  <div class="field">
-    <%= f.number_field :comic_id %>
-  </div>
-  <div class="field">
-    <%= f.number_field :t %>
-  </div>
-  <div class="field">
-    <%= f.number_field :panel_id %>
-  </div>
-
-  <div class="actions">
-    <%= f.submit %>
-  </div>
-<% end %>
+<%= render 'newform' %>
index 33e15b0..13985d9 100644 (file)
@@ -39,7 +39,7 @@ describe StoriesController do
         get :new
         assigns(:story).should be_a_new(Story)
       end
-      it 'ã\82³ã\83\9eã\83¢ã\83\87ã\83«ã\81«ã\83\87ã\83\95ã\82©ã\83«ã\83\88å\80¤è£\9cå\85\85ã\82\92ä¾\9dé ¼ã\81\97ã\81¦ã\81\84ã\82\8b' 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
index 34b1987..f4ae805 100644 (file)
@@ -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