Skip to content

Commit

Permalink
Moved method #search_by_id to Hyrax::SolrService class.
Browse files Browse the repository at this point in the history
  • Loading branch information
lsitu committed Sep 17, 2019
1 parent b0e6957 commit 6fabf64
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 41 deletions.
2 changes: 1 addition & 1 deletion app/models/concerns/hyrax/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def operation_abilities
# removes edit access for the depositor.
def trophy_abilities
can [:create, :destroy], Trophy do |t|
doc = Hyrax::Base.search_by_id(t.work_id, fl: 'depositor_ssim')
doc = Hyrax::SolrService.search_by_id(t.work_id, fl: 'depositor_ssim')
current_user.user_key == doc.fetch('depositor_ssim').first
end
end
Expand Down
11 changes: 0 additions & 11 deletions app/models/hyrax/base.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/presenters/hyrax/admin/workflow_role_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def label
attr_accessor :workflow, :role, :source_id

def admin_set_label(id)
result = Hyrax::Base.search_by_id(id, fl: 'title_tesim')
result = Hyrax::SolrService.search_by_id(id, fl: 'title_tesim')
result['title_tesim'].first
rescue ActiveFedora::ObjectNotFoundError
"[AdminSet ID=#{id}]"
Expand Down
12 changes: 11 additions & 1 deletion app/services/hyrax/solr_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def select_path
'Use `Hyrax.config.solr_select_path` instead'
end

delegate :add, :commit, :count, :delete, :get, :instance, :post, :query, :delete_by_query, to: :new
delegate :add, :commit, :count, :delete, :get, :instance, :post, :query, :delete_by_query, :search_by_id, to: :new
end

# Wraps rsolr get
Expand Down Expand Up @@ -127,6 +127,16 @@ def count(query, use_valkyrie: Hyrax.config.query_index_from_valkyrie)
get(query, use_valkyrie: use_valkyrie, **args)['response']['numFound'].to_i
end

# Wraps ActiveFedora::Base#search_by_id(id, opts)
# @return [Array<SolrHit>] the response docs wrapped in SolrHit objects
def search_by_id(id, opts = {})
opts = opts.merge(rows: 1)
result = Hyrax::SolrService.query("id:#{id}", opts)

raise Hyrax::ObjectNotFoundError, "Object '#{id}' not found in solr" if result.empty?
result.first
end

private

def valkyrie_index
Expand Down
27 changes: 0 additions & 27 deletions spec/models/hyrax/base_spec.rb

This file was deleted.

24 changes: 24 additions & 0 deletions spec/services/hyrax/solr_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,28 @@
end
end
end

describe "#search_by_id" do
context "with a document in solr" do
let(:doc) { instance_double(Hash) }

before do
expect(described_class).to receive(:query).with('id:a_fade_id', hash_including(rows: 1)).and_return([doc])
end

it "returns the document" do
expect(described_class.search_by_id('a_fade_id')).to eq doc
end
end

context "without a document in solr" do
before do
expect(described_class).to receive(:query).with('id:a_fade_id', hash_including(rows: 1)).and_return([])
end

it "returns the document" do
expect { described_class.search_by_id('a_fade_id') }.to raise_error Hyrax::ObjectNotFoundError, "Object 'a_fade_id' not found in solr"
end
end
end
end

0 comments on commit 6fabf64

Please sign in to comment.