Skip to content
Browse files

Handling nil association values when translating facet values

  • Loading branch information...
1 parent 4570fb2 commit 5c1688ce7585752819b43084a258bee15722f2de @pat pat committed Jun 8, 2009
Showing with 25 additions and 1 deletion.
  1. +1 −1 lib/thinking_sphinx/facet.rb
  2. +24 −0 spec/unit/thinking_sphinx/facet_spec.rb
View
2 lib/thinking_sphinx/facet.rb
@@ -92,7 +92,7 @@ def to_s
def translate(object, attribute_value)
column.__stack.each { |method|
- object = object.send(method)
+ return nil unless object = object.send(method)
}
if object.is_a?(Array)
object.collect { |item| item.send(column.__name) }
View
24 spec/unit/thinking_sphinx/facet_spec.rb
@@ -275,4 +275,28 @@
ThinkingSphinx::Facet.new(attribute).type.should == :anything
end
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
+
+ it "should return association values" do
+ person = Person.find(:first)
+ friendship = Friendship.new(:person => person)
+
+ @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
end

0 comments on commit 5c1688c

Please sign in to comment.
Something went wrong with that request. Please try again.