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



183
184
185
# File 'lib/mongoid_nested_set/document.rb', line 183

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

#==(comparison_object) ⇒ Object

Redefine to act like active record



189
190
191
192
193
194
# File 'lib/mongoid_nested_set/document.rb', line 189

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)


165
166
167
# File 'lib/mongoid_nested_set/document.rb', line 165

def child?
  !parent_id.nil?
end

#depth?Boolean

Returns true if depth is supported

Returns:

  • (Boolean)


171
172
173
# File 'lib/mongoid_nested_set/document.rb', line 171

def depth?
  true
end

#leaf?Boolean

Returns true if this is a leaf node

Returns:

  • (Boolean)


158
159
160
161
# File 'lib/mongoid_nested_set/document.rb', line 158

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

#leftObject

Value of the left field



140
141
142
# File 'lib/mongoid_nested_set/document.rb', line 140

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



214
215
216
217
218
219
220
# File 'lib/mongoid_nested_set/document.rb', line 214

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)


177
178
179
# File 'lib/mongoid_nested_set/document.rb', line 177

def outline_numbering?
  !!outline_number_field_name
end

#parent_idObject

Value fo the parent field



134
135
136
# File 'lib/mongoid_nested_set/document.rb', line 134

def parent_id
  self[parent_field_name]
end

#rightObject

Value of the right field



146
147
148
# File 'lib/mongoid_nested_set/document.rb', line 146

def right
  self[right_field_name]
end

#root?Boolean

Returns true if this is a root node

Returns:

  • (Boolean)


152
153
154
# File 'lib/mongoid_nested_set/document.rb', line 152

def root?
  parent_id.nil?
end

#same_scope?(other) ⇒ Boolean

Check if other model is in the same scope

Returns:

  • (Boolean)


198
199
200
201
202
# File 'lib/mongoid_nested_set/document.rb', line 198

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

#to_textObject



205
206
207
208
209
# File 'lib/mongoid_nested_set/document.rb', line 205

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