Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for move_to_child_with_index method #268

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix for move_to_child_with_index method
when moving a node downward (but not to the bottom) within its current parent the
`move_to_child_with_index` method would insert the node in the wrong place.

This is because when moving a node downward the positioning needs to take into account the fact
that the moving node is being removed from its current position.

also, add code to handle a no-op call -- moving a node to the position it's already at -- w/o raising an error.
  • Loading branch information
jordan-brough committed Jun 25, 2014
commit e0283c7824f897ffadcb4a64ccb9a09770395b7c
12 changes: 11 additions & 1 deletion lib/awesome_nested_set/model/movable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ def move_to_child_with_index(node, index)
elsif node.children.count == index
move_to_right_of(node.children.last)
else
move_to_left_of(node.children[index])
my_position = node.children.index(self)
if my_position && my_position < index
# e.g. if self is at position 0 and we want to move self to position 1 then self
# needs to move to the *right* of the node at position 1. That's because the node
# that is currently at position 1 will be at position 0 after the move completes.
move_to_right_of(node.children[index])
elsif my_position && my_position == index
# do nothing. already there.
else
move_to_left_of(node.children[index])
end
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/awesome_nested_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,23 @@
categories(:child_2).right.should == 7
end

it "move downward within current parent" do
categories(:child_1).move_to_child_with_index(categories(:top_level), 1)
categories(:child_1).parent_id.should == categories(:top_level).id
categories(:child_1).left.should == 6
categories(:child_1).right.should == 7
categories(:child_2).reload
categories(:child_2).parent_id.should == categories(:top_level).id
categories(:child_2).left.should == 2
categories(:child_2).right.should == 5
end

it "move to the same position within current parent" do
categories(:child_1).move_to_child_with_index(categories(:top_level), 0)
categories(:child_1).parent_id.should == categories(:top_level).id
categories(:child_1).left.should == 2
categories(:child_1).right.should == 3
end
end

it "move_to_child_of_appends_to_end" do
Expand Down