Skip to content

Commit

Permalink
Merge pull request #1009 from projecthydra/find-search
Browse files Browse the repository at this point in the history
Rename find_with_conditions and find_in_batches to search_with_conditions and search_in_batches
  • Loading branch information
jcoyne committed Mar 15, 2016
2 parents c160a14 + 430f7a8 commit f00d935
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 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, :find_each, to: :all
:destroy_all, :count, :last, :find_with_conditions, :find_in_batches, :search_with_conditions, :search_in_batches, :find_each, to: :all

def self.extended(base)
base.class_attribute :solr_query_handler
Expand Down
25 changes: 18 additions & 7 deletions lib/active_fedora/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def exists?(conditions)
return false unless conditions
case conditions
when Hash
find_with_conditions(conditions, rows: 1).present?
search_with_conditions(conditions, rows: 1).present?
when String
find(conditions).present?
else
Expand All @@ -102,23 +102,29 @@ def exists?(conditions)
# hash representing the query part of an solr statement. If a hash is
# provided, this method will generate conditions based simple equality
# combined using the boolean AND operator.
# @param[Hash] options
# @param [Hash] options
# @option opts [Array] :sort a list of fields to sort by
# @option opts [Array] :rows number of rows to return
def find_with_conditions(conditions, opts = {})
def search_with_conditions(conditions, opts = {})
# set default sort to created date ascending
opts[:sort] = @klass.default_sort_params unless opts.include?(:sort)
SolrService.query(create_query(conditions), opts)
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')
search_with_conditions(*args)
end

# Yields the found ActiveFedora::Base object to the passed block
#
# @param [Hash] conditions the conditions for the solr search to match
# @param [Hash] opts
# @option opts [Boolean] :cast (true) when true, examine the model and cast it to the first known cModel
def find_each(conditions = {}, opts = {})
cast = opts.delete(:cast)
find_in_batches(conditions, opts.merge(fl: ActiveFedora.id_field)) do |group|
search_in_batches(conditions, opts.merge(fl: ActiveFedora.id_field)) do |group|
group.each do |hit|
begin
yield(load_from_fedora(hit[ActiveFedora.id_field], cast))
Expand All @@ -140,11 +146,10 @@ def find_each(conditions = {}, opts = {})
# @option opts [Array] :rows number of rows to return
#
# @example
# Person.find_in_batches('age_t'=>'21', {:batch_size=>50}) do |group|
# Person.search_in_batches('age_t'=>'21', {:batch_size=>50}) do |group|
# group.each { |person| puts person['name_t'] }
# end

def find_in_batches(conditions, opts = {})
def search_in_batches(conditions, opts = {})
opts[:q] = create_query(conditions)
opts[:qt] = @klass.solr_query_handler
# set default sort to created date ascending
Expand All @@ -163,6 +168,12 @@ def find_in_batches(conditions, opts = {})
end
end

# @deprecated
def find_in_batches(*args)
Deprecation.warn(ActiveFedora::Base, '.find_in_batches is deprecated and will be removed in active-fedora 10.0; use .search_in_batches instead')
search_in_batches(*args)
end

# Retrieve the Fedora object with the given id, explore the returned object
# Raises a ObjectNotFoundError if the object is not found.
# @param [String] id of the object to load
Expand Down
2 changes: 1 addition & 1 deletion lib/active_fedora/solr_instance_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def allocate_object

def solr_doc
@solr_doc ||= begin
result = context.find_with_conditions(id: id)
result = context.search_with_conditions(id: id)
if result.empty?
raise ActiveFedora::ObjectNotFoundError, "Object #{id} not found in solr"
end
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/full_featured_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class OralHistory < ActiveFedora::Base

@test_history.save

