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

public included(base)
[View source]


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

public Array[ActiveRecord::Base] ancestors(force_reload = false)

Returns a list of ancestors this node belongs to

Meta Tags

Parameters:

[Boolean] force_reload (defaults to: false)

forces the list to be reloaded

Returns:

[Array[ActiveRecord::Base]]

a list of nodes that this node descends from

[View source]


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

public Range bounds

Meta Tags

Parameters:

Returns:

[Range]

the left to the right boundary of this node

[View source]


324
325
326
# File 'lib/eb_nested_set.rb', line 324

def bounds
  left..right
end

cache_children

public cache_children(*nodes)

Caches the nodes as this node’s children.

[View source]


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

public cache_nested_set

Caches the children of this node

[View source]


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

public cache_parent(parent)

Caches the node as this node’s parent.

[View source]


345
346
347
# File 'lib/eb_nested_set.rb', line 345

def cache_parent(parent) #:nodoc:
  self.parent = parent
end

descendant_of?

public Boolean descendant_of?(node)

Checks if this node is a descendant of node

Meta Tags

Parameters:

[ActiveRecord::Base] node

the node to check agains

Returns:

[Boolean]

whether this node is a descendant

[View source]


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

public Array[ActiveRecord::Base] descendants

Returns all nodes that descend from this node

Meta Tags

Parameters:

Returns:

[Array[ActiveRecord::Base]]
[View source]


251
252
253
# File 'lib/eb_nested_set.rb', line 251

def descendants
  base_class.descendants(self)
end

family

public Array[ActiveRecord::Base] family

Returns the node and all nodes that descend from it.

Meta Tags

Parameters:

Returns:

[Array[ActiveRecord::Base]]
[View source]


267
268
269
# File 'lib/eb_nested_set.rb', line 267

def family
  [self, *descendants]
end

family_ids

public Array[Integer] family_ids(force_reload = false)

Returns the ids of the node and all nodes that descend from it.

Meta Tags

Parameters:

Returns:

[Array[Integer]]
[View source]


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

public Array[ActiveRecord::Base] generation

Returns all nodes that share the same parent as this node.

Meta Tags

Parameters:

Returns:

[Array[ActiveRecord::Base]]
[View source]


293
294
295
# File 'lib/eb_nested_set.rb', line 293

def generation
  root? ? base_class.roots : parent.children
end

kin

public Array[ActiveRecord::Base] kin

Returns all nodes that descend from the same root node as this node

Meta Tags

Parameters:

Returns:

[Array[ActiveRecord::Base]]
[View source]


242
243
244
# File 'lib/eb_nested_set.rb', line 242

def kin
  patriarch.family
end

left

public Integer left

Meta Tags

Parameters:

Returns:

[Integer]

the left boundary of this node

[View source]


331
332
333
# File 'lib/eb_nested_set.rb', line 331

def left
  read_attribute(self.class.nested_set_options[:left])
end

level

public Integer level

Returns how deeply this node is nested, that is how many ancestors it has.

Meta Tags

Parameters:

Returns:

[Integer]

the number of ancestors of this node.

[View source]


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

public Array[ActiveRecord::Base] lineage(force_reload = false)

Returns a list of the node itself and all of its ancestors

Meta Tags

Parameters:

[Boolean] force_reload (defaults to: false)

forces the list to be reloaded

Returns:

[Array[ActiveRecord::Base]]

a list of nodes that this node descends from

[View source]


233
234
235
# File 'lib/eb_nested_set.rb', line 233

def lineage(force_reload=false)
  [self, *ancestors(force_reload)]
end

recalculate_nested_set

public recalculate_nested_set(left)

Rebuild this node’s childrens boundaries

[View source]


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

public Integer right

Meta Tags

Parameters:

Returns:

[Integer]

the right boundary of this node

[View source]


338
339
340
# File 'lib/eb_nested_set.rb', line 338

def right
  read_attribute(self.class.nested_set_options[:right])
end

root

public ActiveRecord::Base root(force_reload = nil)

Also known as: patriarch

Finds the root node that this node descends from

Meta Tags

Parameters:

[Boolean] force_reload (defaults to: nil)

forces the root node to be reloaded

Returns:

[ActiveRecord::Base]

node the root node this descends from

[View source]


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?

public Boolean root?

Checks if this root is a root node

Meta Tags

Parameters:

Returns:

[Boolean]

whether this node is a root node or not

[View source]


183
184
185
# File 'lib/eb_nested_set.rb', line 183

def root?
  not parent_id?
end

siblings

public Array[ActiveRecord::Base] siblings

Returns all nodes that are siblings of this node

Meta Tags

Parameters:

Returns:

[Array[ActiveRecord::Base]]
[View source]


302
303
304
# File 'lib/eb_nested_set.rb', line 302

def siblings
  generation - [self]
end