<commit_before>require "has_archive/version"
require "has_archive/railtie" if defined?(Rails)
require "has_archive/hook"

module HasArchive
  def self.included(base)
    base.send :extend, ClassMethods
    base.send :include, InstanceMethods

    eval <<-EVAL
      class #{base}::Archive < #{base}
        self.table_name = "#{base.to_s.underscore}_archives"

        def restore
          attrs = self.attributes
          attrs.delete('archived_at')
          restored = self.class.parent.new(attrs)
          restored.save && self.destroy(for_real: true)
          self
        end
      end
    EVAL
  end

  module ClassMethods
    def archived
      union = self.unscoped.union(self::Archive.unscoped.select(self.attribute_names.map(&:to_sym)))
      self.from(self.arel_table.create_table_alias(union, self.table_name.to_sym).to_sql)
    end
  end

  module InstanceMethods
    def archive
      archive = self.class::Archive.new(self.attributes)
      archive.archived_at = Time.now
      archive.save(validate: false)
      self.destroy(for_real: true)
    end

    def destroy(for_real: false)
      if !for_real && Rails.configuration.has_archive.override_destroy
        archive
      else
        super()
      end
    end
  end<commit_msg>use semantic name for heredoc (for syntax highlighting)<commit_after>require "has_archive/version"
require "has_archive/railtie" if defined?(Rails)
require "has_archive/hook"

module HasArchive
  def self.included(base)
    base.send :extend, ClassMethods
    base.send :include, InstanceMethods

    eval <<-RUBY
      class #{base}::Archive < #{base}
        self.table_name = "#{base.to_s.underscore}_archives"

        def restore
          attrs = self.attributes
          attrs.delete('archived_at')
          restored = self.class.parent.new(attrs)
          restored.save && self.destroy(for_real: true)
          self
        end
      end
    RUBY
  end

  module ClassMethods
    def archived
      union = self.unscoped.union(self::Archive.unscoped.select(self.attribute_names.map(&:to_sym)))
      self.from(self.arel_table.create_table_alias(union, self.table_name.to_sym).to_sql)
    end
  end

  module InstanceMethods
    def archive
      archive = self.class::Archive.new(self.attributes)
      archive.archived_at = Time.now
      archive.save(validate: false)
      self.destroy(for_real: true)
    end

    def destroy(for_real: false)
      if !for_real && Rails.configuration.has_archive.override_destroy
        archive
      else
        super()
      end
    end
  end
{'fraction_non_alphanumeric': 0.09725490196078432, 'fraction_numerical': 0.0, 'mean_word_length': 2.751470588235294, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '4695048', 'n_tokens_mistral': 802, 'n_tokens_neox': 781, 'n_words': 156}