Module: Mongoid::Acts::NestedSet::Document::InstanceMethods
Instance Method Summary collapse
-
#<=>(x) ⇒ Object
order by left field.
-
#==(comparison_object) ⇒ Object
Redefine to act like active record.
-
#child? ⇒ Boolean
Returns true if this is a child node.
-
#depth? ⇒ Boolean
Returns true if depth is supported.
-
#leaf? ⇒ Boolean
Returns true if this is a leaf node.
-
#left ⇒ Object
Value of the left field.
-
#nested_set_scope ⇒ Object
All nested set queries should use this nested_set_scope, which performs finds using the :scope declared in the acts_as_nested_set declaration.
-
#outline_numbering? ⇒ Boolean
Returns true if outline numbering is supported.
-
#parent_id ⇒ Object
Value fo the parent field.
-
#right ⇒ Object
Value of the right field.
-
#root? ⇒ Boolean
Returns true if this is a root node.
-
#same_scope?(other) ⇒ Boolean
Check if other model is in the same scope.
- #to_text ⇒ Object
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
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
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
158 159 160 161 |
# File 'lib/mongoid_nested_set/document.rb', line 158 def leaf? #!new_record? && right - left == 1 right - left == 1 end |
#left ⇒ Object
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_scope ⇒ Object
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([: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
177 178 179 |
# File 'lib/mongoid_nested_set/document.rb', line 177 def outline_numbering? !!outline_number_field_name end |
#parent_id ⇒ Object
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 |
#right ⇒ Object
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
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
198 199 200 201 202 |
# File 'lib/mongoid_nested_set/document.rb', line 198 def same_scope?(other) Array([:scope]).all? do |attr| self.send(attr) == other.send(attr) end end |
#to_text ⇒ Object
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 |