Sha256: bab6431a68eb2745f3ca72f32ed48ae3de8d71ac5a9f3d4e029d54125d88b63a

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module IiifPrint
  module IiifSearchResponseDecorator
    # Enable the user to search for child metadata in the parent's UV
    # @see https://github.com/scientist-softserv/louisville-hyku/commit/67467e5cf9fdb755f54419f17d3c24c87032d0af
    def annotation_list
      json_results = super

      # Check and process invalid hit
      if json_results&.[]('resources')
        remove_invalid_hit(json_results)
        add_metadata_match(json_results)
      end

      json_results
    end

    def remove_invalid_hit(json_results)
      resources = json_results['resources']
      invalid_hit = resources.detect { |resource| resource["on"].include?(IiifPrint::BlacklightIiifSearch::AnnotationDecorator::INVALID_MATCH_TEXT) }
      return unless invalid_hit

      # Delete invalid hit from resources, remove first hit (which is from the invalid hit), decrement total within
      resources.delete(invalid_hit)
      json_results['hits'].shift
      json_results['within']['total'] -= 1
    end

    def add_metadata_match(json_results)
      json_results['resources'].each do |result_hit|
        next if result_hit['resource'].present?

        # Add resource details if not present
        result_hit['resource'] = {
          "@type": "cnt:ContentAsText",
          "chars": "Metadata match, see sidebar for details"
        }
      end
    end
  end
end
::BlacklightIiifSearch::IiifSearchResponse.prepend(IiifPrint::IiifSearchResponseDecorator)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iiif_print-3.0.1 app/models/iiif_print/iiif_search_response_decorator.rb
iiif_print-3.0.0 app/models/iiif_print/iiif_search_response_decorator.rb