Module: CollectiveIdea::Acts::NestedSet::Model
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/awesome_nested_set/awesome_nested_set.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#ancestors ⇒ Object
Returns an array of all parents.
-
#child? ⇒ Boolean
Returns true is this is a child node.
-
#descendants ⇒ Object
Returns a set of all of its children and nested children.
- #is_ancestor_of?(other) ⇒ Boolean
- #is_descendant_of?(other) ⇒ Boolean
- #is_or_is_ancestor_of?(other) ⇒ Boolean
- #is_or_is_descendant_of?(other) ⇒ Boolean
-
#leaf? ⇒ Boolean
Returns true if this is the end of a branch.
-
#leaves ⇒ Object
Returns a set of all of its nested children which do not have children.
-
#left ⇒ Object
Value of the left column.
-
#left_sibling ⇒ Object
Find the first sibling to the left.
-
#level ⇒ Object
Returns the level of this object in the tree root level is 0.
-
#move_left ⇒ Object
Shorthand method for finding the left sibling and moving to the left of it.
- #move_possible?(target) ⇒ Boolean
-
#move_right ⇒ Object
Shorthand method for finding the right sibling and moving to the right of it.
-
#move_to_child_of(node) ⇒ Object
Move the node to the child of another node (you can pass id only).
-
#move_to_child_with_index(node, index) ⇒ Object
Move the node to the child of another node with specify index (you can pass id only).
-
#move_to_left_of(node) ⇒ Object
Move the node to the left of another node (you can pass id only).
-
#move_to_right_of(node) ⇒ Object
Move the node to the left of another node (you can pass id only).
-
#move_to_root ⇒ Object
Move the node to root nodes.
-
#parent_id ⇒ Object
Any instance method that returns a collection makes use of Rails 2.1’s named_scope (which is bundled for Rails 2.0), so it can be treated as a finder.
-
#right ⇒ Object
Value of the right column.
-
#right_sibling ⇒ Object
Find the first sibling to the right.
-
#root ⇒ Object
Returns root.
-
#root? ⇒ Boolean
Returns true if this is a root node.
-
#same_scope?(other) ⇒ Boolean
Check if other model is in the same scope.
-
#self_and_ancestors ⇒ Object
Returns the array of all parents and self.
-
#self_and_descendants ⇒ Object
Returns a set of itself and all of its nested children.
-
#self_and_siblings ⇒ Object
Returns the array of all children of the parent, including self.
-
#siblings ⇒ Object
Returns the array of all children of the parent, except self.
- #to_text ⇒ Object
Instance Method Details
#ancestors ⇒ Object
Returns an array of all parents
319 320 321 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 319 def ancestors without_self self_and_ancestors end |
#child? ⇒ Boolean
Returns true is this is a child node
294 295 296 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 294 def child? !root? end |
#descendants ⇒ Object
Returns a set of all of its children and nested children
353 354 355 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 353 def descendants without_self self_and_descendants end |
#is_ancestor_of?(other) ⇒ Boolean
365 366 367 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 365 def is_ancestor_of?(other) self.left < other.left && other.left < self.right && same_scope?(other) end |
#is_descendant_of?(other) ⇒ Boolean
357 358 359 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 357 def is_descendant_of?(other) other.left < self.left && self.left < other.right && same_scope?(other) end |
#is_or_is_ancestor_of?(other) ⇒ Boolean
369 370 371 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 369 def is_or_is_ancestor_of?(other) self.left <= other.left && other.left < self.right && same_scope?(other) end |
#is_or_is_descendant_of?(other) ⇒ Boolean
361 362 363 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 361 def is_or_is_descendant_of?(other) other.left <= self.left && self.left < other.right && same_scope?(other) end |
#leaf? ⇒ Boolean
Returns true if this is the end of a branch.
289 290 291 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 289 def leaf? persisted? && right.to_i - left.to_i == 1 end |
#leaves ⇒ Object
Returns a set of all of its nested children which do not have children
334 335 336 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 334 def leaves descendants.where("#{self.class.quoted_table_name}.#{quoted_right_column_name} - #{self.class.quoted_table_name}.#{quoted_left_column_name} = 1") end |
#left ⇒ Object
Value of the left column
274 275 276 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 274 def left self[left_column_name] end |
#left_sibling ⇒ Object
Find the first sibling to the left
381 382 383 384 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 381 def left_sibling siblings.where(["#{self.class.quoted_table_name}.#{quoted_left_column_name} < ?", left]). order("#{self.class.quoted_table_name}.#{quoted_left_column_name} DESC").last end |
#level ⇒ Object
Returns the level of this object in the tree root level is 0
340 341 342 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 340 def level parent_id.nil? ? 0 : ancestors.count end |
#move_left ⇒ Object
Shorthand method for finding the left sibling and moving to the left of it.
392 393 394 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 392 def move_left move_to_left_of left_sibling end |
#move_possible?(target) ⇒ Boolean
432 433 434 435 436 437 438 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 432 def move_possible?(target) self != target && # Can't target self same_scope?(target) && # can't be in different scopes # !(left..right).include?(target.left..target.right) # this needs tested more # detect impossible move !((left <= target.left && right >= target.left) or (left <= target.right && right >= target.right)) end |
#move_right ⇒ Object
Shorthand method for finding the right sibling and moving to the right of it.
397 398 399 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 397 def move_right move_to_right_of right_sibling end |
#move_to_child_of(node) ⇒ Object
Move the node to the child of another node (you can pass id only)
412 413 414 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 412 def move_to_child_of(node) move_to node, :child end |
#move_to_child_with_index(node, index) ⇒ Object
Move the node to the child of another node with specify index (you can pass id only)
417 418 419 420 421 422 423 424 425 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 417 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 |
#move_to_left_of(node) ⇒ Object
Move the node to the left of another node (you can pass id only)
402 403 404 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 402 def move_to_left_of(node) move_to node, :left end |
#move_to_right_of(node) ⇒ Object
Move the node to the left of another node (you can pass id only)
407 408 409 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 407 def move_to_right_of(node) move_to node, :right end |
#move_to_root ⇒ Object
Move the node to root nodes
428 429 430 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 428 def move_to_root move_to nil, :root end |
#parent_id ⇒ Object
Any instance method that returns a collection makes use of Rails 2.1’s named_scope (which is bundled for Rails 2.0), so it can be treated as a finder.
category.self_and_descendants.count
category.ancestors.find(:all, :conditions => "name like '%foo%'")
Value of the parent column
269 270 271 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 269 def parent_id self[parent_column_name] end |
#right ⇒ Object
Value of the right column
279 280 281 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 279 def right self[right_column_name] end |
#right_sibling ⇒ Object
Find the first sibling to the right
387 388 389 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 387 def right_sibling siblings.where(["#{self.class.quoted_table_name}.#{quoted_left_column_name} > ?", left]).first end |
#root ⇒ Object
Returns root
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 299 def root if persisted? self_and_ancestors.where(parent_column_name => nil).first else if parent_id && current_parent = nested_set_scope.find(parent_id) current_parent.root else self end end end |
#root? ⇒ Boolean
Returns true if this is a root node.
284 285 286 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 284 def root? parent_id.nil? end |
#same_scope?(other) ⇒ Boolean
Check if other model is in the same scope
374 375 376 377 378 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 374 def same_scope?(other) Array([:scope]).all? do |attr| self.send(attr) == other.send(attr) end end |
#self_and_ancestors ⇒ Object
Returns the array of all parents and self
312 313 314 315 316 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 312 def self_and_ancestors nested_set_scope.where([ "#{self.class.quoted_table_name}.#{quoted_left_column_name} <= ? AND #{self.class.quoted_table_name}.#{quoted_right_column_name} >= ?", left, right ]) end |
#self_and_descendants ⇒ Object
Returns a set of itself and all of its nested children
345 346 347 348 349 350 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 345 def self_and_descendants nested_set_scope.where([ "#{self.class.quoted_table_name}.#{quoted_left_column_name} >= ? AND #{self.class.quoted_table_name}.#{quoted_left_column_name} < ?", left, right # using _left_ for both sides here lets us benefit from an index on that column if one exists ]) end |
#self_and_siblings ⇒ Object
Returns the array of all children of the parent, including self
324 325 326 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 324 def self_and_siblings nested_set_scope.where(parent_column_name => parent_id) end |
#siblings ⇒ Object
Returns the array of all children of the parent, except self
329 330 331 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 329 def siblings without_self self_and_siblings end |
#to_text ⇒ Object
440 441 442 443 444 |
# File 'lib/awesome_nested_set/awesome_nested_set.rb', line 440 def to_text self_and_descendants.map do |node| "#{'*'*(node.level+1)} #{node.id} #{node.to_s} (#{node.parent_id}, #{node.left}, #{node.right})" end.join("\n") end |