<commit_before>require 'active_record'

module ActsAsParanoid  
  def acts_as_paranoid(options = {})
    raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
    
    configuration = { :column => "deleted_at", :column_type => "time" }
    configuration.update(options) unless options.nil?

    type = case configuration[:column_type]
      when "time" then "Time.now"
      when "boolean" then "true"
      else
        raise ArgumentError, "'time' or 'boolean' expected for :column_type option, got #{configuration[:column_type]}"
    end
    
    class_eval <<-EOV
      default_scope where("#{self.table_name}.#{configuration[:column]} IS ?", nil)

      class << self
        def with_deleted
          self.unscoped.where() # ugly, but it re-fetches the data (no caching)
        end

        def only_deleted
          self.unscoped.
            where("#{self.table_name}.#{configuration[:column]} IS NOT ?
              OR #{self.table_name}.#{configuration[:column]} != ?", nil, true)
        end

        def delete_all!(conditions = nil)
          self.unscoped.delete_all!(conditions)
        end
        
        def delete_all(conditions = nil)
          update_all ["#{configuration[:column]} = ?", #{type}], conditions
        end
      end

      def destroy!
        before_destroy() if respond_to(:before_destroy)
        
        #{self.name}.delete_all!(:id => self)
        
        after_destroy() if respond_to(:after_destroy)
      end
      
      def destroy
        before_destroy() if respond_to(:before_destroy)
        
        if self.#{configuration[:column]} == nil
          #{self.name}.delete_all(:id => self.id)
        else
          #{self.name}.delete_all!(:id => self.id)
        end
        
        after_destroy() if respond_to(:after_destroy)
      end

      def recover
        self.update_attribute(:#{configuration[:column]}, nil)
      end
      
      ActiveRecord::Relation.class_eval do
        alias_method :delete_all!, :delete_all
        alias_method :destroy!, :destroy
      end
    EOV
  end
end

# Extend ActiveRecord's functionality
ActiveRecord::Base.extend ActsAsParanoid<commit_msg>added callbacks<commit_after>require 'active_record'

module ActsAsParanoid  
  def acts_as_paranoid(options = {})
    raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
    
    configuration = { :column => "deleted_at", :column_type => "time" }
    configuration.update(options) unless options.nil?

    type = case configuration[:column_type]
      when "time" then "Time.now"
      when "boolean" then "true"
      else
        raise ArgumentError, "'time' or 'boolean' expected for :column_type option, got #{configuration[:column_type]}"
    end
    
    class_eval <<-EOV
      default_scope where("#{self.table_name}.#{configuration[:column]} IS ?", nil)

      class << self
        def with_deleted
          self.unscoped.where() # ugly, but it re-fetches the data (no caching)
        end

        def only_deleted
          self.unscoped.
            where("#{self.table_name}.#{configuration[:column]} IS NOT ?
              OR #{self.table_name}.#{configuration[:column]} != ?", nil, true)
        end

        def delete_all!(conditions = nil)
          self.unscoped.delete_all!(conditions)
        end
        
        def delete_all(conditions = nil)
          update_all ["#{configuration[:column]} = ?", #{type}], conditions
        end
      end

      def destroy!
        before_destroy() if respond_to?(:before_destroy)
        
        #{self.name}.delete_all!(:id => self)
        
        after_destroy() if respond_to?(:after_destroy)
      end
      
      def destroy
        before_destroy() if respond_to?(:before_destroy)
        
        if self.#{configuration[:column]} == nil
          #{self.name}.delete_all(:id => self.id)
        else
          #{self.name}.delete_all!(:id => self.id)
        end
        
        after_destroy() if respond_to?(:after_destroy)
      end

      def recover
        self.update_attribute(:#{configuration[:column]}, nil)
      end
      
      ActiveRecord::Relation.class_eval do
        alias_method :delete_all!, :delete_all
        alias_method :destroy!, :destroy
      end
    EOV
  end
end

# Extend ActiveRecord's functionality
ActiveRecord::Base.extend ActsAsParanoid
{'fraction_non_alphanumeric': 0.11714156490275893, 'fraction_numerical': 0.0, 'mean_word_length': 2.4393468118195956, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '3372491', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1280, 'n_words': 292}