Skip to content
This repository has been archived by the owner on Jun 30, 2018. It is now read-only.

Update __find_records_by_ids to implement Strategy per ORM. #810

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion lib/tire/results/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,55 @@ def __get_results_with_load(hits)
end

def __find_records_by_ids(klass, ids)
@options[:load] === true ? klass.find(ids) : klass.find(ids, @options[:load])
Strategy.from_class(klass, @options).find(ids)
end
end

# Importing strategies for common persistence frameworks (ActiveModel, Mongoid), as well as
# pagination libraries (WillPaginate, Kaminari), or a custom strategy.
module Strategy
def self.from_class(klass, options={})
return const_get(options[:strategy]).new(klass, options) if options[:strategy]

case
when defined?(::ActiveRecord) && klass.ancestors.include?(::ActiveRecord::Base)
ActiveRecord.new klass, options
else
Default.new klass, options
end
end

module Base
attr_reader :klass, :options

def initialize(klass, options={})
@klass = klass
@options = options
end
end

class ActiveRecord
include Base

def find(ids)
if options[:load] === true
klass.where(:id => ids)
else
klass.where(:id => ids).all(options[:load])
end
end
end

class Default
include Base

def find(ids)
if options[:load] === true
klass.find(ids)
else
klass.find(ids, options[:load])
end
end
end
end

Expand Down
103 changes: 67 additions & 36 deletions test/unit/results_collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,26 +258,31 @@ class ResultsCollectionTest < Test::Unit::TestCase
end
end

should "yield the model instance and the raw hit" do
response = { 'hits' => { 'hits' => [ {'_id' => 1, '_score' => 0.5, '_type' => 'active_record_article'} ] } }

ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" )
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define(:version => 1) { create_table(:active_record_articles) { |t| t.string(:title) } }
model = ActiveRecordArticle.new(:title => 'Test'); model.id = 1

ActiveRecordArticle.expects(:find).with([1]).returns([ model] )

Results::Collection.new(response, :load => true).each_with_hit do |result, hit|
assert_instance_of ActiveRecordArticle, result
assert_instance_of Hash, hit
assert_equal 'Test', result.title
assert_equal 0.5, hit['_score']
{
Tire::Results::Strategy::Default => lambda { |model| ActiveRecordArticle.expects(:find).with([1]).returns([model]) },
Tire::Results::Strategy::ActiveRecord => lambda { |model| ActiveRecordArticle.expects(:where).with(:id => [1]).returns([model]) }
}.each_pair do |strategy, expects|
should "yield the model instance and the raw hit for #{strategy}" do
Tire::Results::Strategy.stubs(:from_class).returns(strategy.new(ActiveRecordArticle, :load => true))

response = { 'hits' => { 'hits' => [ {'_id' => 1, '_score' => 0.5, '_type' => 'active_record_article'} ] } }

ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" )
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define(:version => 1) { create_table(:active_record_articles) { |t| t.string(:title) } }
model = ActiveRecordArticle.new(:title => 'Test'); model.id = 1

expects.call(model)

Results::Collection.new(response, :load => true).each_with_hit do |result, hit|
assert_instance_of ActiveRecordArticle, result
assert_instance_of Hash, hit
assert_equal 'Test', result.title
assert_equal 0.5, hit['_score']
end
end

end


end

context "while paginating results" do
Expand Down Expand Up @@ -344,28 +349,54 @@ class ResultsCollectionTest < Test::Unit::TestCase
ActiveRecordArticle.stubs(:inspect).returns("<ActiveRecordArticle>")
end

should "load the records via model find method from database" do
ActiveRecordArticle.expects(:find).with([1,2,3]).
returns([ Results::Item.new(:id => 3),
Results::Item.new(:id => 1),
Results::Item.new(:id => 2) ])
Results::Collection.new(@response, :load => true).results
end
{
Tire::Results::Strategy::Default => lambda { |ids, items| ActiveRecordArticle.expects(:find).with(ids).returns(items) },
Tire::Results::Strategy::ActiveRecord => lambda { |ids, items| ActiveRecordArticle.expects(:where).with(:id => ids).returns(items) }
}.each_pair do |strategy, expects|
should "load the records via model find method from database for #{strategy}" do
Tire::Results::Strategy.stubs(:from_class).returns(strategy.new(ActiveRecordArticle, :load => true))

expects.call([1, 2, 3], [
Results::Item.new(:id => 3),
Results::Item.new(:id => 1),
Results::Item.new(:id => 2)
])

Results::Collection.new(@response, :load => true).results
end

should "pass the :load option Hash to model find metod" do
ActiveRecordArticle.expects(:find).with([1,2,3], :include => 'comments').
returns([ Results::Item.new(:id => 3),
Results::Item.new(:id => 1),
Results::Item.new(:id => 2) ])
Results::Collection.new(@response, :load => { :include => 'comments' }).results
should "preserve the order of records returned from search for #{strategy}" do
Tire::Results::Strategy.stubs(:from_class).returns(strategy.new(ActiveRecordArticle, :load => true))

expects.call([1, 2, 3], [
Results::Item.new(:id => 3),
Results::Item.new(:id => 1),
Results::Item.new(:id => 2)
])

assert_equal [1,2,3], Results::Collection.new(@response, :load => true).results.map(&:id)
end
end

should "preserve the order of records returned from search" do
ActiveRecordArticle.expects(:find).with([1,2,3]).
returns([ Results::Item.new(:id => 3),
Results::Item.new(:id => 1),
Results::Item.new(:id => 2) ])
assert_equal [1,2,3], Results::Collection.new(@response, :load => true).results.map(&:id)
{
Tire::Results::Strategy::Default => lambda { |ids, load_options, items| ActiveRecordArticle.expects(:find).with(ids, load_options).returns(items) },
Tire::Results::Strategy::ActiveRecord => lambda { |ids, load_options, items|
relation = Struct.new(:all)
relation.expects(:all).with(load_options).returns(items)
ActiveRecordArticle.expects(:where).with(:id => ids).returns(relation)
}
}.each_pair do |strategy, expects|
should "pass the :load option Hash to model find method for #{strategy}" do
Tire::Results::Strategy.stubs(:from_class).returns(strategy.new(ActiveRecordArticle, :load => { :include => "comments" }))

expects.call([1, 2, 3], { :include => "comments" }, [
Results::Item.new(:id => 3),
Results::Item.new(:id => 1),
Results::Item.new(:id => 2)
])

Results::Collection.new(@response, :load => { :include => 'comments' }).results
end
end

should "raise error when model class cannot be inferred from _type" do
Expand Down