Skip to content

Commit

Permalink
Teach #move_to_child_with_index to accept :root as a target (#481)
Browse files Browse the repository at this point in the history
* teach #move_to_child_with_index to accept :root as a target.

* refactor out duplication within #move_to_child_with_index.

* teach #move_to_child_of to also accept :root and use it internally within #move_to_child_with_index.

* extract #roots which is a instance-level version of .roots, and use within #move_to_child_with_index.

* update CHANGELOG.

---------

Co-authored-by: Micah Geisel <[email protected]>
  • Loading branch information
botandrose and botandrose-machine authored Mar 8, 2024
1 parent 70cdc5e commit e2f88c1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Unreleased version
* Teach #move_to_child_of and #move_to_child_with_index to accept :root as a parameter [Micah Geisel](https://github.com/botandrose)
* Add #roots method [Micah Geisel](https://github.com/botandrose)

3.6.0
* Support Rails 7.1 [Harshal Bhakta](https://github.com/harshalbhakta)
Expand Down
19 changes: 12 additions & 7 deletions lib/awesome_nested_set/model/movable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,31 @@ def move_to_right_of(node)

# Move the node to the child of another node
def move_to_child_of(node)
move_to node, :child
if node == :root
move_to_root
else
move_to node, :child
end
end

# Move the node to the child of another node with specify index
def move_to_child_with_index(node, index)
if node.children.empty?
siblings = node == :root ? roots : node.children
if siblings.empty?
move_to_child_of(node)
elsif node.children.count == index
move_to_right_of(node.children.last)
elsif siblings.count == index
move_to_right_of(siblings.last)
else
my_position = node.children.to_a.index(self)
my_position = siblings.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])
move_to_right_of(siblings[index])
elsif my_position && my_position == index
# do nothing. already there.
else
move_to_left_of(node.children[index])
move_to_left_of(siblings[index])
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/awesome_nested_set/model/relatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def root
end
end

def roots
nested_set_scope.where(parent_id: nil)
end

protected

def compute_level
Expand Down
21 changes: 21 additions & 0 deletions spec/awesome_nested_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@
end
end

it "roots" do
expect(categories(:child_3).roots).to eq([categories(:top_level), categories(:top_level_2)])
end

it "root_class_method" do
expect(Category.root).to eq(categories(:top_level))
end
Expand Down Expand Up @@ -510,6 +514,16 @@
expect(Category.valid?).to be_truthy
end

it "move_to_child_of :root" do
categories(:child_2).move_to_child_of(:root)
expect(categories(:child_2).parent).to be_nil
expect(categories(:child_2).level).to eq(0)
expect(categories(:child_2_1).level).to eq(1)
expect(categories(:child_2).left).to eq(9)
expect(categories(:child_2).right).to eq(12)
expect(Category.valid?).to be_truthy
end

describe "#move_to_child_with_index" do
it "move to a node without child" do
categories(:child_1).move_to_child_with_index(categories(:child_3), 0)
Expand Down Expand Up @@ -562,6 +576,13 @@
expect(categories(:child_1).left).to eq(2)
expect(categories(:child_1).right).to eq(3)
end

it "move to top level with specified index" do
categories(:child_1).move_to_child_with_index(:root, 1)
expect(categories(:child_1).parent_id).to be_nil
expect(categories(:child_1).left).to eq(9)
expect(categories(:child_1).right).to eq(10)
end
end

it "move_to_child_of_appends_to_end" do
Expand Down

0 comments on commit e2f88c1

Please sign in to comment.