Sha256: 2825dd2fbe51a5f4f1a43a8247200005842a8bf00bec3acc5b03046929556baa

Contents?: true

Size: 1.98 KB

Versions: 34

Compression:

Stored size: 1.98 KB

Contents

module Workarea
  module Storefront
    class SearchSuggestionViewModel < ApplicationViewModel
      include I18n::DefaultUrlOptions
      include Storefront::Engine.routes.url_helpers
      include Search::LoadProductResults
      include AnalyticsHelper

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

      def source
        model['_source']
      end

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

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

      # TODO this can be simplified in v4, when we can be confident the index
      # will always have relative paths stored for image cache.
      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

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

      def suggestion_type
        source['type']
      end

      def analytics
        return unless suggestion_type == 'product'

        product_analytics_data(product)
      end

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

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

      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
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-storefront-3.4.45 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.44 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.43 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.42 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.41 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.40 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.39 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.38 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.37 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.36 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.35 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.34 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.33 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.32 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.31 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.30 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.29 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.28 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.27 app/view_models/workarea/storefront/search_suggestion_view_model.rb
workarea-storefront-3.4.26 app/view_models/workarea/storefront/search_suggestion_view_model.rb