Module: Mongoid::SleepingKingStudios::HasTree::ClassMethods

Defined in:
lib/mongoid/sleeping_king_studios/has_tree.rb

Overview

Class methods added to the base class via #extend.

Since:

  • 0.2.0

Instance Method Summary collapse

Instance Method Details

#has_tree(options = {}) ⇒ Object

Sets up the relations necessary for the tree structure.

Options Hash (options):

  • :parent (Hash) — default: {}

    The options for the parent relation. Supports the :relation_name option, which sets the name of the tree's :belongs_to relation, as well as any options normally supported by a :belongs_to relation.

  • :children (Hash) — default: {}

    The options for the children relation. Supports the :relation_name options, which sets the name of the tree's :has_many relation, as well as any options normally supported by a :has_many relation.

  • :cache_ancestry (Boolean) — default: false

    Stores the chain of ancestors in an :ancestor_ids array field. Adds the #ancestors and #descendents scopes.

    Warning: Using this option will make many write operations much, much slower and more resource-intensive. Do not use this option outside of read-heavy applications with very specific requirements, e.g. a directory structure where you must access all parent directories on each page view.

Raises:

  • (Mongoid::Errors::InvalidOptions)

    If the options are invalid.

See Also:

  • Mongoid::SleepingKingStudios::HasTree::CacheAncestry::ClassMethods#cache_ancestry

Since:

  • 0.4.0


185
186
187
188
# File 'lib/mongoid/sleeping_king_studios/has_tree.rb', line 185

def has_tree **options
  concern = Mongoid::SleepingKingStudios::HasTree
  concern.apply self, options
end

#rootsMongoid::Criteria

Returns a Criteria specifying all root objects, e.g. objects with no parent object.


# File 'lib/mongoid/sleeping_king_studios/has_tree.rb', line 190