Sha256: 875621bce59895169c7b53a4bb23462e8610fefb4768ae08d630cde177056c9c

Contents?: true

Size: 1.96 KB

Versions: 43

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/class/attribute'

module Spree
  module Core
    module Search
      #
      # NOTE: Use Spree::Config.variant_search_class rather than referencing this
      # directly.
      #

      class Variant
        class_attribute :search_terms
        self.search_terms = [
          :sku_cont,
          :product_name_cont,
          :product_slug_cont,
          :option_values_presentation_cont,
          :option_values_name_cont
        ]

        def initialize(query_string, scope: Spree::Variant.all)
          @query_string = query_string
          @scope = scope
        end

        # Searches the variants table using the ransack 'search_terms' defined on the class.
        # Each word of the query string is searched individually, matching by a union of the ransack
        # search terms, then we find the intersection of those queries, ensuring that each word matches
        # one of the rules.
        #
        # == Returns:
        # ActiveRecord::Relation of variants
        def results
          return @scope if @query_string.blank?

          matches = @query_string.split.map do |word|
            @scope.ransack(search_term_params(word)).result.pluck(:id)
          end

          Spree::Variant.where(id: matches.inject(:&))
        end

        private

        # Returns an array of search term symbols that will be passed to Ransack
        # to query the DB for the given word.
        # Subclasses may override this to allow conditional filtering, etc.
        #
        # @api public
        # @param _word [String] One of the search words provided by the user.
        #   e.g. a SKU
        # @return [Array<Symbol>] the list of search terms to use for this word
        def search_terms(_word)
          self.class.search_terms
        end

        def search_term_params(word)
          terms = Hash[search_terms(word).map { |t| [t, word] }]
          terms.merge(m: 'or')
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
solidus_core-2.8.1 lib/spree/core/search/variant.rb
solidus_core-2.6.3 lib/spree/core/search/variant.rb
solidus_core-2.8.0 lib/spree/core/search/variant.rb
solidus_core-2.7.1 lib/spree/core/search/variant.rb
solidus_core-2.6.2 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.10 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.9 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.8 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.7 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.6 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.5 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.4 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.3 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.1 lib/spree/core/search/variant.rb
solidus_core_devise_token_auth-2.8.0.alpha.0 lib/spree/core/search/variant.rb
solidus_core-2.7.0 lib/spree/core/search/variant.rb