Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

move :below fails #3

Open
FND opened this issue Oct 14, 2011 · 2 comments
Open

move :below fails #3

FND opened this issue Oct 14, 2011 · 2 comments

Comments

@FND
Copy link

FND commented Oct 14, 2011

test case: https://github.com/FND/dm-is-list/compare/below_bug

the first .move :below fails - yet it succeeds if first doing an arbitrary .move :above

Since the RSpec test case seems potentially confusing, here's a sample script that might make it clearer:

module DEBUG

  def self.info
    puts Animal.all(:order => :position.asc).map(&:id).join("  ")
  end

  def self.move(id, vector, ref_id)
    puts Animal.get!(id).move vector => Animal.get!(ref_id)
  end

end

5.times do |i|
  name = "animal#{i}"
  Animal.create
end

DEBUG::info # 1  2  3  4  5
DEBUG::move 5, :below, 1 # false
DEBUG::info # 1  2  3  4  5
DEBUG::move 5, :above, 1 # true
DEBUG::info # 5  1  2  3  4
DEBUG::move 5, :below, 1 # true
DEBUG::info # 1  5  2  3  4
@rdvdijk
Copy link

rdvdijk commented Nov 12, 2011

I've just noticed this as well. The bug in the code is fairly obvious. See here how the newpos (new position of the item to be moved) is determined:

          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

So: Keep the same position if the object is itself, or if the object is already anywhere below this position. I'll try and fix this and propose a pull request.

@rdvdijk
Copy link

rdvdijk commented Nov 12, 2011

I've fixed this and created a pull request for it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants