Sha256: a06772af905a0cebd6428588a58664760c0ebd012abce8636374ad7dde424596

Contents?: true

Size: 1.26 KB

Versions: 34

Compression:

Stored size: 1.26 KB

Contents

module Workarea
  module Storefront
    class SearchesController < Storefront::ApplicationController
      before_action :cache_page

      def index
        render nothing: true and return if search_query.blank?
        autocomplete_params = params.permit(:q)

        search = Search::SearchSuggestions.new(autocomplete_params)

        @results = search.results.map do |result|
          SearchSuggestionViewModel.new(result).to_h
        end
      end

      def show
        if search_query.blank?
          flash[:error] = t('workarea.storefront.flash_messages.no_search_query')
          redirect_back fallback_location: root_path
          return
        end

        response = Search::StorefrontSearch.new(params.to_unsafe_h).response
        handle_search_response(response)
      end

      private

      def search_query
        QueryString.new(params[:q]).sanitized
      end

      def handle_search_response(response)
        redirect_to response.redirect and return if response.redirect?

        set_search(response)
        status = response.template == 'no_results' ? :not_found : :ok
        render response.template, status: status
      end

      def set_search(response)
        @search = SearchViewModel.new(response, view_model_options)
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

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