Module: EvenBetterNestedSet::NestedSet
Public Visibility
Public Class Method Summary
| included(base) |
|---|
Public Instance Method Summary
| #ancestors(force_reload = false) |
Returns a list of ancestors this node belongs to. Returns: Array[ActiveRecord::Base] |
|---|---|
| #bounds |
Returns: Range |
| #cache_children(*nodes) |
Caches the nodes as this node’s children. |
| #cache_nested_set |
Caches the children of this node. |
| #cache_parent(parent) |
Caches the node as this node’s parent. |
| #descendant_of?(node) |
Checks if this node is a descendant of node. Returns: Boolean |
| #descendants |
Returns all nodes that descend from this node. Returns: Array[ActiveRecord::Base] |
| #family |
Returns the node and all nodes that descend from it. Returns: Array[ActiveRecord::Base] |
| #family_ids(force_reload = false) |
Returns the ids of the node and all nodes that descend from it. Returns: Array[Integer] |
| #generation |
Returns all nodes that share the same parent as this node. Returns: Array[ActiveRecord::Base] |
| #kin |
Returns all nodes that descend from the same root node as this node. Returns: Array[ActiveRecord::Base] |
| #left |
Returns: Integer |
| #level |
Returns how deeply this node is nested, that is how many ancestors it has. Returns: Integer |
| #lineage(force_reload = false) |
Returns a list of the node itself and all of its ancestors. Returns: Array[ActiveRecord::Base] |
| #recalculate_nested_set(left) |
Rebuild this node’s childrens boundaries. |
| #right |
Returns: Integer |
| #root(force_reload = nil) #patriarch |
Finds the root node that this node descends from. Returns: ActiveRecord::Base |
| #root? |
Checks if this root is a root node. Returns: Boolean |
| #siblings |
Returns all nodes that are siblings of this node. Returns: Array[ActiveRecord::Base] |
Public Class Method Details
included
59 60 61 62 |
# File 'lib/eb_nested_set.rb', line 59 def self.included(base) super base.extend ClassMethods end |
Public Instance Method Details
ancestors
Returns a list of ancestors this node belongs to
219 220 221 222 223 224 225 |
# File 'lib/eb_nested_set.rb', line 219 def ancestors(force_reload=false) @ancestors = nil if force_reload @ancestors ||= base_class.find( :all,:conditions => ["#{nested_set_column(:left)} < ? AND #{nested_set_column(:right)} > ?", left, right], :order => "#{nested_set_column(:left)} DESC" ) end |
bounds
324 325 326 |
# File 'lib/eb_nested_set.rb', line 324 def bounds left..right end |
cache_children
Caches the nodes as this node’s children.
352 353 354 355 |
# File 'lib/eb_nested_set.rb', line 352 def cache_children(*nodes) #:nodoc: @cached_children ||= [] children.target = @cached_children.push(*nodes) end |
cache_nested_set
Caches the children of this node
258 259 260 |
# File 'lib/eb_nested_set.rb', line 258 def cache_nested_set @cached_children || base_class.sort_nodes_to_nested_set(family) end |
cache_parent
Caches the node as this node’s parent.
345 346 347 |
# File 'lib/eb_nested_set.rb', line 345 def cache_parent(parent) #:nodoc: self.parent = parent end |
descendant_of?
Checks if this node is a descendant of node
193 194 195 |
# File 'lib/eb_nested_set.rb', line 193 def descendant_of?(node) node.left < self.left && self.right < node.right end |
descendants
Returns all nodes that descend from this node
251 252 253 |
# File 'lib/eb_nested_set.rb', line 251 def descendants base_class.descendants(self) end |
family
Returns the node and all nodes that descend from it.
267 268 269 |
# File 'lib/eb_nested_set.rb', line 267 def family [self, *descendants] end |
family_ids
Returns the ids of the node and all nodes that descend from it.
276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/eb_nested_set.rb', line 276 def family_ids(force_reload=false) return @family_ids unless @family_ids.nil? or force_reload transaction do reload_boundaries query = "SELECT id FROM #{self.class.quote_db_property(base_class.table_name)} " + "WHERE #{nested_set_column(:left)} >= #{left} AND #{nested_set_column(:right)} <= #{right} " + "ORDER BY #{nested_set_column(:left)}" @family_ids = base_class.connection.select_values(query).map(&:to_i) end end |
generation
Returns all nodes that share the same parent as this node.
293 294 295 |
# File 'lib/eb_nested_set.rb', line 293 def generation root? ? base_class.roots : parent.children end |
kin
Returns all nodes that descend from the same root node as this node
242 243 244 |
# File 'lib/eb_nested_set.rb', line 242 def kin patriarch.family end |
left
331 332 333 |
# File 'lib/eb_nested_set.rb', line 331 def left read_attribute(self.class.[:left]) end |
level
Returns how deeply this node is nested, that is how many ancestors it has.
311 312 313 314 315 316 317 318 319 |
# File 'lib/eb_nested_set.rb', line 311 def level if root? 0 elsif @ancestors @ancestors.size else base_class.count :conditions => ["#{nested_set_column(:left)} < ? AND #{nested_set_column(:right)} > ?", left, right] end end |
lineage
Returns a list of the node itself and all of its ancestors
233 234 235 |
# File 'lib/eb_nested_set.rb', line 233 def lineage(force_reload=false) [self, *ancestors(force_reload)] end |
recalculate_nested_set
Rebuild this node’s childrens boundaries
360 361 362 363 364 365 366 367 368 369 |
# File 'lib/eb_nested_set.rb', line 360 def recalculate_nested_set(left) #:nodoc: child_left = left + 1 children.each do |child| child_left = child.recalculate_nested_set(child_left) end set_boundaries(left, child_left) save_without_validation! right + 1 end |
right
338 339 340 |
# File 'lib/eb_nested_set.rb', line 338 def right read_attribute(self.class.[:right]) end |
root
Also known as: patriarch
Finds the root node that this node descends from
203 204 205 206 207 208 209 |
# File 'lib/eb_nested_set.rb', line 203 def root(force_reload=nil) @root = nil if force_reload @root ||= transaction do reload_boundaries base_class.roots.find(:first, :conditions => ["#{nested_set_column(:left)} <= ? AND #{nested_set_column(:right)} >= ?", left, right]) end end |
root?
Checks if this root is a root node
183 184 185 |
# File 'lib/eb_nested_set.rb', line 183 def root? not parent_id? end |
siblings
Returns all nodes that are siblings of this node
302 303 304 |
# File 'lib/eb_nested_set.rb', line 302 def siblings generation - [self] end |