diff --git a/lib/active_fedora/querying.rb b/lib/active_fedora/querying.rb index 01803f1df..4f29cb517 100644 --- a/lib/active_fedora/querying.rb +++ b/lib/active_fedora/querying.rb @@ -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 diff --git a/lib/active_fedora/relation/finder_methods.rb b/lib/active_fedora/relation/finder_methods.rb index 172bad13f..cb4757582 100644 --- a/lib/active_fedora/relation/finder_methods.rb +++ b/lib/active_fedora/relation/finder_methods.rb @@ -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') diff --git a/lib/active_fedora/solr_instance_loader.rb b/lib/active_fedora/solr_instance_loader.rb index 1745f0697..ae32cb19e 100644 --- a/lib/active_fedora/solr_instance_loader.rb +++ b/lib/active_fedora/solr_instance_loader.rb @@ -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 @@ -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 diff --git a/spec/unit/finder_methods_spec.rb b/spec/unit/finder_methods_spec.rb index 338872041..1c2e3a20a 100644 --- a/spec/unit/finder_methods_spec.rb +++ b/spec/unit/finder_methods_spec.rb @@ -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