<commit_before>module RailsJQueryAutocomplete
  module Orm
    module ActiveRecord
      def active_record_get_autocomplete_order(method, options, model=nil)
        order = options[:order]

        table_prefix = model ? "#{model.table_name}." : ""
        if sqlite?
          order || "LOWER(#{method}) ASC"
        else
          order || "LOWER(#{table_prefix}#{method}) ASC"
        end
      end

      def active_record_get_autocomplete_items(parameters)
        model   = parameters[:model]
        term    = parameters[:term]
        options = parameters[:options]
        method  = options[:hstore] ? options[:hstore][:method] : parameters[:method]
        scopes  = Array(options[:scopes])
        where   = options[:where]
        limit   = get_autocomplete_limit(options)
        order   = active_record_get_autocomplete_order(method, options, model)

        items = (::Rails::VERSION::MAJOR * 10 + ::Rails::VERSION::MINOR) >= 40 ? model.where(nil) : model.scoped

        scopes.each { |scope| items = items.send(scope) } unless scopes.empty?

        items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model]
        items = items.where(get_autocomplete_where_clause(model, term, method, options)).
            limit(limit).order(order)
        items = items.where(where) unless where.blank?

        items
      end

      def get_autocomplete_select_clause(model, method, options)
        if sqlite?
          table_name = model.quoted_table_name
          ([
              "#{table_name}.#{model.connection.quote_column_name(model.primary_key)} as #{model.primary_key}",
              "#{table_name}.#{model.connection.quote_column_name(method)} as #{method}"
            ] + (options[:extra_data].blank? ? [] : options[:extra_data]))
        else
          table_name = model.table_name
          (["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data]))
        end
      end

      def get_autocomplete_where_clause(model, term, method, options)
        table_name = model.table_name
        is_full_search = options[:full]
        is_case_sensitive_search = options[:case_sensitive]
        like_clause = (postgres?(model) && !is_case_sensitive_search ? 'ILIKE' : 'LIKE')<commit_msg>Fix #45 Make scopes option can accepts Proc<commit_after>module RailsJQueryAutocomplete
  module Orm
    module ActiveRecord
      def active_record_get_autocomplete_order(method, options, model=nil)
        order = options[:order]

        table_prefix = model ? "#{model.table_name}." : ""
        if sqlite?
          order || "LOWER(#{method}) ASC"
        else
          order || "LOWER(#{table_prefix}#{method}) ASC"
        end
      end

      def active_record_get_autocomplete_items(parameters)
        model   = parameters[:model]
        term    = parameters[:term]
        options = parameters[:options]
        method  = options[:hstore] ? options[:hstore][:method] : parameters[:method]
        scopes  = Array(options[:scopes])
        where   = options[:where]
        limit   = get_autocomplete_limit(options)
        order   = active_record_get_autocomplete_order(method, options, model)

        items = (::Rails::VERSION::MAJOR * 10 + ::Rails::VERSION::MINOR) >= 40 ? model.where(nil) : model.scoped

        items = items.select(get_autocomplete_select_clause(model, method, options)) unless options[:full_model]
        items = items.where(get_autocomplete_where_clause(model, term, method, options)).
            limit(limit).order(order)
        items = items.where(where) unless where.blank?

        scopes.each do |scope|
          items = case scope
                  when String
                    items.send(scope)
                  when Proc
                    items.instance_exec(&scope)
                  end
        end

        items
      end

      def get_autocomplete_select_clause(model, method, options)
        if sqlite?
          table_name = model.quoted_table_name
          ([
              "#{table_name}.#{model.connection.quote_column_name(model.primary_key)} as #{model.primary_key}",
              "#{table_name}.#{model.connection.quote_column_name(method)} as #{method}"
            ] + (options[:extra_data].blank? ? [] : options[:extra_data]))
        else
          table_name = model.table_name
          (["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data]))
        end
      end

      def get_autocomplete_where_clause(model, term, method, options)
        table_name = model.table_name
        is_full_search = options[:full]
        is_case_sensitive_search = options[:case_sensitive]
        like_clause = (postgres?(model) && !is_case_sensitive_search ? 'ILIKE' : 'LIKE')
{'fraction_non_alphanumeric': 0.12135621252842671, 'fraction_numerical': 0.002067397146991937, 'mean_word_length': 2.7330246913580245, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5994594', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1398, 'n_words': 254}