
Security News
Follow-up and Clarification on Recent Malicious Ruby Gems Campaign
A clarification on our recent research investigating 60 malicious Ruby gems.
glebtv-mongoid_nested_set
Advanced tools
Mongoid Nested Set is an implementation of the nested set pattern for Mongoid. It is a port of AwesomeNestedSet for ActiveRecord. It supports Mongoid 4+ and Rails 4+
Nested Set represents hierarchies of trees in MongoDB using references rather than embedded documents. A tree is stored as a flat list of documents in a collection. Nested Set allows quick, ordered queries of nodes and their descendants. Tree modification is more costly. The nested set pattern is ideal for models that are read more frequently than modified.
For more on the nested set pattern: http://en.wikipedia.org/wiki/Nested_set_model
Install as Gem
gem install glebtv-mongoid_nested_set
via Gemfile
gem 'glebtv-mongoid_nested_set', '~> 0.7.0'
To start using Mongoid Nested Set, just declare acts_as_nested_set
on your
model:
class Category
include Mongoid::Document
acts_as_nested_set
end
root = Category.create(:name => 'Root Category')
child1 = root.children.create(:name => 'Child Category #1')
child2 = Category.create(:name => 'Child Category #2')
root.children << child2
child1.destroy
Descendants of a destroyed nodes will also be deleted. By default, descendant's
destroy
method will not be called. To enable calling descendant's destroy
method:
class Category
include Mongoid::Document
acts_as_nested_set :dependent => :destroy
end
Several methods exist for moving nodes:
Scopes restrict what is considered a list. This is commonly used to represent multiple trees (or multiple roots) in a single collection.
class Category
include Mongoid::Document
acts_as_nested_set :scope => :root_id
end
Coming from acts_as_tree or adjacency list system where you only have parent_id?
No problem. Simply add acts_as_nested_set
and run:
Category.rebuild!
Your tree will be converted to a valid nested set.
Mongoid Nested Set can manage outline numbers (e.g. 1.3.2) for your documents if
you wish. Simply add :outline_number_field
:
acts_as_nested_set, :outline_number_field => 'number'
Your documents will now include a number
field (you can call it anything you
wish) that will contain outline numbers.
Don't like the outline numbers format? Simply override outline_number_seperator
,
build_outline_number
, or outline_number_sequence
in your model classes. For
example:
class Category
include Mongoid::Document
acts_as_nested_set :scope => :root_id, :outline_number_field => 'number'
# Use a dash instead of a dot for outline numbers
# e.g. 1-3-2
def outline_number_seperator
'-'
end
# Use 0-based indexing instead of 1-based indexing
# e.g. 1.0
def outline_number_sequence(prev_siblings)
prev_siblings.count
end
end
You can learn more about nested sets at:
http://en.wikipedia.org/wiki/Nested_set_model
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
Copyright (c) 2010 Brandon Turner. See LICENSE.txt for further details.
FAQs
Unknown package
We found that glebtv-mongoid_nested_set demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.
Research
/Security News
A malicious Go module posing as an SSH brute forcer exfiltrates stolen credentials to a Telegram bot controlled by a Russian-speaking threat actor.