{"id":19474609,"url":"https://github.com/tomasc/mongoid_versioning","last_synced_at":"2025-04-25T12:31:51.792Z","repository":{"id":21589463,"uuid":"24909523","full_name":"tomasc/mongoid_versioning","owner":"tomasc","description":"Versioning Mongoid documents by means of separate collection.","archived":false,"fork":false,"pushed_at":"2015-10-24T07:32:52.000Z","size":317,"stargazers_count":0,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T22:23:36.159Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/tomasc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-07T20:12:56.000Z","updated_at":"2015-01-02T21:06:30.000Z","dependencies_parsed_at":"2022-08-21T05:10:15.364Z","dependency_job_id":null,"html_url":"https://github.com/tomasc/mongoid_versioning","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Fmongoid_versioning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Fmongoid_versioning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Fmongoid_versioning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasc%2Fmongoid_versioning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomasc","download_url":"https://codeload.github.com/tomasc/mongoid_versioning/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250817798,"owners_count":21492224,"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-11-10T19:25:51.753Z","updated_at":"2025-04-25T12:31:51.477Z","avatar_url":"https://github.com/tomasc.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mongoid Versioning\n\n[![Build Status](https://travis-ci.org/tomasc/mongoid_versioning.svg)](https://travis-ci.org/tomasc/mongoid_versioning) [![Gem Version](https://badge.fury.io/rb/mongoid_versioning.svg)](http://badge.fury.io/rb/mongoid_versioning) [![Coverage Status](https://img.shields.io/coveralls/tomasc/mongoid_versioning.svg)](https://coveralls.io/r/tomasc/mongoid_versioning)\n\nVersioning of Mongoid documents. Past versions are stored in separate collection.\n\nThis gem is intended for versioning of whole documents. However, it might be possible to extend its support to embedded documents as well. Pull requests are welcome.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'mongoid_versioning'\n```\n\nAnd then execute:\n\n```\n$ bundle\n```\n\nOr install it yourself as:\n\n```\n$ gem install mongoid_versioning\n```\n\n## Usage\n\nInclude the `MongoidVersioning::Versioned` module into your model:\n\n```ruby\nclass MyVersionedDocument\n    include Mongoid::Document\n    include MongoidVersioning::Versioned\nend\n```\n\nYour class will then have:\n\n```ruby\nfield :_version, type: Integer\nfield :_based_on_version, type: Integer\n```\n\n### Creating versions\n\nTo create new version of your document:\n\n```ruby\ndoc = MyVersionedDocument.new\ndoc.revise # =\u003e true\ndoc._version # =\u003e 1\ndoc._based_on_version # =\u003e nil\n```\n\nThe `#revise` method validates the document and runs `:revise`, `:save` and `:update` callbacks. (Please note that running `#revise` on new document will resort to standard `#save`.)\n\nA `#revise!` method, raising `Mongoid::Errors::Validations` and `Mongoid::Errors::Callbacks` exceptions, is also available.\n\n### Retrieving versions\n\nTo access all previous versions:\n\n```ruby\ndoc.previous_versions # =\u003e Mongoid::Criteria\n```\n\nThese versions are stored in separate collection, by default named by appending `.versions` to name of the source collection. In the above example it is `my_versioned_documents.versions`.\n\nTo access latest version (as stored in the db):\n\n```ruby\ndoc.latest_version # =\u003e MyVersionedDocument\n```\n\nTo retrieve all versions of a document:\n\n```ruby\ndoc.versions # =\u003e Array\n```\n\nTo retrieve specific version:\n\n```ruby\ndoc.version(2) # =\u003e MyVersionedDocument\n```\n\n### Removing versions\n\nBy default, past versions are never removed. This way it is possible to implement some sort of recovery mechanism of deleted documents.\n\nIt should be however trivial to implement removal of versions on destroy. For example:\n\n```ruby\nafter_destroy -\u003e doc { doc.previous_versions.destroy_all }\n```\n\n## Further Reading\n\nSee [Further Thoughts on How to Track Versions with MongoDB](http://askasya.com/post/revisitversions).\n\n## Contributing\n\n1. Fork it ( https://github.com/tomasc/mongoid_versioning/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasc%2Fmongoid_versioning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomasc%2Fmongoid_versioning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasc%2Fmongoid_versioning/lists"}