Class: Workarea::Search::StorefrontSearch::SpellingCorrection

Inherits:
Object
  • Object
show all
Includes:
Middleware
Defined in:
app/queries/workarea/search/storefront_search/spelling_correction.rb

Instance Method Summary collapse

Methods included from Middleware

#initialize

Instance Method Details

#any_results?(response) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/queries/workarea/search/storefront_search/spelling_correction.rb', line 25

def any_results?(response)
  response.total > 0
end

#call(response) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/queries/workarea/search/storefront_search/spelling_correction.rb', line 7

def call(response)
  if correction = find_spell_correction(response)
    response.reset!(params.merge(q: correction), by: self)
    response.message = I18n.t(
      'workarea.storefront.searches.showing_suggested_results',
      suggestion: correction,
      original: params[:q].to_s.strip
    )
  end

  yield
end

#find_spell_correction(response) ⇒ Object



20
21
22
23
# File 'app/queries/workarea/search/storefront_search/spelling_correction.rb', line 20

def find_spell_correction(response)
  return nil if response.has_filters? || any_results?(response)
  response.query.query_suggestions.first
end