Skip to content

Commit

Permalink
Merge pull request sunspot#18 from crowdint/master
Browse files Browse the repository at this point in the history
Define Hit#to_param to play nicely with Rails URL helpers.
  • Loading branch information
nz committed Sep 5, 2011
2 parents f3295f8 + 5b3c1a5 commit c75e85b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions sunspot/lib/sunspot/search/hit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ def inspect #:nodoc:
"#<Sunspot::Search::Hit:#{@class_name} #{@primary_key}>"
end

#
# Returns the instance primary key when the Hit is used to generate urls
# For example, using a search that stores the :name attribute:
#
# hits = Sunspot.search(Object) do ...
#
# hits.each do |hit|
# link_to hit.stored(:name), edit_object_path(hit)
# end
#
def to_param
self.primary_key
end

private

def setup
Expand Down
6 changes: 6 additions & 0 deletions sunspot/spec/api/search/hits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
session.search(Post).hits.first.instance.should == posts.first
end

it 'should return the instance primary key when you use it as a param' do
posts = Array.new(2) { Post.new }
stub_results(*posts)
session.search(Post).hits.first.to_param.should == posts.first.id.to_s
end

it 'should provide iterator over hits with instances' do
posts = Array.new(2) { Post.new }
stub_results(*posts)
Expand Down

0 comments on commit c75e85b

Please sign in to comment.