OSDN Git Service

t#29020:modify lesser shift on story
authoryasushiito <yas@pen-chan.jp>
Wed, 18 Jul 2012 08:21:24 +0000 (17:21 +0900)
committeryasushiito <yas@pen-chan.jp>
Wed, 18 Jul 2012 08:21:24 +0000 (17:21 +0900)
app/models/story.rb
spec/models/story_spec.rb

index 3f58e68..539e0a9 100644 (file)
@@ -64,6 +64,7 @@ class Story < ActiveRecord::Base
   end
   
   def lesser_shift old_t
   end
   
   def lesser_shift old_t
+    self.t = 0 if self.t < 0
     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
   end
   
     Story.update_all('t = t + 1', ['comic_id = ? and (t >= ? and t < ?)', self.comic_id, self.t, old_t])
   end
   
index 93f3a63..34b1987 100644 (file)
@@ -680,6 +680,31 @@ describe Story do
         @storyc2.t.should eq 0
       end
     end
         @storyc2.t.should eq 0
       end
     end
+    #例外ケース。
+    #負のときは0として正常扱い
+    context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
+      before do
+        @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
+        @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
+        @ot = @story2.t
+        @story2.t = -1
+      end
+      it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
+        #移動させたい行はそのまま残る
+        @story2.lesser_shift @ot
+        l = Story.find(:all).map {|s| s.t }
+        l.sort.should eq [1, 1]
+      end
+      it '既存のt0を1にシフトしている' do
+        @story2.lesser_shift @ot
+        @story.reload
+        @story.t.should eq 1
+      end
+      it '既存のt1は0に補正されている' do
+        @story2.lesser_shift @ot
+        @story2.t.should eq 0
+      end
+    end
   end
   describe '大きい方に移動に於いて' do
     before do
   end
   describe '大きい方に移動に於いて' do
     before do
@@ -817,6 +842,31 @@ describe Story do
         @storyc2.t.should eq 0
       end
     end
         @storyc2.t.should eq 0
       end
     end
+    #例外ケース。
+    #max超えたときはmaxとして正常扱い
+    context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
+      before do
+        @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
+        @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
+        @ot = @story.t
+        @story.t = 2
+      end
+      it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
+        #移動させたい行はそのまま残る
+        @story.higher_shift @ot
+        l = Story.find(:all).map {|s| s.t }
+        l.sort.should eq [0, 0]
+      end
+      it '既存のt1を0にシフトしている' do
+        @story.higher_shift @ot
+        @story2.reload
+        @story2.t.should eq 0
+      end
+      it '既存のt0は1に補正されている' do
+        @story.higher_shift @ot
+        @story.t.should eq 1
+      end
+    end
   end
   describe '入れ替えに於いて' do
     before do
   end
   describe '入れ替えに於いて' do
     before do