<commit_before>class AdminAssistant
  class ActiveRecordColumn < Column
    def initialize(ar_column)
      @ar_column = ar_column
    end
    
    def add_to_query_condition(ar_query_condition, search)
      table_name = search.model_class.table_name
      if search.blank?(name)
        ar_query_condition.add_condition do |sub_cond|
          sub_cond.boolean_join = :or
          sub_cond.sqls << "#{table_name}.#{name} is null"
          sub_cond.sqls << "#{table_name}.#{name} = ''"
        end
      else
        value_for_query = search.send(@ar_column.name)
        unless value_for_query.nil?
          if comp = search.comparator(name)
            ar_query_condition.sqls << "#{table_name}.#{name} #{comp} ?"
            ar_query_condition.bind_vars << value_for_query
          else
            case field_type
              when :boolean, :integer
                ar_query_condition.sqls << "#{table_name}.#{name} = ?"
                ar_query_condition.bind_vars << value_for_query
              else
                ar_query_condition.sqls << "LOWER(#{table_name}.#{name}) like LOWER(?)"
                ar_query_condition.bind_vars << "%#{value_for_query}%"
            end
          end
        end
      end
    end
      
    def attributes_for_search_object(search_params)
      value = if field_type == :datetime
        begin
          Time.utc(
            *(1..5).to_a.map { |i| search_params["#{name}(#{i}i)"].to_i }
          )
        rescue ArgumentError; end
      else
        terms = search_params[@ar_column.name]
        unless terms.blank?
          case field_type
            when :boolean
              terms.blank? ? nil : (terms == 'true')
            else
              terms
          end
        end
      end
      {name => value}
    end
    
    def contains?(column_name)
      column_name.to_s == @ar_column.name
    end
    
    def field_type
      @ar_column.type
    end
    
    def name
      @ar_column.name
    end
    
    class FormView < AdminAssistant::Column::View
      include AdminAssistant::Column::FormViewMethods
      
      def check_box_html(form)
        form.check_box name
      end
      
      def date_select_html(form)
        form.date_select(
          name, {:include_blank => true}.merge(@date_select_options)
        )
      end<commit_msg>i likes me some refactor<commit_after>class AdminAssistant
  class ActiveRecordColumn < Column
    def initialize(ar_column)
      @ar_column = ar_column
    end
    
    def add_to_query_condition(ar_query_condition, search)
      ConditionUpdate.new(ar_query_condition, search, name, field_type).run
    end
      
    class ConditionUpdate
      def initialize(ar_query_condition, search, name, field_type)
        @ar_query_condition, @search, @name, @field_type =
            ar_query_condition, search, name, field_type
      end
      
      def add_blank_condition
        @ar_query_condition.add_condition do |sub_cond|
          sub_cond.boolean_join = :or
          sub_cond.sqls << "#{table_name}.#{@name} is null"
          sub_cond.sqls << "#{table_name}.#{@name} = ''"
        end
      end
      
      def add_case_insensitive_string_comparison
        @ar_query_condition.sqls <<
            "LOWER(#{table_name}.#{@name}) like LOWER(?)"
        @ar_query_condition.bind_vars << "%#{value_for_query}%"
      end
      
      def add_comparative_condition
        @ar_query_condition.sqls << "#{table_name}.#{@name} #{comparator} ?"
        @ar_query_condition.bind_vars << value_for_query
      end
      
      def add_equality_condition
        @ar_query_condition.sqls << "#{table_name}.#{@name} = ?"
        @ar_query_condition.bind_vars << value_for_query
      end
      
      def comparator
        @search.comparator @name
      end
      
      def column_is_a_string_type?
        ![:boolean, :integer].include?(@field_type)
      end
      
      def run
        if @search.blank?(@name)
          add_blank_condition
        else
          unless value_for_query.nil?
            if comparator
              add_comparative_condition
            elsif column_is_a_string_type?
              add_case_insensitive_string_comparison
            else
              add_equality_condition
            end
          end
        end
      end
      
      def table_name
        @search.model_class.table_name
      end
      
      def value_for_query
        @search.send @name
      end
    end
      
    def attributes_for_search_object(search_params)
      value = if field_type == :datetime
        datetime_attributes_for_search_object search_params
      else
        terms = search_params[@ar_column.name]
        unless terms.blank?
          case field_type
            when :boolean
              terms.blank? ? nil : (terms == 'true')
            else
              terms
          end
        end
      end
      {name => value}
    end
    
    def contains?(column_name)
      column_name.to_s == @ar_column.name
    end
    
    def datetime_attributes_for_search_object(search_params)
      begin
        Time.utc(
          *(1..5).to_a.map { |i| search_params["#{name}(#{i}i)"].to_i }
        )
      rescue ArgumentError; end
    end
    
    def field_type
      @ar_column.type
    end
    
    def name
      @ar_column.name
    end
    
    class FormView < AdminAssistant::Column::View
      include AdminAssistant::Column::FormViewMethods
      
      def check_box_html(form)
        form.check_box name
      end
      
      def date_select_html(form)
        form.date_select(
          name, {:include_blank => true}.merge(@date_select_options)
        )
      end
{'fraction_non_alphanumeric': 0.08738036157390996, 'fraction_numerical': 0.0007089684509039348, 'mean_word_length': 1.893846153846154, 'pattern_counts': {'":': 0, '<': 39, '<?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': '6260754', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1701, 'n_words': 315}