Sha256: 69a66383a38618bed8036702e3285765516671030d1f3dfe6fcfb793fe061b64

Contents?: true

Size: 1.44 KB

Versions: 22

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Blacklight
  module Response
    # Render spellcheck results for a search query
    class SpellcheckComponent < Blacklight::Component
      # @param [Blacklight::Response] response
      # @param [Array<String>] options explicit spellcheck options to render
      def initialize(response:, options: nil)
        @response = response
        @options = options
        @options ||= options_from_response(@response)
      end

      def link_to_query(query)
        p = helpers.search_state.to_h.except(:page, :action)
        p[:q] = query
        link_to(query, helpers.search_action_path(p))
      end

      def render?
        Array(@options).any? && show_spellcheck_suggestions?(@response)
      end

      # @!group Search result helpers
      ##
      # Determine whether to display spellcheck suggestions
      #
      # @param [Blacklight::Solr::Response] response
      # @return [Boolean]
      def show_spellcheck_suggestions? response
        # The spelling response field may be missing from non solr repositories.
        response.total <= helpers.blacklight_config.spell_max &&
          !response.spelling.nil? &&
          response.spelling.words.any?
      end

      private

      def options_from_response(response)
        if response&.spelling&.collation
          [response.spelling.collation]
        elsif response&.spelling&.words
          response.spelling.words
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
blacklight-8.8.2 app/components/blacklight/response/spellcheck_component.rb
blacklight-9.0.0.beta1 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.8.1 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.8.0 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.7.0 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.6.1 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.6.0 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.5.1 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.5.0 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.4.0 app/components/blacklight/response/spellcheck_component.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/blacklight-8.3.0/app/components/blacklight/response/spellcheck_component.rb
blacklight-8.3.0 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.2.2 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.2.1 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.2.0 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.1.0 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.0.1 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.0.0 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.0.0.beta8 app/components/blacklight/response/spellcheck_component.rb
blacklight-8.0.0.beta7 app/components/blacklight/response/spellcheck_component.rb