<commit_before>module Kakurenbo
  module SoftDeleteCore
    # Extend ClassMethods after include.
    def self.included(base_class)
      base_class.extend ClassMethods
      base_class.extend Callbacks
      base_class.extend Scopes
      base_class.extend Aliases
    end

    module ClassMethods
      def paranoid?
        true
      end

      # Destroy model(s).
      #
      # @param id [Array<Integer> or Integer] id or ids
      def destroy(id)
        transaction do
          where(:id => id).each{|m| m.destroy}
        end
      end

      # Restore model(s).
      #
      # @param id      [Array<Integer> or Integer] id or ids.
      # @param options [Hash] options(same restore of instance methods.)
      def restore(id, options = {:recursive => true})
        transaction do
          only_deleted.where(:id => id).each{|m| m.restore!(options)}
        end
      end
    end

    module Callbacks
      def self.extended(base_class)
        # Regist callbacks.
        [:before, :around, :after].each do |on|
          base_class.define_singleton_method("#{on}_restore") do |*args, &block|
            set_callback(:restore, on, *args, &block)
          end
        end
        base_class.define_callbacks :restore
      end
    end

    module Scopes
      def self.extended(base_class)
        base_class.instance_eval {
          scope :only_deleted,    ->{ with_deleted.where.not( kakurenbo_column => nil ) }
          scope :without_deleted, ->{ where(kakurenbo_column => nil) }
          if ActiveRecord::VERSION::STRING >= "4.1"
            scope :with_deleted,  ->{ unscope :where => kakurenbo_column }
          else
            scope :with_deleted,  ->{ all.tap{ |s| s.default_scoped = false } }
          end

          default_scope ->{ without_deleted }
        }
      end
    end

    module Aliases
      def self.extended(base_class)
        base_class.instance_eval {
          alias_method :delete!,  :hard_delete!
          alias_method :deleted?, :destroyed?
          alias_method :restore,  :restore!
          alias_method :recover,  :restore!
        }
      end
    end<commit_msg>Change default_scope from without_deleted to with_deleted<commit_after>module Kakurenbo
  module SoftDeleteCore
    # Extend ClassMethods after include.
    def self.included(base_class)
      base_class.extend ClassMethods
      base_class.extend Callbacks
      base_class.extend Scopes
      base_class.extend Aliases
    end

    module ClassMethods
      def paranoid?
        true
      end

      # Destroy model(s).
      #
      # @param id [Array<Integer> or Integer] id or ids
      def destroy(id)
        transaction do
          where(:id => id).each{|m| m.destroy}
        end
      end

      # Restore model(s).
      #
      # @param id      [Array<Integer> or Integer] id or ids.
      # @param options [Hash] options(same restore of instance methods.)
      def restore(id, options = {:recursive => true})
        transaction do
          only_deleted.where(:id => id).each{|m| m.restore!(options)}
        end
      end
    end

    module Callbacks
      def self.extended(base_class)
        # Regist callbacks.
        [:before, :around, :after].each do |on|
          base_class.define_singleton_method("#{on}_restore") do |*args, &block|
            set_callback(:restore, on, *args, &block)
          end
        end
        base_class.define_callbacks :restore
      end
    end

    module Scopes
      def self.extended(base_class)
        base_class.instance_eval {
          scope :only_deleted,    ->{ with_deleted.where.not( kakurenbo_column => nil ) }
          scope :without_deleted, ->{ where(kakurenbo_column => nil) }
          if ActiveRecord::VERSION::STRING >= "4.1"
            scope :with_deleted,  ->{ unscope :where => kakurenbo_column }
          else
            scope :with_deleted,  ->{ all.tap{ |s| s.default_scoped = false } }
          end

          # NOTE: デフォルトスコープを論理削除除外にするとbelongs_toやhas_manyに
          # 論理削除したレコードを含めるようにするにはbelongs_toやhas_manyの
          # 代わりに自前でメソッドを定義して回る必要がある。
          # 今回の利用シーンではほとんどの箇所では論理削除されたレコードを
          # 見せたいので、デフォルトスコープは倫理削除されたレコードを含めるようにする
          # default_scope ->{ without_deleted }
          default_scope ->{ with_deleted }
        }
      end
    end

    module Aliases
      def self.extended(base_class)
        base_class.instance_eval {
          alias_method :delete!,  :hard_delete!
          alias_method :deleted?, :destroyed?
          alias_method :restore,  :restore!
          alias_method :recover,  :restore!
        }
      end
    end
{'fraction_non_alphanumeric': 0.09229762164521056, 'fraction_numerical': 0.0008727907484180668, 'mean_word_length': 2.074446680080483, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 32, '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': '6794468', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1441, 'n_words': 318}