Module: CollectiveIdea::Acts::NestedSet::Model::Movable
- Defined in:
- lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb
Instance Method Summary (collapse)
-
- (Object) find_left_neighbor(parent, order_attribute, ascending)
Find the node immediately to the left of this node.
-
- (Object) move_left
Shorthand method for finding the left sibling and moving to the left of it.
- - (Boolean) move_possible?(target)
-
- (Object) move_right
Shorthand method for finding the right sibling and moving to the right of it.
- - (Object) move_to(target, position)
-
- (Object) move_to_child_of(node)
Move the node to the child of another node.
-
- (Object) move_to_child_with_index(node, index)
Move the node to the child of another node with specify index.
-
- (Object) move_to_left_of(node)
Move the node to the left of another node.
-
- (Object) move_to_ordered_child_of(parent, order_attribute, ascending = true)
Order children in a nested set by an attribute Can order by any attribute class that uses the Comparable mixin, for example a string or integer Usage example when sorting categories alphabetically: @new_category.move_to_ordered_child_of(@root, “name”).
-
- (Object) move_to_right_of(node)
Move the node to the left of another node.
-
- (Object) move_to_root
Move the node to root nodes.
Instance Method Details
- (Object) find_left_neighbor(parent, order_attribute, ascending)
Find the node immediately to the left of this node.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 77 def find_left_neighbor(parent, order_attribute, ascending) left = nil parent.children.each do |n| if ascending left = n if n.send(order_attribute) < self.send(order_attribute) else left = n if n.send(order_attribute) > self.send(order_attribute) end end left end |
- (Object) move_left
Shorthand method for finding the left sibling and moving to the left of it.
18 19 20 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 18 def move_left move_to_left_of left_sibling end |
- (Boolean) move_possible?(target)
9 10 11 12 13 14 15 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 9 def move_possible?(target) self != target && # Can't target self same_scope?(target) && # can't be in different scopes # detect impossible move within_bounds?(target.left, target.left) && within_bounds?(target.right, target.right) end |
- (Object) move_right
Shorthand method for finding the right sibling and moving to the right of it.
23 24 25 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 23 def move_right move_to_right_of right_sibling end |
- (Object) move_to(target, position)
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 89 def move_to(target, position) prevent_unpersisted_move run_callbacks :move do in_tenacious_transaction do target = reload_target(target) self.reload_nested_set Move.new(target, position, self).move end after_move_to(target, position) end end |
- (Object) move_to_child_of(node)
Move the node to the child of another node
38 39 40 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 38 def move_to_child_of(node) move_to node, :child end |
- (Object) move_to_child_with_index(node, index)
Move the node to the child of another node with specify index
43 44 45 46 47 48 49 50 51 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 43 def move_to_child_with_index(node, index) if node.children.empty? move_to_child_of(node) elsif node.children.count == index move_to_right_of(node.children.last) else move_to_left_of(node.children[index]) end end |
- (Object) move_to_left_of(node)
Move the node to the left of another node
28 29 30 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 28 def move_to_left_of(node) move_to node, :left end |
- (Object) move_to_ordered_child_of(parent, order_attribute, ascending = true)
Order children in a nested set by an attribute Can order by any attribute class that uses the Comparable mixin, for example a string or integer Usage example when sorting categories alphabetically: @new_category.move_to_ordered_child_of(@root, “name”)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 61 def move_to_ordered_child_of(parent, order_attribute, ascending = true) self.move_to_root and return unless parent left_neighbor = find_left_neighbor(parent, order_attribute, ascending) self.move_to_child_of(parent) return unless parent.children.many? if left_neighbor self.move_to_right_of(left_neighbor) else # Self is the left most node. self.move_to_left_of(parent.children[0]) end end |
- (Object) move_to_right_of(node)
Move the node to the left of another node
33 34 35 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 33 def move_to_right_of(node) move_to node, :right end |
- (Object) move_to_root
Move the node to root nodes
54 55 56 |
# File 'lib/plugins/awesome_nested_set/lib/awesome_nested_set/model/movable.rb', line 54 def move_to_root move_to_right_of(root) end |