Class: Spree::Core::Search::Variant

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/core/search/variant.rb

Overview

NOTE: Use Spree::Config.variant_search_class rather than referencing this directly.

Instance Method Summary collapse

Constructor Details

#initialize(query_string, scope: Spree::Variant.all) ⇒ Variant

Returns a new instance of Variant



21
22
23
24
# File 'lib/spree/core/search/variant.rb', line 21

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

Instance Method Details

#resultsObject

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



33
34
35
36
37
38
39
40
41
# File 'lib/spree/core/search/variant.rb', line 33

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