Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing float facets - Sphinx returns an integer as the group, so need…

… to use the model to figure out the float value.
  • Loading branch information...
commit 15f65e0b2acf11ae5dc5f240333d636d7acc6f75 1 parent 6c16d98
@pat pat authored
View
7 features/facets.feature
@@ -27,6 +27,13 @@ Feature: Search and browse models by their defined facets
And I should have the facet State
And I should have the facet Age
+ Scenario: Requseting float facets
+ Given Sphinx is running
+ And I am searching on alphas
+ When I am requesting facet results
+ Then I should have 1 facet
+ And the Cost facet should have a 5.55 key
+
Scenario: Requesting facet results
Given Sphinx is running
And I am searching on developers
View
4 features/facets_across_model.feature
@@ -5,7 +5,7 @@ Feature: Search and browse across models by their defined facets
When I am requesting facet results
And I want all possible attributes
Then I should have valid facet results
- And I should have 9 facets
+ And I should have 10 facets
And I should have the facet Class
And the Class facet should have a "Person" key
And I should have the facet Gender
@@ -19,7 +19,7 @@ Feature: Search and browse across models by their defined facets
When I am requesting facet results
And I want all possible attributes
And I don't want classes included
- Then I should have 8 facets
+ Then I should have 9 facets
And I should not have the facet Class
Scenario: Requesting facets common to all indexed models
View
10 features/step_definitions/facet_steps.rb
@@ -77,6 +77,16 @@
results[facet_name(name)].keys.should include(key)
end
+Then /^the ([\w_\s]+) facet should have an? (\d+\.?\d*) key$/ do |name, key|
+ if key[/\./]
+ key = key.to_f
+ else
+ key = key.to_i
+ end
+
+ results[facet_name(name)].keys.should include(key)
+end
+
def facet_name(string)
string.gsub(/\s/, '').underscore.to_sym
end
View
3  features/support/models/alpha.rb
@@ -2,7 +2,8 @@ class Alpha < ActiveRecord::Base
define_index do
indexes :name, :sortable => true
- has value, cost, created_at, created_on
+ has value, created_at, created_on
+ has cost, :facet => true
set_property :field_weights => {"name" => 10}
end
View
6 lib/thinking_sphinx/facet.rb
@@ -72,7 +72,7 @@ def type
end
def value(object, attribute_value)
- return translate(object, attribute_value) if translate?
+ return translate(object, attribute_value) if translate? || float?
case @property.type
when :datetime
@@ -106,5 +106,9 @@ def translate(object, attribute_value)
def column
@property.columns.first
end
+
+ def float?
+ @property.type == :float
+ end
end
end
View
49 spec/lib/thinking_sphinx/facet_spec.rb
@@ -277,26 +277,45 @@
end
describe "#value" do
- before :each do
- @index = ThinkingSphinx::Index.new(Friendship)
- @source = ThinkingSphinx::Source.new(@index)
- @field = ThinkingSphinx::Field.new(
- @source, ThinkingSphinx::Index::FauxColumn.new(:person, :first_name)
- )
- @facet = ThinkingSphinx::Facet.new(@field)
- end
+ describe 'for fields from associations' do
+ before :each do
+ @index = ThinkingSphinx::Index.new(Friendship)
+ @source = ThinkingSphinx::Source.new(@index)
+ @field = ThinkingSphinx::Field.new(
+ @source, ThinkingSphinx::Index::FauxColumn.new(:person, :first_name)
+ )
+ @facet = ThinkingSphinx::Facet.new(@field)
+ end
- it "should return association values" do
- person = Person.find(:first)
- friendship = Friendship.new(:person => person)
+ it "should return association values" do
+ person = Person.find(:first)
+ friendship = Friendship.new(:person => person)
- @facet.value(friendship, 1).should == person.first_name
+ @facet.value(friendship, 1).should == person.first_name
+ end
+
+ it "should return nil if the association is nil" do
+ friendship = Friendship.new(:person => nil)
+
+ @facet.value(friendship, 1).should be_nil
+ end
end
- it "should return nil if the association is nil" do
- friendship = Friendship.new(:person => nil)
+ describe 'for float attributes' do
+ before :each do
+ @index = ThinkingSphinx::Index.new(Alpha)
+ @source = ThinkingSphinx::Source.new(@index)
+ @attribute = ThinkingSphinx::Attribute.new(
+ @source, ThinkingSphinx::Index::FauxColumn.new(:cost)
+ )
+ @facet = ThinkingSphinx::Facet.new(@attribute)
+ end
+
+ it "should translate using the given model" do
+ alpha = Alpha.new(:cost => 10.5)
- @facet.value(friendship, 1).should be_nil
+ @facet.value(alpha, 1093140480).should == 10.5
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.