Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing attribute updating to translate non-integer columns correctly.

  • Loading branch information...
commit ee2a1710f0d884bbf25679c0c7ca16924c80d4e1 1 parent 569f525
@pat pat authored
View
11 features/attribute_updates.feature
@@ -37,3 +37,14 @@ Feature: Update attributes directly to Sphinx
When I search for the document id of beta eight in the beta_delta index
Then it should not exist
+
+ Scenario: Updating boolean attribute in Sphinx
+ Given Sphinx is running
+ And I am searching on alphas
+ When I filter by active alphas
+ Then I should get 10 results
+
+ When I flag alpha five as inactive
+ And I wait for Sphinx to catch up
+ And I filter by active alphas
+ Then I should get 9 results
View
9 features/step_definitions/alpha_steps.rb
@@ -5,3 +5,12 @@
When /^I change the (\w+) of alpha (\w+) to (\w+)$/ do |column, name, replacement|
Alpha.find_by_name(name).update_attributes(column.to_sym => replacement)
end
+
+When /^I filter by active alphas$/ do
+ @results = nil
+ @with[:active] = true
+end
+
+When /^I flag alpha (\w+) as inactive$/ do |name|
+ Alpha.find_by_name(name).update_attributes(:active => false)
+end
View
1  features/support/db/migrations/create_alphas.rb
@@ -1,6 +1,7 @@
ActiveRecord::Base.connection.create_table :alphas, :force => true do |t|
t.column :name, :string, :null => false
t.column :value, :integer, :null => false
+ t.column :active, :boolean, :null => false, :default => true
t.column :cost, :decimal, :precision => 10, :scale => 6
t.column :created_on, :date
t.column :created_at, :timestamp
View
1  features/support/models/alpha.rb
@@ -4,6 +4,7 @@ class Alpha < ActiveRecord::Base
has value, created_at, created_on
has cost, :facet => true
+ has active
set_property :field_weights => {"name" => 10}
end
View
7 lib/thinking_sphinx/active_record/attribute_updates.rb
@@ -37,12 +37,7 @@ def updatable_attributes(index)
def attribute_values_for_index(index)
updatable_attributes(index).inject({}) { |hash, attrib|
- if attrib.type == :datetime && attrib.live_value(self)
- hash[attrib.unique_name.to_s] = attrib.live_value(self).to_time.to_i
- else
- hash[attrib.unique_name.to_s] = attrib.live_value self
- end
-
+ hash[attrib.unique_name.to_s] = attrib.live_value self
hash
}
end
View
17 lib/thinking_sphinx/attribute.rb
@@ -180,7 +180,7 @@ def live_value(instance)
object = instance
column = @columns.first
column.__stack.each { |method| object = object.send(method) }
- object.send(column.__name)
+ sphinx_value object.send(column.__name)
end
def all_ints?
@@ -337,5 +337,20 @@ def all_of_type?(*column_types)
}
}
end
+
+ def sphinx_value(value)
+ case value
+ when TrueClass
+ 1
+ when FalseClass, NilClass
+ 0
+ when Time
+ value.to_i
+ when Date
+ value.to_time.to_i
+ else
+ value
+ end
+ end
end
end
View
39 spec/thinking_sphinx/attribute_spec.rb
@@ -504,4 +504,43 @@
@statement.should match(/SELECT cricket_team_id, id FROM tags/)
end
end
+
+ describe '#live_value' do
+ before :each do
+ @attribute = ThinkingSphinx::Attribute.new @source, [
+ stub('column', :__stack => [], :__name => "col_name")
+ ]
+ @instance = stub('model')
+ end
+
+ it "should translate boolean values to integers" do
+ @instance.stub!(:col_name => true)
+ @attribute.live_value(@instance).should == 1
+
+ @instance.stub!(:col_name => false)
+ @attribute.live_value(@instance).should == 0
+ end
+
+ it "should translate timestamps to integers" do
+ now = Time.now
+ @instance.stub!(:col_name => now)
+ @attribute.live_value(@instance).should == now.to_i
+ end
+
+ it "should translate dates to timestamp integers" do
+ today = Date.today
+ @instance.stub!(:col_name => today)
+ @attribute.live_value(@instance).should == today.to_time.to_i
+ end
+
+ it "should translate nils to 0" do
+ @instance.stub!(:col_name => nil)
+ @attribute.live_value(@instance).should == 0
+ end
+
+ it "should return integers as integers" do
+ @instance.stub!(:col_name => 42)
+ @attribute.live_value(@instance).should == 42
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.