Deleting root node with orphan_strategy as adopt not setting immediate children node as root node #177

Open
amardaxini opened this Issue Feb 3, 2014 · 6 comments

Projects

None yet

4 participants

While deleting root node and orphan_strategy is adopt immediate child node ancestry value is "" because of that,Node.roots is not fetching correct value.

It can be solved by instance_methods.rb

elsif self.ancestry_base_class.orphan_strategy == :adopt
   descendants.each do |descendant|
      descendant.without_ancestry_callbacks do
          new_ancestry = descendant.ancestor_ids.delete_if { |x| x == self.id }.join("/")
         # check for empty string if it's then set to nil
          new_ancestry = nil if new_ancestry.empty?
         descendant.update_attribute descendant.class.ancestry_column, new_ancestry || nil
       end
    end

I get the same effect.

@amardaxini Did you create a monkeypatch or a forked branch with this fix?

@brandonjmckay i am not using delete feature for application ,that's why i haven't monkey patch.
But if you want i have added fix in my issue by setting nil if it's empty

I did the same in my Rails controller

Did it by overriding my destroy-method on the model:

  def destroy
    children = self.children.to_a
    super
    children.each do |child|
      child.reload
      child.ancestry = nil if child.ancestry.empty?
      child.save
    end
  end

A bugfix in the gem would be better, thought.

Contributor

The monkeypatch worked for me, so I've create a pull request (#200).
Thank s to @amardaxini, I think this change is harmless, so I dare ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment