From: yasushiito Date: Wed, 18 Jul 2012 08:21:24 +0000 (+0900) Subject: t#29020:modify lesser shift on story X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=commitdiff_plain;h=ef944d064265adf1f4c47038efc9333d46ed22e5 t#29020:modify lesser shift on story --- diff --git a/app/models/story.rb b/app/models/story.rb index 3f58e680..539e0a94 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -64,6 +64,7 @@ class Story < ActiveRecord::Base 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 diff --git a/spec/models/story_spec.rb b/spec/models/story_spec.rb index 93f3a632..34b1987e 100644 --- a/spec/models/story_spec.rb +++ b/spec/models/story_spec.rb @@ -680,6 +680,31 @@ describe Story do @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 @@ -817,6 +842,31 @@ describe Story do @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