Module: EvenBetterNestedSet::NestedSet

Defined in:
lib/eb_nested_set.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • force_reload (Boolean) (defaults to: false)

    forces the list to be reloaded

Returns:

  • (Array[ActiveRecord::Base])

    a list of nodes that this node descends from



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

#boundsRange

Returns the left to the right boundary of this node.

Returns:

  • (Range)

    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_setObject

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

Parameters:

  • node (ActiveRecord::Base)

    the node to check agains

Returns:

  • (Boolean)

    whether this node is a descendant



196
197
198
# File 'lib/eb_nested_set.rb', line 196

def descendant_of?(node)
  node.left < self.left && self.right < node.right
end

#descendantsArray[ActiveRecord::Base]

Returns all nodes that descend from this node

Returns:

  • (Array[ActiveRecord::Base])


254
255
256
# File 'lib/eb_nested_set.rb', line 254

def descendants
  base_class.descendants(self)
end

#familyArray[ActiveRecord::Base]

Returns the node and all nodes that descend from it.

Returns:

  • (Array[ActiveRecord::Base])


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.

Returns:

  • (Array[Integer])


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

#generationArray[ActiveRecord::Base]

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

Returns:

  • (Array[ActiveRecord::Base])


296
297
298
# File 'lib/eb_nested_set.rb', line 296

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

#kinArray[ActiveRecord::Base]

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

Returns:

  • (Array[ActiveRecord::Base])


245
246
247
# File 'lib/eb_nested_set.rb', line 245

def kin
  patriarch.family
end

#leaf?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/eb_nested_set.rb', line 186

def leaf?
  (right - left) <= 1
end

#leftInteger

Returns the left boundary of this node.

Returns:

  • (Integer)

    the left boundary of this node



334
335
336
# File 'lib/eb_nested_set.rb', line 334

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

#levelInteger

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

Returns:

  • (Integer)

    the number of ancestors of this node.



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

Parameters:

  • force_reload (Boolean) (defaults to: false)

    forces the list to be reloaded

Returns:

  • (Array[ActiveRecord::Base])

    a list of nodes that this node descends from



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

#rightInteger

Returns the right boundary of this node.

Returns:

  • (Integer)

    the right boundary of this node



341
342
343
# File 'lib/eb_nested_set.rb', line 341

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

#root(force_reload = nil) ⇒ ActiveRecord::Base Also known as: patriarch

Finds the root node that this node descends from

Parameters:

  • force_reload (Boolean) (defaults to: nil)

    forces the root node to be reloaded

Returns:

  • (ActiveRecord::Base)

    node the root node this 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

Returns:

  • (Boolean)

    whether this node is a root node or not



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

def root?
  not parent_id?
end

#siblingsArray[ActiveRecord::Base]

Returns all nodes that are siblings of this node

Returns:

  • (Array[ActiveRecord::Base])


305
306
307
# File 'lib/eb_nested_set.rb', line 305

def siblings
  generation - [self]
end