<commit_before>module Spree::Search
  class Base
    attr_accessor :properties

    def initialize(params)
      @properties = {}
      prepare(params)
    end

    def retrieve_products
      base_scope = @cached_product_group ? @cached_product_group.products.active : Product.active
      base_scope = base_scope.in_taxon(taxon) unless taxon.blank? 
      base_scope = get_products_conditions_for(base_scope, keywords) unless keywords.blank?

      base_scope = base_scope.on_hand unless Spree::Config[:show_zero_stock_products]
      @products_scope = @product_group.apply_on(base_scope)

      curr_page = manage_pagination && keywords ? 1 : page
      @products = @products_scope.all.paginate({
          :include  => [:images, :master],
          :per_page => per_page,
          :page     => curr_page
        })

      return @products
    end

    def method_missing(name)
      @properties[name]
    end

    protected
    # method should return new scope based on base_scope
    def get_products_conditions_for(base_scope, query)
      base_scope.like_any([:name, :description], query.split)
    end

    def prepare(params)
      @properties[:taxon] = params[:taxon].blank? ? nil : Taxon.find(params[:taxon])
      @properties[:keywords] = params[:keywords]

      per_page = params[:per_page].to_i
      @properties[:per_page] = per_page > 0 ? per_page : Spree::Config[:products_per_page]
      @properties[:page] = (params[:page].to_i <= 0) ? 1 : params[:page].to_i 
      
      if !params[:order_by_price].blank?
        @product_group = ProductGroup.new.from_route([params[:order_by_price]+"_by_master_price"])
      elsif params[:product_group_name]
        @cached_product_group = ProductGroup.find_by_permalink(params[:product_group_name])
        @product_group = ProductGroup.new
      elsif params[:product_group_query]<commit_msg>Extracted method to get base_scope in Spree::Search::Base class.<commit_after>module Spree::Search
  class Base
    attr_accessor :properties

    def initialize(params)
      @properties = {}
      prepare(params)
    end

    def retrieve_products
      base_scope = get_base_scope
      @products_scope = @product_group.apply_on(base_scope)

      curr_page = manage_pagination && keywords ? 1 : page
      @products = @products_scope.all.paginate({
          :include  => [:images, :master],
          :per_page => per_page,
          :page     => curr_page
        })

      return @products
    end

    def method_missing(name)
      @properties[name]
    end

    protected
    def get_base_scope
      base_scope = @cached_product_group ? @cached_product_group.products.active : Product.active
      base_scope = base_scope.in_taxon(taxon) unless taxon.blank? 
      base_scope = get_products_conditions_for(base_scope, keywords) unless keywords.blank?

      base_scope = base_scope.on_hand unless Spree::Config[:show_zero_stock_products]
      base_scope
    end
    
    # method should return new scope based on base_scope
    def get_products_conditions_for(base_scope, query)
      base_scope.like_any([:name, :description], query.split)
    end

    def prepare(params)
      @properties[:taxon] = params[:taxon].blank? ? nil : Taxon.find(params[:taxon])
      @properties[:keywords] = params[:keywords]

      per_page = params[:per_page].to_i
      @properties[:per_page] = per_page > 0 ? per_page : Spree::Config[:products_per_page]
      @properties[:page] = (params[:page].to_i <= 0) ? 1 : params[:page].to_i 
      
      if !params[:order_by_price].blank?
        @product_group = ProductGroup.new.from_route([params[:order_by_price]+"_by_master_price"])
      elsif params[:product_group_name]
        @cached_product_group = ProductGroup.find_by_permalink(params[:product_group_name])
        @product_group = ProductGroup.new
      elsif params[:product_group_query]
{'fraction_non_alphanumeric': 0.10504420176807072, 'fraction_numerical': 0.0020800832033281333, 'mean_word_length': 3.447398843930636, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '749830', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1163, 'n_words': 214}