Skip to content

Commit

Permalink
Tie in discovery_permisssions from initalize()
Browse files Browse the repository at this point in the history
  • Loading branch information
atz committed Sep 29, 2016
1 parent a9cc08b commit ff5c0a4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ def collections_search_builder_class
end

def collections_search_builder(access_level = nil)
collections_search_builder_class.new(self, nil).tap do |builder|
builder.discovery_permissions = access_levels[access_level] if access_level
end
collections_search_builder_class.new(self, access_levels[access_level])
end
end
17 changes: 10 additions & 7 deletions app/search_builders/curation_concerns/collection_search_builder.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
module CurationConcerns
# Our parent class is the generated SearchBuilder descending from Blacklight::SearchBuilder
# Our parent class is the generated SearchBuilder descending from Blacklight::SearchBuilder.
# Defines search_params_logic used when searching for Collections.
# @see https://github.com/projecthydra/curation_concerns/blob/master/app/search_builders/curation_concerns/README.md SearchBuilders README
#
class CollectionSearchBuilder < ::SearchBuilder
include FilterByType
# Defines which search_params_logic should be used when searching for Collections
def initialize(context, access)
@access = access_levels[access]
# @param [#blacklight_config, #current_ability] scope the object that has access to #blacklight_config
# From a controller, scope would be `self`. This argument passed to ::SearchBuilder.new()
# @param [Symbol] permissions defining breadth of search, e.g. :edit, :read
# @note permissions will otherwise be defaulted by inherited #discovery_permissions
def initialize(scope, access = nil)
@rows = 100
super(context)
@discovery_permissions ||= access_levels[access] if access
super(scope)
end

# @return [String] class URI of the model, as indexed in Solr has_model_ssim field
Expand All @@ -18,7 +21,7 @@ def model_class_uri

# @return [String] Solr field name indicating default sort order
def sort_field
Solrizer.solr_name('title', :sortable)
'title_si'
end

# @return [Hash{Symbol => Array[Symbol]}] bottom-up map of "what you need" to "what qualifies"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,75 @@
require 'spec_helper'

describe CurationConcerns::CollectionSearchBuilder do
let(:context) { double('context') }
let(:context) { double('context', blacklight_config: blacklight_config) }
let(:solr_params) { { fq: [] } }
let(:blacklight_config) { CatalogController.blacklight_config.deep_copy }

subject { described_class.new(context, :read) }
describe '#filter_models' do
before { subject.filter_models(solr_params) }
RSpec.shared_examples 'common SearchBuilder' do
it { should be_a SearchBuilder }

it 'adds Collection to :fq' do
expect(solr_params[:fq].first).to include('{!field f=has_model_ssim}Collection')
describe '#filter_models' do
it 'adds Collection to :fq' do
subject.filter_models(solr_params)
expect(solr_params[:fq].first).to include('{!field f=has_model_ssim}Collection')
end
end

describe '#sort' do
it 'returns default sort' do
expect(subject.sort).to eq 'score desc, date_uploaded_dtsi desc'
end
end

describe '#processor_chain' do
it 'includes methods we override' do
expect(subject.processor_chain).to include(:add_sorting_to_solr, :filter_models)
end
end

describe '#add_sorting_to_solr' do
it 'without query, applies default sort' do
subject.add_sorting_to_solr(solr_params)
expect(solr_params[:sort]).to eq 'title_si asc'
end

it 'with query, does not apply default sort' do
subject.add_sorting_to_solr(solr_params.merge(q: 'Abraham Lincoln'))
expect(solr_params[:sort]).not_to eq 'title_si asc'
end
end
end

describe 'undefined access' do
subject { described_class.new(context) }
it_behaves_like 'common SearchBuilder'

describe '#discovery_permissions' do
it 'returns the inherited default permissions' do
expect(subject.discovery_permissions).to contain_exactly('read', 'discover', 'edit')
end
end
end

describe 'read access' do
subject { described_class.new(context, ['read']) }
it_behaves_like 'common SearchBuilder'

describe '#discovery_permissions' do
it 'returns the set permissions' do
expect(subject.discovery_permissions).to eq ['read']
end
end
end

describe 'edit access' do
subject { described_class.new(context, ['edit']) }
it_behaves_like 'common SearchBuilder'

describe '#discovery_permissions' do
it 'returns the set permissions' do
expect(subject.discovery_permissions).to eq ['edit']
end
end
end
end

0 comments on commit ff5c0a4

Please sign in to comment.