Removing parent node but not child with awesome_nested_set?

Removing the parent node, but not the child ones.

All child nodes must be moved to the remote parent level.

How can I process this script using the awesome_nested_set plugin?

EDIT

Before removing a child

Id, Title, lft, rgt, parent_id

1, root, 7, 12, nil

2, child, 8, 11, 1

3, auxiliary child, 9, 10, 2

After deleting 2 entries

Id, Title, lft, rgt, parent_id

1, root, 7, 12, nil

3, auxiliary child, 9, 10, 1

I want to move the child child to the immediate parent of the remote object. Is this the correct result? Or should lft and rgt be changed after deletion?

+1
source share
2 answers

controller

  @node = Node.find(params[:id])
  @node.delete_node_keep_sub_nodes
  @node.reload
  @node.destroy

Model

  def delete_node_keep_sub_nodes
    if self.child?
      self.move_children_to_immediate_parent
    else
      self.move_children_to_root
    end
  end

  def move_children_to_immediate_parent
    node_immediate_children = self.children
    node_immediate_parent = self.parent
    node_immediate_children.each do |child|
      child.move_to_child_of(node_immediate_parent)
      node_immediate_parent.reload
    end
  end

  def move_children_to_root
    node_immediate_children = self.children
    node_immediate_children.each do |child|
      child.move_to_root
      child.reload
    end
  end
+1
source

Something like that:

  • node

@node = Node.find(params[:id])
@children = @node.children
@parent = @node.parent
@children.each{ |child| child.move_to_child_of @parent }
+1

Source: https://habr.com/ru/post/1793523/


All Articles