Skip to content

Commit

Permalink
Allow specify of eager loading in Searchable::search()
Browse files Browse the repository at this point in the history
[sunspot#90 state:resolved]
  • Loading branch information
Mat Brown committed Mar 11, 2010
1 parent e43fab2 commit 402fe53
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 13 additions & 3 deletions sunspot_rails/lib/sunspot/rails/searchable.rb
Expand Up @@ -96,20 +96,30 @@ module ClassMethods
#
# ==== Example
#
# Post.search do
# Post.search(:include => [:blog]) do
# keywords 'best pizza'
# with :blog_id, 1
# order :updated_at, :desc
# facet :category_ids
# end
#
# ==== Options
#
# :include:: Specify associations to eager load
#
# ==== Returns
#
# Sunspot::Search:: Object containing results, totals, facets, etc.
#
def search(&block)
Sunspot.search(self, &block)
def search(options = {}, &block)
options.assert_valid_keys(:include)
search = Sunspot.new_search(self, &block)
if options[:include]
search.build do |query|
query.data_accessor_for(self).include = options[:include]
end
end
search.execute
end

#
Expand Down
11 changes: 11 additions & 0 deletions sunspot_rails/spec/model_spec.rb
Expand Up @@ -129,6 +129,17 @@
data_accessor_for(Post).include = [:blog]
end.results.should == [@post]
end

it 'should pass :include option from search call to data accessor' do
Post.should_receive(:find).with(anything(), hash_including(:include => [:blog])).and_return([@post])
Post.search(:include => [:blog]) do
with :title, 'Test Post'
end.results.should == [@post]
end

it 'should not allow bogus options to search' do
lambda { Post.search(:bogus => :option) }.should raise_error(ArgumentError)
end

it 'should use the select option on the data accessor when specified' do
Post.should_receive(:find).with(anything(), hash_including(:select => 'title, published_at')).and_return([@post])
Expand Down

0 comments on commit 402fe53

Please sign in to comment.