<commit_before>module AdvancedSearches
  class Gene
    include Base

    def initialize(params)
      @params = params
      @presentation_class = GeneWithStateParamsPresenter
    end

    def model_class
      ::Gene
    end

    private
    def handler_for_field(field)
      default_handler = method(:default_handler).to_proc
      @handlers ||= {
        'id' => default_handler.curry['genes.id'],
        'entrez_id' => default_handler.curry['genes.entrez_id'],
        'name' => default_handler.curry['genes.name'],
        'description' => default_handler.curry['genes.description'],
        'aliases' => default_handler.curry['gene_aliases.name'],
        'variants' => default_handler.curry['variants.name'],
        'suggested_changes_count' => method(:handle_suggested_changes_count),
      }
      @handlers[field]
    end

    def handle_suggested_changes_count(operation_type, parameters)
      sanitized_status = ActiveRecord::Base.sanitize(parameters.shift)
      having_clause = comparison(operation_type, 'COUNT(DISTINCT(suggested_changes.id))')

      condition = ::Gene.select('genes.id')
        .joins("LEFT OUTER JOIN suggested_changes ON suggested_changes.moderated_id = genes.id AND suggested_changes.status = #{sanitized_status} AND suggested_changes.moderated_type = 'Gene'")
        .group('genes.id')
        .having(having_clause, *parameters.map(&:to_i)).to_sql

      [
        ["genes.id IN (#{condition})"],
        []
      ]
    end
  end
end
<commit_msg>Add advanced search by assertion to genes<commit_after>module AdvancedSearches
  class Gene
    include Base

    def initialize(params)
      @params = params
      @presentation_class = GeneWithStateParamsPresenter
    end

    def model_class
      ::Gene
    end

    private
    def handler_for_field(field)
      default_handler = method(:default_handler).to_proc
      @handlers ||= {
        'id' => default_handler.curry['genes.id'],
        'entrez_id' => default_handler.curry['genes.entrez_id'],
        'name' => default_handler.curry['genes.name'],
        'description' => default_handler.curry['genes.description'],
        'aliases' => default_handler.curry['gene_aliases.name'],
        'variants' => default_handler.curry['variants.name'],
        'suggested_changes_count' => method(:handle_suggested_changes_count),
        'assertion_count' => method(:handle_assertion_count),
      }
      @handlers[field]
    end

    def handle_suggested_changes_count(operation_type, parameters)
      sanitized_status = ActiveRecord::Base.sanitize(parameters.shift)
      having_clause = comparison(operation_type, 'COUNT(DISTINCT(suggested_changes.id))')

      condition = ::Gene.select('genes.id')
        .joins("LEFT OUTER JOIN suggested_changes ON suggested_changes.moderated_id = genes.id AND suggested_changes.status = #{sanitized_status} AND suggested_changes.moderated_type = 'Gene'")
        .group('genes.id')
        .having(having_clause, *parameters.map(&:to_i)).to_sql

      [
        ["genes.id IN (#{condition})"],
        []
      ]
    end

    def handle_assertion_count(operation_type, parameters)
      query = ::Gene.select('genes.id')
        .joins(:assertions).to_sql

      if operation_type == 'is'
        [
          ["genes.id IN (#{query})"],
          []
        ]
      elsif operation_type == 'is_not'
        [
          ["genes.id NOT IN (#{query})"],
          []
        ]
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.1227866473149492, 'fraction_numerical': 0.0, 'mean_word_length': 3.063679245283019, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, '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': '506947', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1031, 'n_words': 171}