OSDN Git Service

t#29045:create overwrite on panel
authoryasushiito <yas@pen-chan.jp>
Thu, 19 Jul 2012 09:45:25 +0000 (18:45 +0900)
committeryasushiito <yas@pen-chan.jp>
Thu, 19 Jul 2012 09:45:25 +0000 (18:45 +0900)
app/controllers/panels_controller.rb
app/models/panel.rb
spec/controllers/panels_controller_spec.rb
spec/models/panel_spec.rb

index 6854b42..2dc0e67 100644 (file)
@@ -3,12 +3,6 @@ class PanelsController < ApplicationController
   before_filter :authenticate_user!, :only => [:index, :show, :new, :edit, :create, :update, :destroy]
   before_filter :authenticate_admin!, :only => [:list, :browse]
 
-  private
-  
-  def treat_param panel
-    panel.author_id = @author.id
-  end
-  
   public
   
   # GET /panels
@@ -64,7 +58,7 @@ class PanelsController < ApplicationController
 
   def new
     @panel = Panel.new
-    @panel.supply_default @author
+    @panel.supply_default
 
     respond_to do |format|
       format.html # new.html.erb
@@ -84,13 +78,14 @@ class PanelsController < ApplicationController
   # POST /panels
   # POST /panels.json
   def create
+    @panel = Panel.new
+    @panel.supply_default
     if params[:json]
       jsn = JSON.parse(params[:json])
     end
     @prm = params[:panel] || jsn
-    @panel = Panel.new(@prm)
-    treat_param @panel
-
+    @panel.attributes = @prm
+    @panel.overwrite @author
     respond_to do |format|
       if @panel.store
         format.html { redirect_to @panel, notice: 'Panel was successfully created.' }
@@ -110,8 +105,9 @@ class PanelsController < ApplicationController
       jsn = JSON.parse(params[:json])
     end
     @prm = params[:panel] || jsn
+    @panel.attributes = @prm
+    @panel.overwrite @author
     respond_to do |format|
-      @panel.attributes = @prm
       if @panel.store
         format.html { redirect_to @panel, notice: 'Panel was successfully updated.' }
         format.json { head :ok }
index ecaa027..079f1aa 100644 (file)
@@ -17,9 +17,12 @@ class Panel < ActiveRecord::Base
   validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
   validates :author_id, :presence => true, :numericality => true, :existence => true
   
-  def supply_default au
+  def supply_default
+    self.border = 2
+  end
+  
+  def overwrite au
     return false unless au
-    self.border = 0 if self.border.blank?
     self.author_id = au.id
   end
   
index 64a6abf..700b236 100644 (file)
@@ -315,6 +315,14 @@ describe PanelsController do
       end\r
     end\r
     context 'つつがなく終わるとき' do\r
+      it 'コマモデルにデフォルト値補充を依頼している' do\r
+        Panel.any_instance.should_receive(:supply_default).exactly(1)\r
+        post :create, :panel => @attr\r
+      end\r
+      it 'コマモデルに上書き補充を依頼している' do\r
+        Panel.any_instance.should_receive(:overwrite).exactly(1)\r
+        post :create, :panel => @attr\r
+      end\r
       it 'モデルに保存依頼する' do\r
         Panel.any_instance.should_receive(:store).exactly(1)\r
         post :create, :panel => @attr\r
@@ -502,6 +510,10 @@ describe PanelsController do
         Panel.should_receive(:show).exactly(1)\r
         put :update, :id => @panel.id, :panel => @attr\r
       end\r
+      it 'コマモデルに上書き補充を依頼している' do\r
+        Panel.any_instance.should_receive(:overwrite).exactly(1)\r
+        put :update, :id => @panel.id, :panel => @attr\r
+      end\r
       it 'モデルに保存依頼する' do\r
         Panel.any_instance.should_receive(:store).exactly(1)\r
         put :update, :id => @panel.id, :panel => @attr\r
index f0e02e9..ee50463 100644 (file)
@@ -1,8 +1,6 @@
 # -*- encoding: utf-8 -*-\r
 require 'spec_helper'\r
 #コマ\r
-#add publish flag\r
-#own visible list_json_opt\r
 describe Panel do\r
   before do\r
     Factory :admin\r
@@ -192,31 +190,25 @@ describe Panel do
     end\r
   end\r
   \r
-  describe 'ã\83\87ã\83¼ã\82¿補充に於いて' do\r
+  describe 'ã\83\87ã\83\95ã\82©ã\83«ã\83\88å\80¤補充に於いて' do\r
     before do\r
       @panel = Factory.build :panel, :author_id => @author.id\r
     end\r
-    context 'widthを補充' do\r
+    it 'borderは2を補充する' do\r
+      @panel.border = nil\r
+      @panel.supply_default\r
+      @panel.border.should eq 2\r
     end\r
-    context 'heightを補充' do\r
-    end\r
-    context 'borderを補充' do\r
-      it '空の時は0を補充する' do\r
-        @panel.border = nil\r
-        @panel.supply_default @author\r
-        @panel.border.should eq 0\r
-      end\r
-      it '空でない時は変化しない' do\r
-        @panel.border = 1\r
-        lambda {\r
-          @panel.supply_default @author\r
-        }.should_not change @panel, :border\r
-      end\r
+  end\r
+  \r
+  describe '上書き補充に於いて' do\r
+    before do\r
+      @panel = Factory.build :panel, :author_id => @author.id\r
     end\r
     context 'author_idを補充' do\r
       it 'ログイン中の作家idを補充する' do\r
         @panel.author_id = nil\r
-        @panel.supply_default @author\r
+        @panel.overwrite @author\r
         @panel.author_id.should eq @author.id\r
       end\r
     end\r