class Spree::Core::Search::Variant
NOTE: Use Spree::Config.variant_search_class rather than referencing this directly.
Public Class Methods
new(query_string, scope: Spree::Variant.all)
click to toggle source
# File lib/spree/core/search/variant.rb, line 23 def initialize(query_string, scope: Spree::Variant.all) @query_string = query_string @scope = scope end
Public Instance Methods
results()
click to toggle source
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
# 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
Private Instance Methods
search_term_params(word)
click to toggle source
# File lib/spree/core/search/variant.rb, line 59 def search_term_params(word) terms = Hash[search_terms(word).map { |term| [term, word] }] terms.merge(m: 'or') end
search_terms(_word)
click to toggle source
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
# File lib/spree/core/search/variant.rb, line 55 def search_terms(_word) self.class.search_terms end