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.25 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.24 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.23 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.22 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.21 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.20 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.19 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.18 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.17 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.16 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.15 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.14 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.13 app/controllers/workarea/storefront/searches_controller.rb
workarea-storefront-3.4.12 app/controllers/workarea/storefront/searches_controller.rb