{"id":13484374,"url":"https://github.com/benedikt/mongoid-tree","last_synced_at":"2025-03-27T16:30:46.388Z","repository":{"id":989589,"uuid":"796714","full_name":"benedikt/mongoid-tree","owner":"benedikt","description":"A tree structure for Mongoid documents using the materialized path pattern","archived":false,"fork":false,"pushed_at":"2024-08-14T12:36:26.000Z","size":420,"stargazers_count":300,"open_issues_count":0,"forks_count":68,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-19T04:06:47.466Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.rubydoc.info/github/benedikt/mongoid-tree","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benedikt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2010-07-25T16:03:41.000Z","updated_at":"2025-01-24T17:07:06.000Z","dependencies_parsed_at":"2024-10-30T18:41:32.759Z","dependency_job_id":null,"html_url":"https://github.com/benedikt/mongoid-tree","commit_stats":{"total_commits":276,"total_committers":16,"mean_commits":17.25,"dds":"0.22101449275362317","last_synced_commit":"44fedc46c0b73899cfb4af0594c5dca7e7c9730d"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benedikt%2Fmongoid-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benedikt%2Fmongoid-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benedikt%2Fmongoid-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benedikt%2Fmongoid-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benedikt","download_url":"https://codeload.github.com/benedikt/mongoid-tree/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245882237,"owners_count":20687857,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T17:01:23.282Z","updated_at":"2025-03-27T16:30:44.587Z","avatar_url":"https://github.com/benedikt.png","language":"Ruby","funding_links":[],"categories":["Ruby","ORM/ODM Extensions"],"sub_categories":[],"readme":"# mongoid-tree [![Build Status](https://github.com/benedikt/mongoid-tree/workflows/Tests/badge.svg)](https://github.com/benedikt/mongoid-tree)\n\nA tree structure for Mongoid documents using the materialized path pattern\n\n## Requirements\n\n* mongoid (\u003e= 4.0, \u003c 10.0)\n\nFor a mongoid 3.x compatible version, please use mongoid-tree 1.0.x,\nfor a mongoid 2.x compatible version, please use mongoid-tree 0.7.x.\n\n\n## Install\n\nTo install mongoid_tree, simply add it to your Gemfile:\n\n    gem 'mongoid-tree', require: 'mongoid/tree'\n\nIn order to get the latest development version of mongoid-tree:\n\n    gem 'mongoid-tree', git: 'git://github.com/benedikt/mongoid-tree', branch: :main\n\nYou might want to add `require: nil` option and explicitly `require 'mongoid/tree'` where needed and finally run\n\n    bundle install\n\n### Upgrade from mongoid-tree 1.x\n\nTo fix issues with the ordering of ancestors, mongoid-tree 2.0 introduces a new `depth` field to the documents that include the `Mongoid::Tree` module. In case your project uses its own `depth` field, you can now rely on mongoid-tree to handle this.\n\n## Usage\n\nRead the API documentation at https://www.rubydoc.info/github/benedikt/mongoid-tree and take a look at the `Mongoid::Tree` module\n\n```ruby\nclass Node\n  include Mongoid::Document\n  include Mongoid::Tree\nend\n```\n\n### Utility methods\n\nThere are several utility methods that help getting to other related documents in the tree:\n\n```ruby\nNode.root\nNode.roots\nNode.leaves\n\nnode.root\nnode.parent\nnode.children\nnode.ancestors\nnode.ancestors_and_self\nnode.descendants\nnode.descendants_and_self\nnode.siblings\nnode.siblings_and_self\nnode.leaves\n```\n\nIn addition it's possible to check certain aspects of the document's position in the tree:\n\n```ruby\nnode.root?\nnode.leaf?\nnode.depth\nnode.ancestor_of?(other)\nnode.descendant_of?(other)\nnode.sibling_of?(other)\n```\n\nSee `Mongoid::Tree` for more information on these methods.\n\n\n### Ordering\n\n`Mongoid::Tree` doesn't order children by default. To enable ordering of tree nodes include the `Mongoid::Tree::Ordering` module. This will add a `position` field to your document and provide additional utility methods:\n\n```ruby\nnode.lower_siblings\nnode.higher_siblings\nnode.first_sibling_in_list\nnode.last_sibling_in_list\n\nnode.move_up\nnode.move_down\nnode.move_to_top\nnode.move_to_bottom\nnode.move_above(other)\nnode.move_below(other)\n\nnode.at_top?\nnode.at_bottom?\n```\n\nExample:\n\n```ruby\nclass Node\n  include Mongoid::Document\n  include Mongoid::Tree\n  include Mongoid::Tree::Ordering\nend\n```\n\nSee `Mongoid::Tree::Ordering` for more information on these methods.\n\n### Traversal\n\nIt's possible to traverse the tree using different traversal methods using the `Mongoid::Tree::Traversal` module.\n\nExample:\n\n```ruby\nclass Node\n  include Mongoid::Document\n  include Mongoid::Tree\n  include Mongoid::Tree::Traversal\nend\n\nnode.traverse(:breadth_first) do |n|\n  # Do something with Node n\nend\n```\n\n### Destroying\n\n`Mongoid::Tree` does not handle destroying of nodes by default. However it provides several strategies that help you to deal with children of deleted documents. You can simply add them as `before_destroy` callbacks.\n\nAvailable strategies are:\n\n* `:nullify_children` -- Sets the children's parent_id to null\n* `:move_children_to_parent` -- Moves the children to the current document's parent\n* `:destroy_children` -- Destroys all children by calling their `#destroy` method (invokes callbacks)\n* `:delete_descendants` -- Deletes all descendants using a database query (doesn't invoke callbacks)\n\nExample:\n\n```ruby\nclass Node\n  include Mongoid::Document\n  include Mongoid::Tree\n\n  before_destroy :nullify_children\nend\n```\n\n\n### Callbacks\n\nThere are two callbacks that are called before and after the rearranging process. This enables you to do additional computations after the documents position in the tree is updated. See `Mongoid::Tree` for details.\n\nExample:\n\n```ruby\nclass Page\n  include Mongoid::Document\n  include Mongoid::Tree\n\n  after_rearrange :rebuild_path\n\n  field :slug\n  field :path\n\n  private\n\n  def rebuild_path\n    self.path = self.ancestors_and_self.collect(\u0026:slug).join('/')\n  end\nend\n```\n\n### Validations\n\n`Mongoid::Tree` currently does not validate the document's children or parent associations by default. To explicitly enable validation for children and parent documents it's required to add a `validates_associated` validation.\n\nExample:\n\n```ruby\nclass Node\n  include Mongoid::Document\n  include Mongoid::Tree\n\n  validates_associated :parent, :children\nend\n```\n\n## Build Status\n\nmongoid-tree is on [GitHub Actions](https://github.com/benedikt/mongoid-tree/actions) running the specs on Ruby 3.1-3.3 and Mongoid 4.x-9.x.\n\n## Known issues\n\nSee [https://github.com/benedikt/mongoid-tree/issues](https://github.com/benedikt/mongoid-tree/issues)\n\n\n## Repository\n\nSee [https://github.com/benedikt/mongoid-tree](https://github.com/benedikt/mongoid-tree) and feel free to fork it!\n\n\n## Contributors\n\nSee a list of all contributors at [https://github.com/benedikt/mongoid-tree/contributors](https://github.com/benedikt/mongoid-tree/contributors). Thanks a lot everyone!\n\n\n## Copyright\n\nCopyright (c) 2010-2024 Benedikt Deicke. See LICENSE for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenedikt%2Fmongoid-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenedikt%2Fmongoid-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenedikt%2Fmongoid-tree/lists"}