Class: Showroom::Search::Suggestion

Inherits:
Resource
  • Object
show all
Defined in:
lib/showroom/models/search/suggestion.rb

Overview

Base class for all search suggestion types.

Subclasses that correspond to a fully-loadable model override Suggestion.complete_model_class to declare which model to fetch. Calling #load on a suggestion that has no model raises NoMethodError.

Examples:

Loadable suggestion

suggestion = result.products.first
suggestion.load  # => Showroom::Product

Non-loadable suggestion

suggestion = result.queries.first
suggestion.load  # => NoMethodError

Instance Attribute Summary

Attributes inherited from Resource

#attrs, #client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, #[], has_many, has_one, #initialize, #inspect, main_attr_keys, main_attrs, #method_missing, #respond_to_missing?, #to_h

Constructor Details

This class inherits a constructor from Showroom::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Showroom::Resource

Class Method Details

.complete_model_classClass

Override in subclasses to declare the corresponding full model class.

Returns:

  • (Class)

Raises:

  • (NoMethodError)

    when the suggestion type has no associated model



24
25
26
# File 'lib/showroom/models/search/suggestion.rb', line 24

def complete_model_class
  raise NoMethodError, "#{name} has no associated model — #load is not available"
end

Instance Method Details

#loadResource

Fetches the full model record for this suggestion.

Delegates to the model class declared by complete_model_class, finding by #loadable_identifier.

Returns:

Raises:

  • (NoMethodError)

    when the suggestion type has no associated model

  • (Showroom::NotFound)

    when the record is not found



45
46
47
# File 'lib/showroom/models/search/suggestion.rb', line 45

def load
  self.class.complete_model_class.find(loadable_identifier)
end

#loadable_identifierString

Returns the identifier used to fetch the full record (defaults to the handle attribute). Override in subclasses when the identifier differs.

Returns:

  • (String)


33
34
35
# File 'lib/showroom/models/search/suggestion.rb', line 33

def loadable_identifier
  @attrs['handle']
end