Module: Mongoid::SleepingKingStudios::HasTree

Extended by:
ActiveSupport::Concern, Concern
Defined in:
lib/mongoid/sleeping_king_studios/has_tree.rb,
lib/mongoid/sleeping_king_studios/has_tree/errors.rb,
lib/mongoid/sleeping_king_studios/has_tree/metadata.rb,
lib/mongoid/sleeping_king_studios/has_tree/cache_ancestry.rb,
lib/mongoid/sleeping_king_studios/has_tree/parent/metadata.rb,
lib/mongoid/sleeping_king_studios/has_tree/children/metadata.rb,
lib/mongoid/sleeping_king_studios/has_tree/cache_ancestry/metadata.rb

Overview

Note:

From 0.2.0 to 0.3.1, was Mongoid::SleepingKingStudios::Tree.

Adds a belongs_to parent relation and a has_many children relation to set up a basic tree structure, as well as several helper methods.

Since 0.4.1, you must call the class method ::has_tree in order to set up the parent and children relations. You can pass optional parameters into this method to customise the created relations, including the names of the relations.

Examples:

Setting up the tree:

class SluggableDocument
  include Mongoid::Document
  include Mongoid::SleepingKingStudios::Tree

  has_tree
end # class

Setting up the tree with alternate relation names:

class EvilEmployee
  include Mongoid::Document
  include Mongoid::SleepingKingStudios::Tree

  has_tree :parent => { :relation_name => 'overlord' },
    :children => { :relation_name => 'minions', :dependent => :destroy }
end # class

Since:

  • 0.2.0

Defined Under Namespace

Modules: CacheAncestry, Children, ClassMethods, Errors, Parent Classes: Metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concern

characterize, relate, valid_options, validate_options

Class Method Details

.apply(base, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets up the has_tree relation, creating fields, accessors and validations.

Since:

  • 0.6.0


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mongoid/sleeping_king_studios/has_tree.rb', line 50

def self.apply base, options
  options[:parent]   ||= {}
  options[:children] ||= {}

  options[:parent][:inverse_of]   = options[:children].fetch(:relation_name, :children)
  options[:children][:inverse_of] = options[:parent].fetch(:relation_name,   :parent)

  name = :has_tree
  validate_options    name, options
  meta = characterize name, options, Metadata

  relate base, name, meta

  define_relations base, meta
  define_helpers   base, meta

  if meta.cache_ancestry?
    concern = Mongoid::SleepingKingStudios::HasTree::CacheAncestry
    concern.send :apply, base, meta.cache_ancestry
  end # if
end

.define_helpers(base, metadata) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets up the helper methods for the relations as follows.

Defines the following class methods:

  • ::roots

Defines the following instance methods:

  • #leaf?

  • #root

  • #root?

Since:

  • 0.6.0


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mongoid/sleeping_king_studios/has_tree.rb', line 88

def self.define_helpers base, 
  base.metaclass.send :define_method, :roots do
    where({ :"#{.parent.relation_name}_id" => nil })
  end # class method roots

  base.send :define_method, :root do
    parent = send(.parent.relation_name)
    parent ? parent.root : self
  end # method root

  base.send :define_method, :leaf? do
    send(.children.relation_name).blank?
  end # method root?

  base.send :define_method, :root? do
    send(.parent.relation_name).nil?
  end # method root?

  base.send :define_method, :siblings do
    self.class.where(:id => { "$ne" => self.id }).where(.foreign_key => self.send(.foreign_key))
  end # method siblings
end

.define_relations(base, metadata) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets up the parent and children relations.

Since:

  • 0.6.0


119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mongoid/sleeping_king_studios/has_tree.rb', line 119

def self.define_relations base, 
  parent_options = .parent.properties.dup
  parent_options.update :class_name => base.name
  parent_options.delete :relation_name

  children_options = .children.properties.dup
  children_options.update :class_name => base.name
  children_options.delete :relation_name

  base.belongs_to .parent.relation_name,   parent_options
  base.has_many   .children.relation_name, children_options
end

.valid_optionsArray<Symbol>

Get the valid options allowed with this concern.

Since:

  • 0.4.1


137
138
139
140
141
142
143
# File 'lib/mongoid/sleeping_king_studios/has_tree.rb', line 137

def self.valid_options
  %i(
    cache_ancestry
    children
    parent
  ) # end Array
end

Instance Method Details

#childrenArray<Tree>

Returns the list of child objects.


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

#leaf?Boolean

Returns true if the object is a leaf object, e.g. has no child objects.


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

#parentTree?

Returns the parent object, or nil if the object is a root.


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

#rootTree

Returns the root object of the current object's tree.


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

#root?Boolean

Returns true if the object is a root object, e.g. has no parent object.


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

#siblingsMongoid::Criteria

Returns a Criteria specifying all persisted objects in the tree whose parent is the current object's parent, excluding the current object.

Since:

  • 0.6.1


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