@solr_result = OralHistory.find_with_conditions(id: @test_history.id)[0]
@solr_result = OralHistory.search_with_conditions(id: @test_history.id)[0]
@properties_sample_values.each_pair do |field, value|
next if field == :hard_copy_availability # FIXME: HYDRA-824
next if field == :location # FIXME HYDRA-825
Expand All @@ -153,8 +153,8 @@ class OralHistory < ActiveFedora::Base
expect(dc_xml.root.elements["dcterms:subject"].text).to eq "My Subject Heading"
end

it "supports #find_with_conditions" do
solr_result = OralHistory.find_with_conditions({})
it "supports #search_with_conditions" do
solr_result = OralHistory.search_with_conditions({})
expect(solr_result).to_not be_nil
end
end
6 changes: 3 additions & 3 deletions spec/unit/finder_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def initialize
end
end

describe "#find_in_batches" do
describe "#search_in_batches" do
let(:docs) { double('docs', has_next?: false) }
let(:select_handler) { 'select' }
let(:connection) { double('conn') }
Expand All @@ -70,14 +70,14 @@ def initialize
end
it "yields the docs" do
expect { |b|
finder.find_in_batches({ 'age_t' => '21' }, { other_opt: 'test' }, &b)
finder.search_in_batches({ 'age_t' => '21' }, { other_opt: 'test' }, &b)
}.to yield_with_args(docs)
end

context "with custom select handler" do
let(:select_handler) { 'select_test' }
it "uses the custom select handler" do
finder.find_in_batches({ 'age_t' => '21' }, other_opt: 'test') do end
finder.search_in_batches({ 'age_t' => '21' }, other_opt: 'test') do end
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Basic < ActiveFedora::Base
end
end

describe '#find_in_batches' do
describe '#search_in_batches' do
describe "with conditions hash" do
let(:solr) { ActiveFedora::SolrService.instance.conn }
let(:expected_query) { "#{model_query} AND " \
Expand All @@ -168,7 +168,7 @@ class Basic < ActiveFedora::Base
expect(solr).to receive(:paginate).with(1, 1002, 'select', expected_params).and_return('response' => { 'docs' => mock_docs })
yielded = double("yielded method")
expect(yielded).to receive(:run).with(mock_docs)
SpecModel::Basic.find_in_batches({ foo: 'bar', baz: ['quix', 'quack'] }, batch_size: 1002, fl: 'id') { |group| yielded.run group }
SpecModel::Basic.search_in_batches({ foo: 'bar', baz: ['quix', 'quack'] }, batch_size: 1002, fl: 'id') { |group| yielded.run group }
end
end
end
Expand Down Expand Up @@ -240,10 +240,10 @@ class Basic < ActiveFedora::Base
end
end

describe '#find_with_conditions' do
describe '#search_with_conditions' do
let(:mock_result) { double('Result') }
let(:klass) { SpecModel::Basic }
subject { klass.find_with_conditions(conditions) }
subject { klass.search_with_conditions(conditions) }

before do
expect(ActiveFedora::SolrService).to receive(:query)
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/solr_config_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class Basic < ActiveFedora::Base
expect(@test_object.to_solr[:id]).to be_nil
end

it "is used by ActiveFedora::Base#find_with_conditions" do
it "is used by ActiveFedora::Base#search_with_conditions" do
mock_response = double("SolrResponse")
expect(ActiveFedora::SolrService).to receive(:query)
.with("_query_:\"{!raw f=has_model_ssim}SolrSpecModel::Basic\" AND " \
"_query_:\"{!raw f=#{field}}changeme:30\"",
sort: ["#{ActiveFedora::SolrQueryBuilder.solr_name('system_create', :stored_sortable, type: :date)} asc"])
.and_return(mock_response)

expect(SolrSpecModel::Basic.find_with_conditions(id: "changeme:30")).to equal(mock_response)
expect(SolrSpecModel::Basic.search_with_conditions(id: "changeme:30")).to equal(mock_response)
end
end

Expand Down

0 comments on commit f00d935

Please sign in to comment.