Module: Mongoid::Acts::NestedSet::Document::InstanceMethods

Includes:
Comparable, Fields, Relations, Update
Defined in:
lib/mongoid_nested_set/document.rb

Instance Method Summary collapse

Methods included from Fields

#left_field_name, #outline_number_field_name, #parent_field_name, #quoted_left_field_name, #quoted_parent_field_name, #quoted_right_field_name, #quoted_scope_field_names, #right_field_name, #scope_class, #scope_field_names

Methods included from Update

#move_left, #move_possible?, #move_right, #move_to_child_of, #move_to_left_of, #move_to_right_of, #move_to_root

Methods included from Relations

#ancestors, #descendants, #is_ancestor_of?, #is_descendant_of?, #is_or_is_ancestor_of?, #is_or_is_descendant_of?, #leaves, #left_sibling, #level, #right_sibling, #root, #self_and_ancestors, #self_and_descendants, #self_and_siblings, #siblings

Instance Method Details

#<=>(x) ⇒ Object

order by left field



187
188
189
# File 'lib/mongoid_nested_set/document.rb', line 187

def <=>(x)
  left <=> x.left
end

#==(comparison_object) ⇒ Object

Redefine to act like active record



193
194
195
196
197
198
# File 'lib/mongoid_nested_set/document.rb', line 193

def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.instance_of?(scope_class) &&
     comparison_object.id == id &&
     !comparison_object.new_record?)
end

#child?Boolean

Returns true if this is a child node

Returns:

  • (Boolean)


169
170
171
# File 'lib/mongoid_nested_set/document.rb', line 169

def child?
  !parent_id.nil?
end

#depth?Boolean

Returns true if depth is supported

Returns:

  • (Boolean)


175
176
177
# File 'lib/mongoid_nested_set/document.rb', line 175

def depth?
  true
end

#leaf?Boolean

Returns true if this is a leaf node

Returns:

  • (Boolean)


162
163
164
165
# File 'lib/mongoid_nested_set/document.rb', line 162

def leaf?
  #!new_record? && right - left == 1
  right - left == 1
end

#leftObject

Value of the left field



144
145
146
# File 'lib/mongoid_nested_set/document.rb', line 144

def left
  self[left_field_name]
end

#nested_set_scopeObject

All nested set queries should use this nested_set_scope, which performs finds using the :scope declared in the acts_as_nested_set declaration



218
219
220
221
222
223
224
# File 'lib/mongoid_nested_set/document.rb', line 218

def nested_set_scope
  scopes = Array(acts_as_nested_set_options[:scope])
  conditions = scopes.inject({}) do |conditions,attr|
    conditions.merge attr => self[attr]
  end unless scopes.empty?
  scope_class.criteria.where(conditions).asc(left_field_name)
end

#outline_numbering?Boolean

Returns true if outline numbering is supported

Returns:

  • (Boolean)


181
182
183
# File 'lib/mongoid_nested_set/document.rb', line 181

def outline_numbering?
  !!outline_number_field_name
end

#parent_idObject

Value fo the parent field



138
139
140
# File 'lib/mongoid_nested_set/document.rb', line 138

def parent_id
  self[parent_field_name]
end

#rightObject

Value of the right field



150
151
152
# File 'lib/mongoid_nested_set/document.rb', line 150

def right
  self[right_field_name]
end

#root?Boolean

Returns true if this is a root node

Returns:

  • (Boolean)


156
157
158
# File 'lib/mongoid_nested_set/document.rb', line 156

def root?
  parent_id.nil?
end

#same_scope?(other) ⇒ Boolean

Check if other model is in the same scope

Returns:

  • (Boolean)


202
203
204
205
206
# File 'lib/mongoid_nested_set/document.rb', line 202

def same_scope?(other)
  Array(acts_as_nested_set_options[:scope]).all? do |attr|
    self.send(attr) == other.send(attr)
  end
end

#to_textObject



209
210
211
212
213
# File 'lib/mongoid_nested_set/document.rb', line 209

def to_text
  self_and_descendants.map do |node|
    "#('*'*(node.level+1)} #{node.id} #{node.to_s} (#{node.parent_id}, #{node.left}, #{node.right})"
  end.join("\n")
end