<commit_before>require 'active_record/fixtures' # so we can hook it & reset our cache afterwards
require 'active_support/core_ext/object/to_param'

module ConstantTableSaver
  module BaseMethods
    def constant_table(options = {})
      options.assert_valid_keys(:name, :name_prefix, :name_suffix)
      class_attribute :constant_table_options, :instance_writer => false
      self.constant_table_options = options
      
      if ActiveRecord::VERSION::MAJOR == 3
        extend ActiveRecord3ClassMethods
      else
        extend ActiveRecord4ClassMethods
      end
      extend ClassMethods
      extend NameClassMethods if constant_table_options[:name]
      
      klass = defined?(ActiveRecord::FixtureSet) ? ActiveRecord::FixtureSet : ActiveRecord::Fixtures
      class <<klass
        # normally, create_fixtures method gets called exactly once - but unfortunately, it
        # loads the class and does a #respond_to?, which causes us to load and cache before
        # the new records are added, so we need to reset our cache afterwards.
        def create_fixtures_with_constant_tables(*args)
          create_fixtures_without_constant_tables(*args).tap { ConstantTableSaver.reset_all_caches }
        end
        def reset_cache_with_constant_tables(*args)
          reset_cache_without_constant_tables(*args).tap     { ConstantTableSaver.reset_all_caches }
        end
        alias_method_chain :create_fixtures, :constant_tables
        alias_method_chain :reset_cache,     :constant_tables
      end unless klass.respond_to?(:create_fixtures_with_constant_tables)
    end
  end

  def self.reset_all_caches
    klasses = ActiveRecord::Base.respond_to?(:descendants) ? ActiveRecord::Base.descendants : ActiveRecord::Base.send(:subclasses)
    klasses.each {|klass| klass.reset_constant_record_cache! if klass.respond_to?(:reset_constant_record_cache!)}
  end
  
  module ClassMethods
    # Resets the cached records.  Remember that this only affects this process, so while this
    # is useful for running tests, it's unlikely that you can use this in production - you
    # would need to call it on every Rails instance on every Rails server.  Don't use this
    # plugin on if the table isn't really constant!
    def reset_constant_record_cache!
      @constant_record_methods.each {|method_id| (class << self; self; end;).send(:remove_method, method_id)} if @constant_record_methods
      @cached_records = @cached_records_by_id = @constant_record_methods = @cached_blank_scope = @find_by_sql = nil
    end
  end

  module ActiveRecord4ClassMethods
    def find_by_sql(sql, binds = [])
      @find_by_sql ||= {
        :all   => all.to_sql,
        :id    => relation.where(relation.table[primary_key].eq(connection.substitute_at(columns_hash[primary_key], 1))).limit(1).to_sql,
        :oldid => relation.where(relation.table[primary_key].eq(connection.substitute_at(columns_hash[primary_key], 1))).order(relation.table[primary_key].asc).limit(1).to_sql, # used by 4.0
        :first => relation.order(relation.table[primary_key].asc).limit(1).to_sql,
        :last  => relation.order(relation.table[primary_key].desc).limit(1).to_sql,
      }

      _sql = sanitize_sql(sql)
      _sql = _sql.to_sql if sql.respond_to?(:to_sql)

      if binds.empty?
        if _sql == @find_by_sql[:all]
          return @cached_records ||= super(relation.to_sql).each(&:freeze)
        elsif _sql == @find_by_sql[:first]
          return [relation.to_a.first].compact
        elsif _sql == @find_by_sql[:last]
          return [relation.to_a.last].compact
        end

      elsif (_sql == @find_by_sql[:id] || _sql == @find_by_sql[:oldid]) &&
            binds.length == 1 &&
            binds.first.first.is_a?(ActiveRecord::ConnectionAdapters::Column) &&
            binds.first.first.name == primary_key
        @cached_records_by_id ||= relation.to_a.index_by {|record| record.id.to_param}
        return [@cached_records_by_id[binds.first.last.to_param]].compact
      end
