<commit_before>require 'active_support'
require 'mongoid'
require 'mongoid/paranoia'
require 'mongoid/versioning'

module Mongoid
  module Undo
    extend ActiveSupport::Concern

    include Mongoid::Paranoia
    include Mongoid::Versioning
    include Mongoid::Interceptable

    included do
      # _id must be marked as not-versioned
      fields['_id'].options.merge!(versioned: false)
      field :action, type: Symbol, versioned: false
      index deleted_at: 1

      [:create, :update, :destroy].each do |action|
        name = :"set_action_after_#{action}"

        define_method name do
          collection.find(atomic_selector).update('$set' => { action: action })
          version = self.instance_variable_get(:@version)
          reload
          self.instance_variable_set :@version, version unless version.nil?
        end
        set_callback action, :after, name
      end

      after_find do
        @version = read_attribute(:version)
      end

      define_model_callbacks :undo, :redo
    end

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

    def undoable?
      case action
      when :create, :destroy
        true
      when :update
        read_attribute(:version).to_i > @version
      end
    end
    alias_method :redoable?, :undoable?

  private
    def retrieve
      update_attributes(versions.last.versioned_attributes.except('version', 'updated_at'))
    end
  end
end<commit_msg>ActiveModel 4 changes.<commit_after>require 'active_support'
require 'mongoid'
require 'mongoid/paranoia'
require 'mongoid/versioning'

module Mongoid
  module Undo
    extend ActiveSupport::Concern

    include Mongoid::Paranoia
    include Mongoid::Versioning
    include Mongoid::Interceptable

    included do
      # _id must be marked as not-versioned
      fields['_id'].options.merge!(versioned: false)
      field :action, type: Symbol, versioned: false
      index deleted_at: 1

      [:create, :update, :destroy].each do |action|
        name = :"set_action_after_#{action}"

        define_method name do
          collection.find(atomic_selector).update('$set' => { action: action })
          version = self.instance_variable_get(:@version)
          reload
          self.instance_variable_set :@version, version unless version.nil?
        end
        set_callback action, :after, name
      end

      after_find do
        @version = read_attribute(:version)
      end

      define_model_callbacks :undo, :redo
    end

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

    def undoable?
      case action
      when :create, :destroy
        true
      when :update
        read_attribute(:version).to_i > @version
      end
    end
    alias_method :redoable?, :undoable?

  private
    def retrieve
      update versions.last.versioned_attributes.except('version', 'updated_at')
    end
  end
end
{'fraction_non_alphanumeric': 0.08124806919987643, 'fraction_numerical': 0.0009267840593141798, 'mean_word_length': 2.4779806659505907, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1635447', 'n_tokens_mistral': 940, 'n_tokens_neox': 914, 'n_words': 246}