Module: ClosureTree::Model::ClassMethods

Defined in:
lib/closure_tree/acts_as_tree.rb

Instance Method Summary collapse

Instance Method Details

#find_by_path(path) ⇒ Object

Find the node whose ancestry_path is path



325
326
327
328
# File 'lib/closure_tree/acts_as_tree.rb', line 325

def find_by_path(path)
  root = roots.send("find_by_#{name_column}", path.shift)
  root.try(:find_by_path, path)
end

#find_or_create_by_path(path, attributes = {}) ⇒ Object

Find or create nodes such that the ancestry_path is path



331
332
333
334
335
336
337
338
339
340
# File 'lib/closure_tree/acts_as_tree.rb', line 331

def find_or_create_by_path(path, attributes = {})
  name = path.shift
  # shenanigans because find_or_create can't infer we want the same class as this:
  # Note that roots will already be constrained to this subclass (in the case of polymorphism):
  root = roots.send("find_by_#{name_column}", name)
  if root.nil?
    root = create!(attributes.merge(name_sym => name))
  end
  root.find_or_create_by_path(path, attributes)
end

#rebuild!Object

Rebuilds the hierarchy table based on the parent_id column in the database. Note that the hierarchy table will be truncated.



318
319
320
321
322
# File 'lib/closure_tree/acts_as_tree.rb', line 318

def rebuild!
  hierarchy_class.delete_all # not destroy_all -- we just want a simple truncate.
  roots.each { |n| n.send(:rebuild!) } # roots just uses the parent_id column, so this is safe.
  nil
end

#rootObject

Returns an arbitrary node that has no parents.



312
313
314
# File 'lib/closure_tree/acts_as_tree.rb', line 312

def root
  roots.first
end