diff --git a/app/helpers/iiif_print/application_helper.rb b/app/helpers/iiif_print/application_helper.rb index f48f67e2..4fa5de77 100644 --- a/app/helpers/iiif_print/application_helper.rb +++ b/app/helpers/iiif_print/application_helper.rb @@ -1,5 +1,25 @@ module IiifPrint # Application Helper module module ApplicationHelper + ## + # This is in place to coerce the :q string to :query for passing the :q value to the query value + # of a IIIF Print manifest. + # + # @param doc [SolrDocument] + # @param request [ActionDispatch::Request] + # + # @return [String] + def generate_work_url(doc, request) + url = super + return url if request.params[:q].blank? + + key = doc.any_highlighting? ? 'parent_query' : 'query' + query = { key => request.params[:q] }.to_query + if url.include?("?") + url + "&#{query}" + else + url + "?#{query}" + end + end end end diff --git a/spec/helpers/iiif_print/application_helper_spec.rb b/spec/helpers/iiif_print/application_helper_spec.rb new file mode 100644 index 00000000..cbc19018 --- /dev/null +++ b/spec/helpers/iiif_print/application_helper_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ApplicationHelper do + let(:helper) { _view } + + let(:cname) { 'hyku-me.test' } + let(:account) { build(:search_only_account, cname: cname) } + + let(:uuid) { SecureRandom.uuid } + let(:request) do + instance_double(ActionDispatch::Request, + port: 3000, + protocol: "https://", + host: account.cname, + params: { q: q }) + end + let(:doc) { SolrDocument.new(id: uuid, 'has_model_ssim': ['GenericWork'], 'account_cname_tesim': account.cname) } + + before do + allow(helper).to receive(:current_account) { account } + end + + describe '#generate_work_url' do + context 'when params has a "q" parameter' do + let(:q) { "wonka-vision" } + + context 'when any_highlighting? is false' do + it 'passes that along as :query' do + expect(doc).to receive(:any_highlighting?).and_return(false) + expect(helper.generate_work_url(doc, request)).to end_with("?query=#{q}") + end + end + + context 'when any_highlighting? is true' do + it 'passes that along as :parent_query' do + expect(doc).to receive(:any_highlighting?).and_return(true) + expect(helper.generate_work_url(doc, request)).to end_with("?parent_query=#{q}") + end + end + end + end +end