Coder Social home page Coder Social logo

mongoid-versioning's Introduction

Versioned documents for Mongoid4Build Status

Mongoid supports simple versioning through inclusion of the Mongoid::Versioning module. Including this module will create a versions embedded relation on the document that it will append to on each save. It will also update the version number on the document, which is an integer.

Installation

Add this line to your application's Gemfile:

gem 'mongoid-versioning', github: 'simi/mongoid-versioning'

And then execute:

bundle

Usage

class Person
  include Mongoid::Document
  include Mongoid::Versioning
end

You can also set a max_versions setting, and Mongoid will only keep the max most recent versions.

class Person
  include Mongoid::Document
  include Mongoid::Versioning

  # keep at most 5 versions of a record
  max_versions 5
end

You may skip versioning at any point in time by wrapping the persistence call in a versionless block.

person.versionless do |doc|
  doc.update_attributes(name: "Theodore")
end

Authors

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

mongoid-versioning's People

Contributors

arthurnn avatar deevis avatar erich avatar gautamrege avatar simi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mongoid-versioning's Issues

Reverting changes

In a gem that tracks changes to a document, it would be useful to have a feature to revert changes, and between versions.

Invalid option :versioned provided to relation :versions

I'm trying to update app from rails 4 to rails 5, which required mongoid > 5. with it mongoid-versioning fails with

➜  cms git:(rails-5) ✗ be rails s
=> Booting WEBrick
=> Rails 5.0.6 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
[2017-12-19 13:12:33] INFO  WEBrick 1.3.1
[2017-12-19 13:12:33] INFO  ruby 2.3.3 (2016-11-21) [x86_64-darwin16]
[2017-12-19 13:12:33] INFO  WEBrick::HTTPServer#start: pid=33566 port=3000
Started GET "/admin/articles" for ::1 at 2017-12-19 13:12:51 -0500
DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead. (called from add_before_filter at /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/cancancan-1.10.1/lib/cancan/controller_resource.rb:9)

Mongoid::Errors::InvalidOptions (
message:
  Invalid option :versioned provided to relation :versions.
summary:
  Mongoid checks the options that are passed to the relation macros to ensure that no ill side effects occur by letting something slip by.
resolution:
  Valid options are: as, cascade_callbacks, cyclic, order, store_as, before_add, after_add, before_remove, after_remove, class_name, counter_cache, extend, inverse_class_name, inverse_of, name, relation, validate, make sure these are the ones you are using.):

app/models/article.rb:4:in `include'
app/models/article.rb:4:in `<class:Article>'
app/models/article.rb:1:in `<top (required)>'
app/controllers/admin/articles_controller.rb:1:in `<top (required)>'
  Rendering /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (4.2ms)
  Rendering /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.7ms)
  Rendering /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (21.2ms)
  Rendered /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (179.4ms)

Update usage information

Is it possible to improve the readme to show more use cases for this gem?

For example, it would be good to know how to retrieve a particular version etc

Delete one version

I've a Rails model set up with Mongoid::Versioning.

class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  include Mongoid::Versioning

  field :name, type: String
end

I want to delete a specific older version. For eg.

post = Post.create name: 'A'
post.update_attributes name: 'B'
post.update_attributes name: 'C'
post.version #=> 3
post.versions.count #=> 2
first_version = post.versions.first #=> #<Post _id: , created_at: _, updated_at: _, version: 2, name: "B">

I want to delete first_version, but when I try to delete it..

first_version.delete
post.versions.count #=> 1
post.reload
post.versions.count #=> 0

..all versions get deleted.

I've tried using destroy instead, tried running the code inside a block passed to post.versionless, to no avail. This definitely looks like a bug. Am I doing something wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.