Sha256: 0fa72403cbe2b31ab1866f92eef77f93eb85a19bd135c477cd06daea7bac6c5b

Contents?: true

Size: 991 Bytes

Versions: 18

Compression:

Stored size: 991 Bytes

Contents

module Mongoid #:nodoc:
  # Include this module to get automatic versioning of root level documents.
  # This will add a version field to the +Document+ and a has_many association
  # with all the versions contained in it.
  module Versioning
    def self.included(base)
      base.class_eval do
        field :version, :type => Integer, :default => 1
        has_many :versions, :class_name => self.name
        before_save :revise
        include InstanceMethods
      end
    end
    module InstanceMethods
      # Create a new version of the +Document+. This will load the previous
      # document from the database and set it as the next version before saving
      # the current document. It then increments the version number.
      def revise
        last_version = self.class.first(:conditions => { :_id => id, :version => version })
        if last_version
          self.versions << last_version.clone
          self.version = version + 1
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
mongoid-0.9.4 lib/mongoid/versioning.rb
mongoid-0.9.3 lib/mongoid/versioning.rb
mongoid-0.9.2 lib/mongoid/versioning.rb
mongoid-0.9.1 lib/mongoid/versioning.rb
mongoid-0.9.0 lib/mongoid/versioning.rb
mongoid-0.8.10 lib/mongoid/versioning.rb
mongoid-0.8.9 lib/mongoid/versioning.rb
mongoid-0.8.8 lib/mongoid/versioning.rb
mongoid-0.8.7 lib/mongoid/versioning.rb
mongoid-0.8.6 lib/mongoid/versioning.rb
mongoid-0.8.5 lib/mongoid/versioning.rb
mongoid-0.8.4 lib/mongoid/versioning.rb
mongoid-0.8.3 lib/mongoid/versioning.rb
mongoid-0.8.2 lib/mongoid/versioning.rb
mongoid-0.8.1 lib/mongoid/versioning.rb
mongoid-0.7.10 lib/mongoid/versioning.rb
mongoid-0.7.9 lib/mongoid/versioning.rb
mongoid-0.7.8 lib/mongoid/versioning.rb