Skip to content

Commit

Permalink
Direct access to weight.
Browse files Browse the repository at this point in the history
  • Loading branch information
pat committed Jan 14, 2012
1 parent 25f3f3e commit aff62e4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ A list of what still needs to be implemented, in no particular order:
* Abstract Inheritance support
* Rails 3.0 support
* Searching from association contexts
* Weight access
* Searching by specific indices
* Matching many values on a single MVA attribute (:with_all)
* Matching none of many values on a single MVA attribute (:without_any)
Expand Down
4 changes: 4 additions & 0 deletions lib/thinking_sphinx/search/glaze.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def unglazed
@object
end

def weight
@object.respond_to?(:weight) ? @object.weight : @raw[:weight]
end

private

def method_missing(method, *args, &block)
Expand Down
7 changes: 7 additions & 0 deletions spec/acceptance/attribute_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@

Book.search('gods').first.sphinx_attributes[:year].should == 2001
end

it "provides direct access to the search weight/relevance scores" do
Book.create! :title => 'American Gods', :year => 2001
index

Book.search('gods').first.weight.should == 3500
end
end
18 changes: 18 additions & 0 deletions spec/thinking_sphinx/search/glaze_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,22 @@ def sphinx_attributes
should == :special_attributes
end
end

describe '#weight' do
it "returns the object's weight by default" do
raw['weight'] = 101

glaze.weight.should == 101
end

it "respects an existing sphinx_attributes method" do
klass = Class.new do
def weight
202
end
end

ThinkingSphinx::Search::Glaze.new(klass.new).weight.should == 202
end
end
end

0 comments on commit aff62e4

Please sign in to comment.