Skip to content

Commit

Permalink
Merge pull request kidpollo#39 from zsxking/master
Browse files Browse the repository at this point in the history
Make tanker work with scope of ActiveRecords
  • Loading branch information
kidpollo committed Aug 17, 2011
2 parents 5e05ff9 + 02b623a commit 8be83b5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ or

# just include the Tanker module
include Tanker

# define a scope
scope :awesome, :conditions => {:title => 'Awesome'}

# define the index by supplying the index name and the fields to index
# this is the index name you create in the Index Tank dashboard
Expand Down Expand Up @@ -121,6 +124,10 @@ Search Topics
@topics = Topic.search_tank('blah', :conditions => {:tag_list => 'tag1'}) # Gets 2 results!
@topics = Topic.search_tank('blah', :conditions => {:title => 'Awesome', :tag_list => 'tag1'}) # Gets 1 result!

Search with scope (Only tested on ActiveRecord)

@topics = Topic.awesome.search_tank('blah') # Gets 1 result!

Search with wildcards!

@topics = Topic.search_tank('Awe*', :page => 1, :per_page => 10) # Gets 1 result!
Expand Down
10 changes: 8 additions & 2 deletions lib/tanker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,19 @@ def instantiate_results_from_db(index_result)

id_map.each do |klass, ids|
# replace the id list with an eager-loaded list of records for this model
id_map[klass] = constantize(klass).find(ids)
klass_const = constantize(klass)
if klass_const.respond_to?('find_all_by_id')
id_map[klass] = klass_const.find_all_by_id(ids)
else
id_map[klass] = klass_const.find(ids)
end
end
# return them in order
results.map do |result|
results = results.map do |result|
model, id = result["__type"], result["__id"]
id_map[model].detect {|record| id == record.id.to_s }
end
results.compact
end

def instantiate_results_from_results(index_result, fetch = false, snippets = false)
Expand Down
10 changes: 10 additions & 0 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
class Product < ActiveRecord::Base
include Tanker

scope :amazon, :conditions => {:href => "amazon"}

tankit 'tanker_integration_tests' do
indexes :name
indexes :href, :category => true
Expand Down Expand Up @@ -260,5 +262,13 @@ class Product < ActiveRecord::Base
@results.count.should == 3
end
end

describe 'on scope' do
it 'should return amazon product only', :focus => true do
results = Product.amazon.search_tank('decent')
results.should include(@samsung, @motorola)
results.should have_exactly(2).products
end
end
end

19 changes: 10 additions & 9 deletions spec/tanker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def self.included(base)
}
)

Person.should_receive(:find).and_return(
Person.should_receive(:find_all_by_id).and_return(
[Person.new]
)

Expand Down Expand Up @@ -260,7 +260,7 @@ def self.included(base)
}
)

Person.should_receive(:find).with(['mystring1d', '1']).and_return(
Person.should_receive(:find_all_by_id).with(['mystring1d', '1']).and_return(
[Person.new, Person.new]
)

Expand Down Expand Up @@ -323,10 +323,10 @@ def self.included(base)
}
)

Dog.should_receive(:find).and_return(
Dog.should_receive(:find_all_by_id).and_return(
[Dog.new(:name => 'fido', :id => 7)]
)
Cat.should_receive(:find).and_return(
Cat.should_receive(:find_all_by_id).and_return(
[Cat.new(:name => 'fluffy', :id => 9)]
)

Expand All @@ -350,7 +350,7 @@ def self.included(base)
}]
})

Foo::Bar.should_receive(:find).and_return([stub(:id => 42)])
Foo::Bar.should_receive(:find_all_by_id).and_return([stub(:id => 42)])

Foo::Bar.search_tank('bar')
end
Expand All @@ -374,7 +374,7 @@ def self.included(base)
}
)

Person.should_receive(:find).with(['1', '2']).and_return(
Person.should_receive(:find_all_by_id).with(['1', '2']).and_return(
[Person.new, Person.new]
)

Expand Down Expand Up @@ -402,7 +402,7 @@ def self.included(base)
}
)

Person.should_receive(:find).with(['1', '2']).and_return(
Person.should_receive(:find_all_by_id).with(['1', '2']).and_return(
[Person.new, Person.new]
)

Expand Down Expand Up @@ -540,7 +540,7 @@ def self.included(base)
"search_time" => 1
}
)
Person.should_receive(:find).and_return([Person.new])
Person.should_receive(:find_all_by_id).and_return([Person.new])

lambda { Person.search_tank('test') }.should raise_error(Tanker::BadConfiguration)
end
Expand All @@ -560,7 +560,7 @@ def self.included(base)
}
)

Person.should_receive(:find).and_return([Person.new])
Person.should_receive(:find_all_by_id).and_return([Person.new])

array = Person.search_tank('hey!')
array.class.should == Tanker::Pagination::Kaminari
Expand All @@ -570,4 +570,5 @@ def self.included(base)
array.current_page.should == 1
end
end

end

0 comments on commit 8be83b5

Please sign in to comment.