Module: EvenBetterNestedSet::NestedSet
- Defined in:
- lib/eb_nested_set.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#ancestors(force_reload = false) ⇒ Array[ActiveRecord::Base]
Returns a list of ancestors this node belongs to.
-
#bounds ⇒ Range
The left to the right boundary of this node.
-
#cache_children(*nodes) ⇒ Object
Caches the nodes as this node’s children.
-
#cache_nested_set ⇒ Object
Caches the children of this node.
-
#cache_parent(parent) ⇒ Object
Caches the node as this node’s parent.
-
#descendant_of?(node) ⇒ Boolean
Checks if this node is a descendant of node.
-
#descendants ⇒ Array[ActiveRecord::Base]
Returns all nodes that descend from this node.
-
#family ⇒ Array[ActiveRecord::Base]
Returns the node and all nodes that descend from it.
-
#family_ids(force_reload = false) ⇒ Array[Integer]
Returns the ids of the node and all nodes that descend from it.
-
#generation ⇒ Array[ActiveRecord::Base]
Returns all nodes that share the same parent as this node.
-
#kin ⇒ Array[ActiveRecord::Base]
Returns all nodes that descend from the same root node as this node.
- #leaf? ⇒ Boolean
-
#left ⇒ Integer
The left boundary of this node.
-
#level ⇒ Integer
Returns how deeply this node is nested, that is how many ancestors it has.
-
#lineage(force_reload = false) ⇒ Array[ActiveRecord::Base]
Returns a list of the node itself and all of its ancestors.
-
#recalculate_nested_set(left) ⇒ Object
Rebuild this node’s childrens boundaries.
-
#right ⇒ Integer
The right boundary of this node.
-
#root(force_reload = nil) ⇒ ActiveRecord::Base
(also: #patriarch)
Finds the root node that this node descends from.
-
#root? ⇒ Boolean
Checks if this root is a root node.
-
#siblings ⇒ Array[ActiveRecord::Base]
Returns all nodes that are siblings of this node.
Class Method Details
.included(base) ⇒ Object
58 59 60 61 |
# File 'lib/eb_nested_set.rb', line 58 def self.included(base) super base.extend ClassMethods end |
Instance Method Details
#ancestors(force_reload = false) ⇒ Array[ActiveRecord::Base]
Returns a list of ancestors this node belongs to
222 223 224 225 226 227 228 |
# File 'lib/eb_nested_set.rb', line 222 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 ⇒ Range
Returns the left to the right boundary of this node.
327 328 329 |
# File 'lib/eb_nested_set.rb', line 327 def bounds left..right end |
#cache_children(*nodes) ⇒ Object
Caches the nodes as this node’s children.
355 356 357 358 |
# File 'lib/eb_nested_set.rb', line 355 def cache_children(*nodes) #:nodoc: @cached_children ||= [] children.target = @cached_children.push(*nodes) end |
#cache_nested_set ⇒ Object
Caches the children of this node
261 262 263 |
# File 'lib/eb_nested_set.rb', line 261 def cache_nested_set @cached_children || base_class.sort_nodes_to_nested_set(family) end |
#cache_parent(parent) ⇒ Object
Caches the node as this node’s parent.
348 349 350 |
# File 'lib/eb_nested_set.rb', line 348 def cache_parent(parent) #:nodoc: self.parent = parent end |
#descendant_of?(node) ⇒ Boolean
Checks if this node is a descendant of node
196 197 198 |
# File 'lib/eb_nested_set.rb', line 196 def descendant_of?(node) node.left < self.left && self.right < node.right end |
#descendants ⇒ Array[ActiveRecord::Base]
Returns all nodes that descend from this node
254 255 256 |
# File 'lib/eb_nested_set.rb', line 254 def descendants base_class.descendants(self) end |
#family ⇒ Array[ActiveRecord::Base]
Returns the node and all nodes that descend from it.
270 271 272 |
# File 'lib/eb_nested_set.rb', line 270 def family [self, *descendants] end |
#family_ids(force_reload = false) ⇒ Array[Integer]
Returns the ids of the node and all nodes that descend from it.
279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/eb_nested_set.rb', line 279 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 ⇒ Array[ActiveRecord::Base]
Returns all nodes that share the same parent as this node.
296 297 298 |
# File 'lib/eb_nested_set.rb', line 296 def generation root? ? base_class.roots : parent.children end |
#kin ⇒ Array[ActiveRecord::Base]
Returns all nodes that descend from the same root node as this node
245 246 247 |
# File 'lib/eb_nested_set.rb', line 245 def kin patriarch.family end |
#leaf? ⇒ Boolean
186 187 188 |
# File 'lib/eb_nested_set.rb', line 186 def leaf? (right - left) <= 1 end |
#left ⇒ Integer
Returns the left boundary of this node.
334 335 336 |
# File 'lib/eb_nested_set.rb', line 334 def left read_attribute(self.class.[:left]) end |
#level ⇒ Integer
Returns how deeply this node is nested, that is how many ancestors it has.
314 315 316 317 318 319 320 321 322 |
# File 'lib/eb_nested_set.rb', line 314 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(force_reload = false) ⇒ Array[ActiveRecord::Base]
Returns a list of the node itself and all of its ancestors
236 237 238 |
# File 'lib/eb_nested_set.rb', line 236 def lineage(force_reload=false) [self, *ancestors(force_reload)] end |
#recalculate_nested_set(left) ⇒ Object
Rebuild this node’s childrens boundaries
363 364 365 366 367 368 369 370 371 372 |
# File 'lib/eb_nested_set.rb', line 363 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 ⇒ Integer
Returns the right boundary of this node.
341 342 343 |
# File 'lib/eb_nested_set.rb', line 341 def right read_attribute(self.class.[:right]) end |
#root(force_reload = nil) ⇒ ActiveRecord::Base Also known as: patriarch
Finds the root node that this node descends from
206 207 208 209 210 211 212 |
# File 'lib/eb_nested_set.rb', line 206 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? ⇒ Boolean
Checks if this root is a root node
182 183 184 |
# File 'lib/eb_nested_set.rb', line 182 def root? not parent_id? end |
#siblings ⇒ Array[ActiveRecord::Base]
Returns all nodes that are siblings of this node
305 306 307 |
# File 'lib/eb_nested_set.rb', line 305 def siblings generation - [self] end |