Sha256: d6d8600667560bae25a56bf45a5351b2d6e5e05c74ddb2f72274e640a67eaba3

Contents?: true

Size: 1007 Bytes

Versions: 2

Compression:

Stored size: 1007 Bytes

Contents

require 'active_support'
require 'mongoid'

module Mongoid::Fields
  class Localized
    def mongoize(object)
      if object.is_a? ::Hash
        object
      else
        { ::I18n.locale.to_s => type.mongoize(object) }
      end
    end
  end
end


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

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-undo-0.4.0 lib/mongoid/undo.rb
mongoid-undo-0.3.0 lib/mongoid/undo.rb