Sha256: 52b2e43497d4a693830a0df26c3b8d24dbd99437d302c2cdb3320396cb5c6c11

Contents?: true

Size: 800 Bytes

Versions: 3

Compression:

Stored size: 800 Bytes

Contents

require 'active_support'
require 'mongoid'

module Mongoid
  module Undo
    extend ActiveSupport::Concern

    include Mongoid::Paranoia
    include Mongoid::Versioning

    included do
      field :action, type: Symbol, versioned: false
      index deleted_at: 1

      [:create, :update, :destroy].each do |action|
        set_callback action, :after do
          collection.find(atomic_selector).update('$set' => { action: action })
          reload
        end
      end
    end

    def undo
      case action
      when :create, :destroy
        deleted_at.present? ? restore : delete
      when :update
        retrieve
      end
    end
    alias_method :redo, :undo

  private
    def retrieve
      update_attributes(versions.last.versioned_attributes.except('version'))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-undo-0.7.0 lib/mongoid/undo.rb
mongoid-undo-0.6.0 lib/mongoid/undo.rb
mongoid-undo-0.5.0 lib/mongoid/undo.rb