lib/mongoid/versioning.rb in mongoid-2.8.1 vs lib/mongoid/versioning.rb in mongoid-3.0.0.rc
- old
+ new
@@ -1,26 +1,26 @@
# encoding: utf-8
-module Mongoid #:nodoc:
+module Mongoid
# 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
+ field :version, type: Integer, default: 1
embeds_many \
:versions,
- :class_name => self.name,
- :validate => false,
- :cyclic => true,
- :inverse_of => nil,
- :versioned => true
+ class_name: self.name,
+ validate: false,
+ cyclic: true,
+ inverse_of: nil,
+ versioned: true
- set_callback :save, :before, :revise, :if => :revisable?
+ set_callback :save, :before, :revise, if: :revisable?
class_attribute :version_max
self.cyclic = true
end
@@ -34,21 +34,20 @@
#
# @since 1.0.0
def revise
previous = previous_revision
if previous && versioned_attributes_changed?
- versions.build(
- previous.versioned_attributes, :without_protection => true
- ).attributes.delete("_id")
+ new_version = versions.build(
+ previous.versioned_attributes, without_protection: true
+ )
+ new_version._id = nil
if version_max.present? && versions.length > version_max
deleted = versions.first
if deleted.paranoid?
versions.delete_one(deleted)
- collection.update(
- atomic_selector,
- { "$pull" => { "versions" => { "version" => deleted.version }}}
- )
+ collection.find(atomic_selector).
+ update({ "$pull" => { "versions" => { "version" => deleted.version }}})
else
versions.delete(deleted)
end
end
self.version = (version || 1 ) + 1
@@ -61,12 +60,12 @@
# @example Revise the document.
# person.revise!
#
# @since 2.2.1
def revise!
- new_version = versions.build(
- (previous_revision || self).versioned_attributes, :without_protection => true
+ versions.build(
+ (previous_revision || self).versioned_attributes, without_protection: true
)
versions.shift if version_max.present? && versions.length > version_max
self.version = (version || 1 ) + 1
save
end
@@ -130,12 +129,12 @@
#
# @since 2.0.0
def previous_revision
_loading_revision do
self.class.unscoped.
- where(:_id => id).
- any_of({ :version => version }, { :version => nil }).first
+ where(_id: id).
+ any_of({ version: version }, { version: nil }).first
end
end
# Is the document able to be revised? This is true if the document has
# changed and we have not explicitly told it not to version.
@@ -158,11 +157,11 @@
#
# @return [ true, false ] Is the document not currently versioning.
#
# @since 2.0.0
def versionless?
- !!@versionless
+ @versionless ||= false
end
# Filters fields that should not be versioned out of an attributes hash.
# Dynamic attributes are always versioned.
#
@@ -170,18 +169,18 @@
#
# @return [ Hash ] The hash without non-versioned columns.
#
# @since 2.1.0
def only_versioned_attributes(hash)
- {}.tap do |versioned|
- hash.except("versions").each_pair do |name, value|
- field = fields[name]
- versioned[name] = value if !field || field.versioned?
- end
+ versioned = {}
+ hash.except("versions").each_pair do |name, value|
+ field = fields[name]
+ versioned[name] = value if !field || field.versioned?
end
+ versioned
end
- module ClassMethods #:nodoc:
+ module ClassMethods
# Sets the maximum number of versions to store.
#
# @example Set the maximum.
# Person.max_versions(5)