Class: Workarea::Storefront::SearchSuggestionViewModel

Inherits:
ApplicationViewModel
  • Object
show all
Includes:
I18n::DefaultUrlOptions, Search::LoadProductResults, AnalyticsHelper
Defined in:
app/view_models/workarea/storefront/search_suggestion_view_model.rb

Instance Method Summary collapse

Methods included from AnalyticsHelper

#add_to_cart_analytics_data, #add_to_cart_confirmation_analytics_data, #cart_view_analytics_data, #category_view_analytics_data, #checkout_addresses_view_analytics_data, #checkout_edit_analytics_data, #checkout_login_analytics_data, #checkout_order_placed_analytics_data, #checkout_payment_selected_analytics_data, #checkout_payment_view_analytics_data, #checkout_shipping_service_selected_analytics_data, #checkout_shipping_view_analytics_data, #checkout_signup_analytics_data, #content_block_analytics_data, #email_signup_analytics_data, #forgot_password_analytics_data, #login_analytics_data, #order_analytics_data, #order_item_analytics_data, #primary_navigation_analytics_data, #product_analytics_data, #product_click_analytics_data, #product_list_analytics_data, #product_view_analytics_data, #remove_from_cart_analytics_data, #search_results_view_analytics_data, #signup_analytics_data, #update_cart_item_analytics_data, #validation_error_analytics_data

Instance Method Details

#analyticsObject



56
57
58
59
60
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 56

def analytics
  return unless suggestion_type == 'product'

  product_analytics_data(product)
end

#asset_hostObject



46
47
48
49
50
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 46

def asset_host
  URI.parse(Rails.application.config.action_controller.asset_host)
rescue URI::InvalidURIError
  nil
end

#imageObject

TODO this can be simplified in v4, when we can be confident the index will always have relative paths stored for image cache.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 33

def image
  return if source['cache']['image'].blank?

  image_url = URI.parse(source['cache']['image'])

  if asset_host.present?
    image_url.scheme = asset_host.scheme
    image_url.host = asset_host.host
  end

  image_url.to_s
end

#nameObject



23
24
25
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 23

def name
  source['content']['name']
end

#productObject



62
63
64
65
66
67
68
69
70
71
72
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 62

def product
  @product ||=
    begin
      loaded = load_model_from(model)

      Storefront::ProductViewModel.wrap(
        loaded[:model],
        loaded.slice(:inventory, :pricing)
      )
    end
end

#sourceObject



19
20
21
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 19

def source
  model['_source']
end

#suggestion_typeObject



52
53
54
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 52

def suggestion_type
  source['type']
end

#to_hObject



9
10
11
12
13
14
15
16
17
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 9

def to_h
  {
    value: name,
    type: type,
    image: image,
    url: url,
    analytics: analytics
  }
end

#typeObject



27
28
29
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 27

def type
  t("workarea.storefront.searches.#{suggestion_type.pluralize}")
end

#urlObject



74
75
76
77
78
79
80
81
82
83
84
# File 'app/view_models/workarea/storefront/search_suggestion_view_model.rb', line 74

def url
  if suggestion_type == 'product'
    product_path(product)
  elsif suggestion_type == 'search'
    search_path(q: name)
  elsif suggestion_type == 'category'
    category_path(source['slug'])
  elsif suggestion_type == 'page'
    page_path(source['slug'])
  end
end