ere
ere

Reputation: 1779

limiting version attributes in mongoid

I'm using Mongoid::Versioning which works great except that I would like to prevent several fields from being versioned.

There's not a lot of info written about it in the docs so I'm not sure how to do this.

http://mongoid.org/docs/extras.html

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

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

They show how to skip a version altogether but not how to restrict certain fields from being versioned.

Any ideas?

UPDATE

I found something like this digging through the code, but I'm not sure how to use it.

https://github.com/mongoid/mongoid/blob/master/lib/mongoid/versioning.rb#L90

Upvotes: 3

Views: 496

Answers (3)

Luca G. Soave
Luca G. Soave

Reputation: 12679

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

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

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

Upvotes: 0

shingara
shingara

Reputation: 46914

All Field has option :versioned true by default If you don't want this versionned you can pass false. By exemple I want name versioned but no login

class User
  include Mongoid::Document
  include Mongoid::Versioning

  field :name, :type => String
  field :login, :type => String, :versioned => false
end

You can pass the :versioned option in embed association too.

You can override this option by iterating over the .fields on your Document.

So in your code you can add avoid versionned on some field by creating a little methode :

class User

  include Mongoid::Document
  include Mongoid::Versioning
  include Mongoid::Voteable

  field :name, :type => String
  field :login, :type => String

  def self.avoid_versioned(*unversioned_fields)
    unversioned_fields.each do |f|

      fe = self.fields[f.to_s]
      fe.options[:versioned] = false if fe

      re = self.relations[f.to_s]
      re[:versioned] = false if re

    end
  end
  avoid_versioned( :login, :votes )
end

Upvotes: 4

holden
holden

Reputation: 13581

You can probably find a way to do this, but I would suggest checking out this gem instead.

https://github.com/aq1018/mongoid-history

track_history   :on => [:title, :body],       # I want to track title and body fields only. Default is :all
                  :modifier_field => :modifier, # Adds "referened_in :modifier" to track who made the change. Default is :modifier
                  :version_field => :version,   # Adds "field :version, :type => Integer" to track current version. Default is :version
                  :track_create   =>  false,    # Do you want to track document creation? Default is false
                  :track_update   =>  true,     # Do you want to track document updates? Default is true
                  :track_destroy  =>  false,    # Do you want to track document destruction? Default is false

Upvotes: 2

Related Questions