new code logic for saving tree.. should work fine even if the node positions fail
This commit is contained in:
1 parent
3db075b9d2
commit
b1867dd47a
4 files changed
+28
-22
No files matched your search
+15
-13
@@ -44,17 +44,23 @@ class Item
|
||||
a
|
||||
end
|
||||
|
||||
def shift_to(new_parent, position)
|
||||
def find_by_parent_and_position(parent, position)
|
||||
parent.children.detect{|child| child.position == position}
|
||||
end
|
||||
|
||||
|
||||
def shift_to(new_parent, position, next_sibling_id)
|
||||
position = position.to_i
|
||||
new_parent = Item.find(new_parent)
|
||||
current_position_sibling = find_by_parent_and_position(new_parent, position)
|
||||
if current_position_sibling
|
||||
if self.position > current_position_sibling.position
|
||||
self.move_above(current_position_sibling)
|
||||
else
|
||||
self.move_below(current_position_sibling)
|
||||
end
|
||||
elsif self.parent != new_parent
|
||||
|
||||
if next_sibling_id
|
||||
next_sibling = Item.find(next_sibling_id)
|
||||
self.move_above(next_sibling)
|
||||
else
|
||||
self.move_to_bottom
|
||||
end
|
||||
|
||||
if self.parent != new_parent
|
||||
self.parent = new_parent
|
||||
save
|
||||
end
|
||||
@@ -86,10 +92,6 @@ class Item
|
||||
end
|
||||
end
|
||||
|
||||
def find_by_parent_and_position(parent, position)
|
||||
parent.children.detect{|child| child.position == position}
|
||||
end
|
||||
|
||||
# Enable the validation for parent_id
|
||||
def validates_presence_of_parent_id?
|
||||
true
|
||||
|
||||
Reference in new issue
Block a user