Skip to content

Commit

Permalink
Change signature of search builder
Browse files Browse the repository at this point in the history
Now it takes the configuration and the current_ability
The current_ability is necessary for the blacklight-access_controls
plugin.

This provides a more defined api, rather than just passing a `scope`
object that responds to `blacklight_config` and perhaps `current_ability`.
  • Loading branch information
jcoyne committed May 11, 2017
1 parent f77bc16 commit cccebaf
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 47 deletions.
6 changes: 5 additions & 1 deletion app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ def facet_limit_for(facet_field)
#

def search_service
search_service_class.new(blacklight_config, search_state.to_h)
# If cancancan is installed, pass in the current ability.
# Although we aren't using this in blacklight itself, plugins such as
# blacklight-access_controls make use of this.
ability = current_ability if respond_to?(:current_ability)
search_service_class.new(blacklight_config, ability, search_state.to_h)
end

##
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/concerns/blacklight/request_builders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module RequestBuilders
# Override this method to use a search builder other than the one in the config
delegate :search_builder_class, to: :blacklight_config

# @return [SearchBuilder]
def search_builder
search_builder_class.new(self)
search_builder_class.new(blacklight_config, current_ability)
end

##
Expand Down
8 changes: 3 additions & 5 deletions app/services/blacklight/search_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ module Blacklight
class SearchService
include Blacklight::RequestBuilders

def initialize(blacklight_config, user_params = {})
def initialize(blacklight_config, current_ability, user_params = {})
@blacklight_config = blacklight_config
@current_ability = current_ability
@user_params = user_params
end

# TODO: Can this be private?
attr_reader :blacklight_config

# a solr query method
# @param [Hash] user_params ({}) the user provided parameters (e.g. query, facets, sort, etc)
# @yield [search_builder] optional block yields configured SearchBuilder, caller can modify or create new SearchBuilder to be used. Block should return SearchBuilder to be used.
Expand Down Expand Up @@ -91,7 +89,7 @@ def grouped_key_for_results

private

attr_reader :user_params
attr_reader :blacklight_config, :current_ability, :user_params

##
# Retrieve a set of documents by id
Expand Down
31 changes: 10 additions & 21 deletions lib/blacklight/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,13 @@ class SearchBuilder

attr_reader :processor_chain, :blacklight_params

# @overload initialize(scope)
# @param [Object] scope scope the scope where the filter methods reside in.
# @overload initialize(processor_chain, scope)
# @param [List<Symbol>,TrueClass] processor_chain options a list of filter methods to run or true, to use the default methods
# @param [Object] scope the scope where the filter methods reside in.
def initialize(*options)
case options.size
when 1
@processor_chain = default_processor_chain.dup
@scope = options.first
when 2
@processor_chain, @scope = options
else
raise ArgumentError, "wrong number of arguments. (#{options.size} for 1..2)"
end

# @param [Configuration] blacklight_config the configuration
# @param [Ability] current_ability the current abilities of the user
# @param [List<Symbol>] processor_chain a list of filter methods to run.
def initialize(blacklight_config, current_ability, processor_chain = default_processor_chain.dup)
@processor_chain = processor_chain
@blacklight_config = blacklight_config
@current_ability = current_ability
@blacklight_params = {}
@merged_params = {}
@reverse_merged_params = {}
Expand All @@ -51,7 +42,7 @@ def where(conditions)
# Append additional processor chain directives
def append(*addl_processor_chain)
params_will_change!
builder = self.class.new(processor_chain + addl_processor_chain, scope)
builder = self.class.new(blacklight_config, current_ability, processor_chain + addl_processor_chain)
.with(blacklight_params)
.merge(@merged_params)
.reverse_merge(@reverse_merged_params)
Expand All @@ -71,7 +62,7 @@ def append(*addl_processor_chain)
# Methods in argument that aren't currently in processor
# chain are ignored as no-ops, rather than raising.
def except(*except_processor_chain)
builder = self.class.new(processor_chain - except_processor_chain, scope)
builder = self.class.new(blacklight_config, current_ability, processor_chain - except_processor_chain)
.with(blacklight_params)
.merge(@merged_params)
.reverse_merge(@reverse_merged_params)
Expand Down Expand Up @@ -142,8 +133,6 @@ def processed_parameters
end
end

delegate :blacklight_config, to: :scope

def start=(value)
params_will_change!
@start = value.to_i
Expand Down Expand Up @@ -245,7 +234,7 @@ def should_add_field_to_request? _field_name, field
field.include_in_request || (field.include_in_request.nil? && blacklight_config.add_field_configuration_to_solr_request)
end

attr_reader :scope
attr_reader :blacklight_config, :current_ability

def params_will_change!
@dirty = true
Expand Down
16 changes: 6 additions & 10 deletions spec/models/blacklight/search_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
RSpec.describe Blacklight::SearchBuilder do
let(:processor_chain) { [] }
let(:blacklight_config) { Blacklight::Configuration.new }
let(:scope) { double blacklight_config: blacklight_config }
subject(:builder) { described_class.new processor_chain, scope }
let(:ability) { double }
subject(:builder) { described_class.new blacklight_config, ability, processor_chain }

context "with default processor chain" do
subject { described_class.new scope }
let(:builder) { described_class.new blacklight_config, ability }
subject { builder.processor_chain }

it "uses the class-level default_processor_chain" do
expect(subject.processor_chain).to eq []
expect(subject).to eq []
end
end

Expand Down Expand Up @@ -112,12 +114,6 @@
end
end

describe "#blacklight_config" do
it "gets the blacklight_config from the scope" do
expect(subject.blacklight_config).to eq scope.blacklight_config
end
end

describe "#page" do
it "is the current user parameter page number" do
expect(subject.with(page: 2).send(:page)).to eq 2
Expand Down
10 changes: 3 additions & 7 deletions spec/models/blacklight/solr/search_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@

let(:blacklight_config) { CatalogController.blacklight_config.deep_copy }
let(:user_params) { Hash.new }
let(:context) { CatalogController.new }

before { allow(context).to receive(:blacklight_config).and_return(blacklight_config) }

let(:current_ability) { double }
let(:search_builder_class) do
Class.new(Blacklight::SearchBuilder) do
include Blacklight::Solr::SearchBuilderBehavior
end
end
let(:search_builder) { search_builder_class.new(context) }
let(:search_builder) { search_builder_class.new(blacklight_config, current_ability) }

subject { search_builder.with(user_params) }

Expand Down Expand Up @@ -78,7 +75,7 @@
end

context "when search_params_logic is customized" do
let(:search_builder) { search_builder_class.new(method_chain, context) }
let(:search_builder) { search_builder_class.new(blacklight_config, current_ability, method_chain) }
let(:method_chain) { [:add_foo_to_solr_params] }

it "allows customization of search_params_logic" do
Expand Down Expand Up @@ -634,7 +631,6 @@
end
end


it 'defaults limit to 20' do
expect(solr_parameters[:"f.#{facet_field}.facet.limit"]).to eq 21
end
Expand Down
4 changes: 2 additions & 2 deletions spec/services/blacklight/search_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# blacklight code get a single document returned?)
#
RSpec.describe Blacklight::SearchService do

let(:service) { described_class.new(blacklight_config, user_params) }
let(:ability) { nil }
let(:service) { described_class.new(blacklight_config, ability, user_params) }
let(:repository) { Blacklight::Solr::Repository.new(blacklight_config) }
let(:user_params) { {} }
subject { service }
Expand Down

0 comments on commit cccebaf

Please sign in to comment.