<commit_msg>remove some code used to support 4.x < 4.1, seems 4.0.8 (at least) uses the same as 4.1 anyway<commit_after>require 'active_record/fixtures' # so we can hook it & reset our cache afterwards
require 'active_support/core_ext/object/to_param'

module ConstantTableSaver
  module BaseMethods
    def constant_table(options = {})
      options.assert_valid_keys(:name, :name_prefix, :name_suffix)
      class_attribute :constant_table_options, :instance_writer => false
      self.constant_table_options = options
      
      if ActiveRecord::VERSION::MAJOR == 3
        extend ActiveRecord3ClassMethods
      else
        extend ActiveRecord4ClassMethods
      end
      extend ClassMethods
      extend NameClassMethods if constant_table_options[:name]
      
      klass = defined?(ActiveRecord::FixtureSet) ? ActiveRecord::FixtureSet : ActiveRecord::Fixtures
      class <<klass
        # normally, create_fixtures method gets called exactly once - but unfortunately, it
        # loads the class and does a #respond_to?, which causes us to load and cache before
        # the new records are added, so we need to reset our cache afterwards.
        def create_fixtures_with_constant_tables(*args)
          create_fixtures_without_constant_tables(*args).tap { ConstantTableSaver.reset_all_caches }
        end
        def reset_cache_with_constant_tables(*args)
          reset_cache_without_constant_tables(*args).tap     { ConstantTableSaver.reset_all_caches }
        end
        alias_method_chain :create_fixtures, :constant_tables
        alias_method_chain :reset_cache,     :constant_tables
      end unless klass.respond_to?(:create_fixtures_with_constant_tables)
    end
  end

  def self.reset_all_caches
    klasses = ActiveRecord::Base.respond_to?(:descendants) ? ActiveRecord::Base.descendants : ActiveRecord::Base.send(:subclasses)
    klasses.each {|klass| klass.reset_constant_record_cache! if klass.respond_to?(:reset_constant_record_cache!)}
  end
  
  module ClassMethods
    # Resets the cached records.  Remember that this only affects this process, so while this
    # is useful for running tests, it's unlikely that you can use this in production - you
    # would need to call it on every Rails instance on every Rails server.  Don't use this
    # plugin on if the table isn't really constant!
    def reset_constant_record_cache!
      @constant_record_methods.each {|method_id| (class << self; self; end;).send(:remove_method, method_id)} if @constant_record_methods
      @cached_records = @cached_records_by_id = @constant_record_methods = @cached_blank_scope = @find_by_sql = nil
    end
  end

  module ActiveRecord4ClassMethods
    def find_by_sql(sql, binds = [])
      @find_by_sql ||= {
        :all   => all.to_sql,
        :id    => relation.where(relation.table[primary_key].eq(connection.substitute_at(columns_hash[primary_key], 1))).limit(1).to_sql,
        :first => relation.order(relation.table[primary_key].asc).limit(1).to_sql,
        :last  => relation.order(relation.table[primary_key].desc).limit(1).to_sql,
      }

      _sql = sanitize_sql(sql)
      _sql = _sql.to_sql if sql.respond_to?(:to_sql)

      if binds.empty?
        if _sql == @find_by_sql[:all]
          return @cached_records ||= super(relation.to_sql).each(&:freeze)
        elsif _sql == @find_by_sql[:first]
          return [relation.to_a.first].compact
        elsif _sql == @find_by_sql[:last]
          return [relation.to_a.last].compact
        end

      elsif _sql == @find_by_sql[:id] &&
            binds.length == 1 &&
            binds.first.first.is_a?(ActiveRecord::ConnectionAdapters::Column) &&
            binds.first.first.name == primary_key
        @cached_records_by_id ||= relation.to_a.index_by {|record| record.id.to_param}
        return [@cached_records_by_id[binds.first.last.to_param]].compact
      end

{'fraction_non_alphanumeric': 0.09485612426788897, 'fraction_numerical': 0.0038197097020626434, 'mean_word_length': 3.5195627157652476, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, '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': '1047184', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2344, 'n_words': 523}