Skip to content

Commit

Permalink
Merge pull request #1010 from projecthydra/search-by-id
Browse files Browse the repository at this point in the history
Push .search_by_id into ActiveFedora::FinderMethod
  • Loading branch information
jcoyne committed Mar 16, 2016
2 parents f00d935 + 74597d7 commit d39dc66
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/active_fedora/querying.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ActiveFedora
module Querying
delegate :find, :first, :exists?, :where, :limit, :offset, :order, :delete_all,
:destroy_all, :count, :last, :find_with_conditions, :find_in_batches, :search_with_conditions, :search_in_batches, :find_each, to: :all
:destroy_all, :count, :last, :find_with_conditions, :find_in_batches, :search_with_conditions, :search_in_batches, :search_by_id, :find_each, to: :all

def self.extended(base)
base.class_attribute :solr_query_handler
Expand Down
14 changes: 14 additions & 0 deletions lib/active_fedora/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ def search_with_conditions(conditions, opts = {})
SolrService.query(create_query(conditions), opts)
end

# Returns a single solr hit matching the given id
# @param [String] id document id
# @param [Hash] opts
def search_by_id(id, opts = {})
opts[:rows] = 1
result = search_with_conditions({ id: id }, opts)

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

result.first
end

# @deprecated
def find_with_conditions(*args)
Deprecation.warn(ActiveFedora::Base, '.find_with_conditions is deprecated and will be removed in active-fedora 10.0; use .search_with_conditions instead')
Expand Down
17 changes: 7 additions & 10 deletions lib/active_fedora/solr_instance_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class SolrInstanceLoader
def initialize(context, id, solr_doc = nil)
@context = context
@id = id
@solr_doc = solr_doc
validate_solr_doc_and_id!(@solr_doc)
self.solr_doc = solr_doc
end

def object
Expand All @@ -37,18 +36,16 @@ def allocate_object

def solr_doc
@solr_doc ||= begin
result = context.search_with_conditions(id: id)
if result.empty?
raise ActiveFedora::ObjectNotFoundError, "Object #{id} not found in solr"
end
@solr_doc = result.first
validate_solr_doc_and_id!(@solr_doc)
@solr_doc
self.solr_doc = context.search_by_id(id)
end
end

def solr_doc=(solr_doc)
validate_solr_doc_and_id!(@solr_doc) unless @solr_doc.nil?
@solr_doc = solr_doc
end

def validate_solr_doc_and_id!(document)
return true if document.nil?
solr_id = document[ActiveFedora.id_field]
return if id == solr_id
raise ActiveFedora::FedoraSolrMismatchError, id, solr_id
Expand Down
24 changes: 24 additions & 0 deletions spec/unit/finder_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,28 @@ def initialize
end
end
end

describe '#search_by_id' do
context 'with a document in solr' do
let(:doc) { double('Document') }

before do
expect(finder).to receive(:search_with_conditions).with({ id: 'x' }, hash_including(rows: 1)).and_return([doc])
end

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

context 'without a document in solr' do
before do
expect(finder).to receive(:search_with_conditions).with({ id: 'x' }, hash_including(rows: 1)).and_return([])
end

it "returns the document" do
expect { finder.search_by_id('x') }.to raise_error ActiveFedora::ObjectNotFoundError
end
end
end
end

0 comments on commit d39dc66

Please sign in to comment.