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.



23
24
25
26
# File 'lib/spree/core/search/variant.rb', line 23

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



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

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