Skip to content

Commit

Permalink
Use fielded search to link_to_field. Fixes #2451
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 11, 2016
1 parent 58376e4 commit 5eab1bc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 29 deletions.
6 changes: 3 additions & 3 deletions app/helpers/sufia/sufia_helper_behavior.rb
Expand Up @@ -89,7 +89,7 @@ def link_to_facet_list(values, solr_field, empty_message = "No value entered".ht
# @see Blacklight::SearchState#initialize
def link_to_field(name, value, label = nil, facet_hash = {})
label ||= value
params = { search_field: 'advanced', name => "\"#{value}\"" }
params = { search_field: name, q: "\"#{value}\"" }
state = search_state_with_facets(params, facet_hash)
link_to(label, main_app.search_catalog_path(state))
end
Expand All @@ -115,8 +115,8 @@ def human_readable_date(options)
def index_field_link(options)
raise ArgumentError unless options[:config] && options[:config][:field_name]
name = options[:config][:field_name]
values = options[:value]
safe_join(values.map { |item| link_to_field(name, item, item) }, ", ".html_safe)
links = options[:value].map { |item| link_to_field(name, item, item) }
safe_join(links, ", ")
end

# Uses Rails auto_link to add links to fields
Expand Down
4 changes: 2 additions & 2 deletions app/search_builders/sufia/catalog_search_builder.rb
Expand Up @@ -6,8 +6,8 @@ class Sufia::CatalogSearchBuilder < Sufia::SearchBuilder

# show both works that match the query and works that contain files that match the query
def show_works_or_works_that_contain_files(solr_parameters)
return if solr_parameters[:q].blank?
solr_parameters[:user_query] = solr_parameters[:q]
return if blacklight_params[:q].blank? || blacklight_params[:search_field]
solr_parameters[:user_query] = blacklight_params[:q]
solr_parameters[:q] = new_query
end

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/sufia/templates/catalog_controller.rb
Expand Up @@ -237,7 +237,7 @@ def self.modified_field
end

config.add_search_field('depositor') do |field|
solr_name = solr_name("depositor", :stored_searchable)
solr_name = solr_name("depositor", :symbol)
field.solr_local_parameters = {
qf: solr_name,
pf: solr_name
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/blacklight_helper_spec.rb
Expand Up @@ -94,7 +94,7 @@ def search_action_path(stuff)

context "identifier_tesim" do
let(:field_name) { 'identifier_tesim' }
it { is_expected.to eq '<a href="/catalog?identifier=%2265434567654345654%22&amp;search_field=advanced">65434567654345654</a>' }
it { is_expected.to eq '<a href="/catalog?q=%2265434567654345654%22&amp;search_field=identifier">65434567654345654</a>' }
end
end
end
Expand Down
17 changes: 10 additions & 7 deletions spec/helpers/sufia_helper_spec.rb
Expand Up @@ -51,16 +51,19 @@ def new_state
end

describe '#index_field_link' do
let(:args) { { config: { field_name: 'contributor' }, value: ['Fritz Lang', 'Mel Brooks'] } }
it 'requires 1 arg' do
expect { helper.index_field_link }.to raise_error ArgumentError
expect { helper.index_field_link({}, 'junk') }.to raise_error ArgumentError
let(:args) do
{
config: { field_name: 'contributor' },
value: ['Fritz Lang', 'Mel Brooks']
}
end

subject { helper.index_field_link(args) }
it 'returns link' do

it 'returns a link' do
expect(subject).to be_html_safe
expect(subject).to eq '<a href="/catalog?contributor=%22Fritz+Lang%22&amp;search_field=advanced">Fritz Lang</a>, ' \
+ '<a href="/catalog?contributor=%22Mel+Brooks%22&amp;search_field=advanced">Mel Brooks</a>'
expect(subject).to eq '<a href="/catalog?q=%22Fritz+Lang%22&amp;search_field=contributor">Fritz Lang</a>, ' \
+ '<a href="/catalog?q=%22Mel+Brooks%22&amp;search_field=contributor">Mel Brooks</a>'
end
end
end
Expand Down
48 changes: 33 additions & 15 deletions spec/search_builder/sufia/catalog_search_builder_spec.rb
@@ -1,22 +1,40 @@
describe Sufia::CatalogSearchBuilder do
let(:builder) { described_class.new([], self) }
let(:solr_params) { { q: user_query } }
let(:context) { double }
let(:builder) { described_class.new(context).with(blacklight_params) }
let(:solr_params) { Blacklight::Solr::Request.new }
let(:blacklight_params) { { q: user_query } }
let(:user_query) { "find me" }

context "with a user query" do
let(:user_query) { "find me" }
it "creates a valid solr join for works and files" do
builder.show_works_or_works_that_contain_files(solr_params)
expect(solr_params[:user_query]).to eq user_query
expect(solr_params[:q]).to eq "{!lucene}_query_:\"{!dismax v=$user_query}\" _query_:\"{!join from=id to=file_set_ids_ssim}{!dismax v=$user_query}\""
describe "#show_works_or_works_that_contain_files" do
subject { builder.show_works_or_works_that_contain_files(solr_params) }

context "with a user query" do
it "creates a valid solr join for works and files" do
subject
expect(solr_params[:user_query]).to eq user_query
expect(solr_params[:q]).to eq "{!lucene}_query_:\"{!dismax v=$user_query}\" _query_:\"{!join from=id to=file_set_ids_ssim}{!dismax v=$user_query}\""
end
end

context "without a user query" do
let(:blacklight_params) { {} }
it "does not modify the query" do
subject
expect(solr_params[:user_query]).to be_nil
expect(solr_params[:q]).to be_nil
end
end
end

context "with out a user query" do
let(:user_query) { nil }
it "does not modify the query" do
builder.show_works_or_works_that_contain_files(solr_params)
expect(solr_params[:user_query]).to be_nil
expect(solr_params[:q]).to be_nil
context "when doing a fielded search" do
let(:blacklight_params) { { q: user_query, search_field: 'depositor' } }
# Blacklight sets up these values when we've done a fielded search.
# Here we're ensuring they aren't wiped out
let(:solr_params) { Blacklight::Solr::Request.new("q" => "{!qf=depositor_ssim pf=depositor_ssim}\"#{user_query}\"") }
it "does not modify the query" do
subject
expect(solr_params[:user_query]).to be_nil
expect(solr_params[:q]).to eq '{!qf=depositor_ssim pf=depositor_ssim}"find me"'
end
end
end
end

0 comments on commit 5eab1bc

Please sign in to comment.