Sha256: 975ccc44fca10c9116f9aad9c7c666d9a690e47e99bce6294f3b573e73499f3f

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

module Spree
  module Core
    module Search
      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_terms(word)).result.pluck(:id)
          end

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

        private

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
solidus_core-1.0.7 lib/spree/core/search/variant.rb
solidus_core-1.0.6 lib/spree/core/search/variant.rb
solidus_core-1.0.5 lib/spree/core/search/variant.rb
solidus_core-1.0.4 lib/spree/core/search/variant.rb
solidus_core-1.0.3 lib/spree/core/search/variant.rb
solidus_core-1.0.2 lib/spree/core/search/variant.rb
solidus_core-1.0.1 lib/spree/core/search/variant.rb
solidus_core-1.0.0 lib/spree/core/search/variant.rb
solidus_core-1.0.0.rc2 lib/spree/core/search/variant.rb
solidus_core-1.0.0.rc1 lib/spree/core/search/variant.rb
solidus_core-1.0.0.pre3 lib/spree/core/search/variant.rb
solidus_core-1.0.0.pre2 lib/spree/core/search/variant.rb
solidus_core-1.0.0.pre lib/spree/core/search/variant.rb