From ce9b943255b262bfd146dfadac925f9fd6884779 Mon Sep 17 00:00:00 2001 From: Roel van Dijk Date: Sat, 12 Nov 2011 22:12:47 +0100 Subject: [PATCH] Make :above and :below move items directly above and belove the passed item. --- lib/dm-is-list/is/list.rb | 4 ++-- spec/integration/list_spec.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/dm-is-list/is/list.rb b/lib/dm-is-list/is/list.rb index 990175c..dbfe53f 100644 --- a/lib/dm-is-list/is/list.rb +++ b/lib/dm-is-list/is/list.rb @@ -556,14 +556,14 @@ def move_without_saving(vector) # -- the same as self # -- already below self # -- higher up than self (lower number in list) - ( (self == object) or (object.position > self.position) ) ? self.position : object.position + ( (self == object) or (object.position == self.position + 1) ) ? self.position : object.position when :below # the object given, can either be: # -- the same as self # -- already above self # -- lower than self (higher number in list) - ( self == object or (object.position < self.position) ) ? self.position : object.position + 1 + ( self == object or (object.position == self.position - 1) ) ? self.position : object.position + 1 when :to # can only move within top and bottom positions of list diff --git a/spec/integration/list_spec.rb b/spec/integration/list_spec.rb index 5072c45..9a89ede 100644 --- a/spec/integration/list_spec.rb +++ b/spec/integration/list_spec.rb @@ -206,6 +206,13 @@ end end + it "should move item directly :above another in list even if it's already above it" do + DataMapper.repository(:default) do |repos| + Todo.get(2).move(:above => Todo.get(4) ).should == true + todo_list.should == [ [1, 1], [3, 2], [2, 3], [4, 4], [5, 5] ] + end + end + it "should NOT move item :above itself" do DataMapper.repository(:default) do |repos| Todo.get(1).move(:above => Todo.get(1) ).should == false @@ -238,6 +245,13 @@ end end + it "should move item directly :below another in list even if it's already below it" do + DataMapper.repository(:default) do |repos| + Todo.get(4).move(:below => Todo.get(2) ).should == true + todo_list.should == [ [1, 1], [2, 2], [4, 3], [3, 4], [5, 5] ] + end + end + it "should NOT move item :below itself" do DataMapper.repository(:default) do |repos| Todo.get(1).move(:below => Todo.get(1) ).should == false # is this logical ???