Sha256: 2a0d0b7db06aee01fe0d51c449a577106215b0ea5b29cd56fa14cfea7483afac

Contents?: true

Size: 958 Bytes

Versions: 1

Compression:

Stored size: 958 Bytes

Contents

# encoding: utf-8
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
    extend ActiveSupport::Concern
    included do
      field :version, :type => Integer, :default => 1
      has_many :versions, :class_name => self.name
      before_save :revise
    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

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-pre-2.0.0.pre lib/mongoid/versioning.rb