Module: TreeDelegation::InstanceMethods

Defined in:
lib/tree_delegation.rb

Instance Method Summary (collapse)

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?

Returns:

  • (Boolean)


65
66
67
# File 'lib/tree_delegation.rb', line 65

def has_hidden_ancestor?
  ancestors.hidden.any?
end

- (Boolean) is_descendant_of?(other)

Returns:

  • (Boolean)


89
90
91
# File 'lib/tree_delegation.rb', line 89

def is_descendant_of?(other)
  ancestors.include?(other)
end

- (Boolean) is_hidden?

Returns:

  • (Boolean)


61
62
63
# File 'lib/tree_delegation.rb', line 61

def is_hidden?
  hidden? or has_hidden_ancestor?
end

- (Boolean) leaf?

Returns:

  • (Boolean)


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

Raises:

  • (ActiveRecord::ActiveRecordError)


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

- (Object) root

Returns root



42
43
44
# File 'lib/tree_delegation.rb', line 42

def root
  super
end

- (Boolean) root?

Returns true if this is a root node.

Returns:

  • (Boolean)


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