<commit_before># encoding: UTF-8

require "active_support/core_ext/module/delegation"

module PgSearch
  class ScopeOptions
    attr_reader :config, :feature_options

    def initialize(config)
      @config = config
      @model = config.model
      @feature_options = config.feature_options
    end

    def apply(scope)
      scope.
        select("#{quoted_table_name}.*, (#{rank}) AS pg_search_rank").
        where(conditions).
        order("pg_search_rank DESC, #{order_within_rank}").
        joins(joins).
        extend(DisableEagerLoading)
    end

    # workaround for https://github.com/Casecommons/pg_search/issues/14
    module DisableEagerLoading
      def eager_loading?
        return false
      end
    end

    private

    delegate :connection, :quoted_table_name, :to => :@model

    def conditions
      config.features.map do |feature_name, feature_options|
        feature_for(feature_name).conditions
      end.inject do |accumulator, expression|
        Arel::Nodes::Or.new(accumulator, expression)
      end
    end

    def order_within_rank
      config.order_within_rank || "#{primary_key} ASC"
    end

    def primary_key
      "#{quoted_table_name}.#{connection.quote_column_name(@model.primary_key)}"
    end

    def joins
      config.associations.map do |association|
        association.join(primary_key)
      end.join(' ')
    end<commit_msg>Workaround issue with Arel OR nodes and where()<commit_after># encoding: UTF-8

require "active_support/core_ext/module/delegation"

module PgSearch
  class ScopeOptions
    attr_reader :config, :feature_options

    def initialize(config)
      @config = config
      @model = config.model
      @feature_options = config.feature_options
    end

    def apply(scope)
      scope.
        select("#{quoted_table_name}.*, (#{rank}) AS pg_search_rank").
        where(conditions).
        order("pg_search_rank DESC, #{order_within_rank}").
        joins(joins).
        extend(DisableEagerLoading)
    end

    # workaround for https://github.com/Casecommons/pg_search/issues/14
    module DisableEagerLoading
      def eager_loading?
        return false
      end
    end

    private

    delegate :connection, :quoted_table_name, :to => :@model

    def conditions
      config.features.map do |feature_name, feature_options|
        feature_for(feature_name).conditions
      end.inject do |accumulator, expression|
        Arel::Nodes::Or.new(accumulator, expression).to_sql
      end
    end

    def order_within_rank
      config.order_within_rank || "#{primary_key} ASC"
    end

    def primary_key
      "#{quoted_table_name}.#{connection.quote_column_name(@model.primary_key)}"
    end

    def joins
      config.associations.map do |association|
        association.join(primary_key)
      end.join(' ')
    end
{'fraction_non_alphanumeric': 0.09569548203486304, 'fraction_numerical': 0.0021344717182497333, 'mean_word_length': 2.8786206896551723, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '6621271', 'n_tokens_mistral': 870, 'n_tokens_neox': 839, 'n_words': 176}