Module: TreeDelegation::InstanceMethods
- Defined in:
- lib/tree_delegation.rb
Instance Method Summary (collapse)
-
- (Object) ancestors(*args)
Returns an array of all parents.
-
- (Object) children
Returns a set of only this entry's immediate children.
-
- (Object) descendants(*args)
Returns a set of all of its children and nested children.
- - (Boolean) has_hidden_ancestor?
- - (Boolean) is_descendant_of?(other)
- - (Boolean) is_hidden?
- - (Boolean) leaf?
-
- (Object) left_sibling
Find the first sibling to the left.
-
- (Object) move_left
Shorthand method for finding the left sibling and moving to the left of it.
-
- (Object) move_right
Shorthand method for finding the right sibling and moving to the right of it.
-
- (Object) move_to(target, position)
Move node to left, right or child of target node.
-
- (Object) move_to_child_of(node)
Move the node to the child of another node (you can pass id only).
-
- (Object) move_to_left_of(node)
Move the node to the left of another node (you can pass id only).
-
- (Object) move_to_right_of(node)
Move the node to the left of another node (you can pass id only).
-
- (Object) parent
Returns the immediate parent.
- - (Object) parent_id
-
- (Object) parent_should_allow_type
checks wether the content type is valid as a child of the parent.
-
- (Object) reorder_children(*ids)
Reorders the nodes children by the order the of their ids given.
-
- (Object) right_sibling
Find the first sibling to the right.
-
- (Object) root
Returns root.
-
- (Boolean) root?
Returns true if this is a root node.
-
- (Object) self_and_ancestors
Returns the array of all parents and self.
-
- (Object) self_and_descendants
Returns a set of itself and all of its nested children.
-
- (Object) siblings
Returns the array of all children of the parent, except self.
Instance Method Details
- (Object) ancestors(*args)
Returns an array of all parents
57 58 59 |
# File 'lib/tree_delegation.rb', line 57 def ancestors(*args) super end |
- (Object) children
Returns a set of only this entry's immediate children
85 86 87 |
# File 'lib/tree_delegation.rb', line 85 def children super.sorted_by_position end |
- (Object) descendants(*args)
Returns a set of all of its children and nested children
80 81 82 |
# File 'lib/tree_delegation.rb', line 80 def descendants(*args) super end |
- (Boolean) has_hidden_ancestor?
65 66 67 |
# File 'lib/tree_delegation.rb', line 65 def has_hidden_ancestor? ancestors.hidden.any? end |
- (Boolean) is_descendant_of?(other)
89 90 91 |
# File 'lib/tree_delegation.rb', line 89 def is_descendant_of?(other) ancestors.include?(other) end |
- (Boolean) is_hidden?
61 62 63 |
# File 'lib/tree_delegation.rb', line 61 def is_hidden? hidden? or has_hidden_ancestor? end |
- (Boolean) leaf?
37 38 39 |
# File 'lib/tree_delegation.rb', line 37 def leaf? is_childless? end |
- (Object) left_sibling
Find the first sibling to the left
94 95 96 |
# File 'lib/tree_delegation.rb', line 94 def left_sibling higher_item end |
- (Object) move_left
Shorthand method for finding the left sibling and moving to the left of it.
104 105 106 |
# File 'lib/tree_delegation.rb', line 104 def move_left move_higher end |
- (Object) move_right
Shorthand method for finding the right sibling and moving to the right of it.
109 110 111 |
# File 'lib/tree_delegation.rb', line 109 def move_right move_lower end |
- (Object) move_to(target, position)
Move node to left, right or child of target node
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/tree_delegation.rb', line 129 def move_to(target, position) raise ActiveRecord::ActiveRecordError, "You cannot move a new node" if self.new_record? if position == :child self.parent = target else self.parent = target.parent end if save case position when :left insert_at!(target.position) when :right insert_at!(target.position + 1) when :child move_to_bottom! end else raise ActiveRecord::ActiveRecordError, "Move failed" end end |
- (Object) move_to_child_of(node)
Move the node to the child of another node (you can pass id only)
124 125 126 |
# File 'lib/tree_delegation.rb', line 124 def move_to_child_of(node) move_to node, :child end |
- (Object) move_to_left_of(node)
Move the node to the left of another node (you can pass id only)
114 115 116 |
# File 'lib/tree_delegation.rb', line 114 def move_to_left_of(node) move_to node, :left end |
- (Object) move_to_right_of(node)
Move the node to the left of another node (you can pass id only)
119 120 121 |
# File 'lib/tree_delegation.rb', line 119 def move_to_right_of(node) move_to node, :right end |
- (Object) parent
Returns the immediate parent
47 48 49 |
# File 'lib/tree_delegation.rb', line 47 def parent super end |
- (Object) parent_id
28 29 30 |
# File 'lib/tree_delegation.rb', line 28 def parent_id super end |
- (Object) parent_should_allow_type
checks wether the content type is valid as a child of the parent
164 165 166 167 168 |
# File 'lib/tree_delegation.rb', line 164 def parent_should_allow_type unless self.parent.nil? || content.own_content_class.valid_parent_class?(self.parent.content_class) errors.add_to_base "'#{self.parent.content_class.human_name}' #{I18n.t('tree_delegation.doesnt_accept')} '#{content.own_content_class.human_name}' #{:type}." end end |
- (Object) reorder_children(*ids)
Reorders the nodes children by the order the of their ids given
153 154 155 156 157 158 159 160 161 |
# File 'lib/tree_delegation.rb', line 153 def reorder_children(*ids) transaction do ordered_ids = ids.flatten.uniq ordered_ids.each do |child_id| position = ordered_ids.index(child_id) + 1 children.find(child_id).insert_at!(position) end end end |
- (Object) right_sibling
Find the first sibling to the right
99 100 101 |
# File 'lib/tree_delegation.rb', line 99 def right_sibling lower_item end |
- (Boolean) root?
Returns true if this is a root node.
33 34 35 |
# File 'lib/tree_delegation.rb', line 33 def root? is_root? end |
- (Object) self_and_ancestors
Returns the array of all parents and self
52 53 54 |
# File 'lib/tree_delegation.rb', line 52 def self_and_ancestors path end |
- (Object) self_and_descendants
Returns a set of itself and all of its nested children
75 76 77 |
# File 'lib/tree_delegation.rb', line 75 def self_and_descendants subtree end |
- (Object) siblings
Returns the array of all children of the parent, except self
70 71 72 |
# File 'lib/tree_delegation.rb', line 70 def siblings super end |