Skip to content

Commit

Permalink
Fixed bug causing snippets to be overwritten. The issue was in
Browse files Browse the repository at this point in the history
`create_snippet_attribute`. The singleton method was being defined
incorrectly. Specifically it was being defined on the model class
rather than on a singleton class. As a result, it was being overwritten
every time thru `results` loop in `instantiate_results_from_results`.
A very similar issue is nicely described here:
stackoverflow.com/questions/3026943/define-method-for-instance-of-class
  • Loading branch information
coryschires committed Jun 25, 2011
1 parent 18977a8 commit fa7b920
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
7 changes: 3 additions & 4 deletions lib/tanker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,9 @@ def tanker_index_data

#dynamically create a snippet read attribute (method)
def create_snippet_attribute(key, value)
# the method name should something_snippet not snippet_something as the api returns it
self.class.send(:define_method, "#{key.match(/snippet_(\w+)/)[1]}_snippet") do
value
end
# method name should something_snippet not snippet_something as the api returns it
method_name = "#{key.match(/snippet_(\w+)/)[1]}_snippet"
(class << self; self end).send(:define_method, method_name) { value }
end

def tanker_index_options
Expand Down
2 changes: 0 additions & 2 deletions spec/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ class Product < ActiveRecord::Base
end

it 'should return a snippet for iphone' do
pending("Bug: Why is this being overwritten by the motorola snippet?")
snippets = @results.map(&:description_snippet)
snippets.should include("Puts even more <b>features</b> at your fingertips")
end
Expand Down Expand Up @@ -280,7 +279,6 @@ class Product < ActiveRecord::Base
end

it 'should set the "description_snippet" attribute for all results' do
pending("Bug: Fails for the same reason as previous test. Snippets are being overwritten.")
@indexed_motorola.description_snippet.should == "Not sure about <b>features</b> since I've never owned one"
@indexed_iphone.description_snippet.should == "Puts even more <b>features</b> at your fingertips"
end
Expand Down

0 comments on commit fa7b920

Please sign in to comment.