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

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Variant.



15
16
17
18
# File 'lib/spree/core/search/variant.rb', line 15

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



27
28
29
30
31
32
33
34
35
# File 'lib/spree/core/search/variant.rb', line 27

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