Skip to content

Commit

Permalink
Refactor Spotlight::Search to remove Blacklight::SearchHelper mixin; …
Browse files Browse the repository at this point in the history
…instead, build the SearchBuilder instance locally
  • Loading branch information
cbeer committed Dec 10, 2015
1 parent c588f53 commit 448bbbe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
24 changes: 14 additions & 10 deletions app/models/spotlight/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ class Search < ActiveRecord::Base
accepts_nested_attributes_for :thumbnail, update_only: true
accepts_nested_attributes_for :masthead, update_only: true

include Blacklight::SearchHelper
include Spotlight::Catalog::AccessControlsEnforcement

def thumbnail_image_url
thumbnail.image.thumb.url if thumbnail && thumbnail.image
end

def count
repository.search(search_builder.with(query_params).rows(0).merge(facet: false))['response']['numFound']
repository.search(search_params.rows(0))['response']['numFound']
end

def images
Expand Down Expand Up @@ -67,15 +64,22 @@ def set_default_thumbnail
end
end

def search_params
search_builder.with(query_params.with_indifferent_access).merge(facet: false)
end

private

def search_builder_class
blacklight_config.search_builder_class
end

def search_builder
search_builder_class.new(true, self)
end

def solr_response
@solr_response ||= repository.search(
search_builder.with(query_params).rows(1000).merge(
facet: false,
fl: default_search_fields
)
)
@solr_response ||= repository.search(search_params.rows(1000).merge(fl: default_search_fields))
end

def default_search_fields
Expand Down
22 changes: 19 additions & 3 deletions spec/models/spotlight/search_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'spec_helper'

describe Spotlight::Search, type: :model do
let(:exhibit) { FactoryGirl.create(:exhibit) }

let(:query_params) { { 'f' => { 'genre_sim' => ['map'] } } }
subject { FactoryGirl.create(:exhibit).searches.build(title: 'Search', query_params: query_params) }
subject { exhibit.searches.build(title: 'Search', query_params: query_params) }

let(:blacklight_config) { ::CatalogController.blacklight_config }
let(:document) do
Expand All @@ -15,8 +17,6 @@
blacklight_config.index.title_field => 'title')
end

it { is_expected.to be_a Spotlight::Catalog::AccessControlsEnforcement }

context 'thumbnail' do
it 'calls DefaultThumbnailJob to fetch a default feature image' do
expect(Spotlight::DefaultThumbnailJob).to receive(:perform_later).with(subject)
Expand Down Expand Up @@ -79,4 +79,20 @@
end
end
end

describe '#search_params' do
it 'maps the search to the appropriate facet values' do
expect(subject.search_params.to_hash).to include 'fq' => array_including('{!raw f=genre_sim}map')
end

context 'with filter_resources_by_exhibit configured' do
before do
allow(Spotlight::Engine.config).to receive(:filter_resources_by_exhibit).and_return(true)
end

it 'includes the exhibit context' do
expect(subject.search_params.to_hash).to include 'fq' => array_including("spotlight_exhibit_slug_#{exhibit.slug}_bsi:true")
end
end
end
end

0 comments on commit 448bbbe

Please sign in to comment.