Skip to content
Browse files

edge case where user has no nearby stores

  • Loading branch information...
1 parent d14de05 commit 8d6f9449f502defd4eb2da0a1fe176afb48a8d33 @mike-burns mike-burns committed Oct 19, 2008
View
6 app/helpers/application_helper.rb
@@ -4,7 +4,11 @@ def body_class
end
def number_to_formatted_currency(number)
- number_to_currency(number, :unit => "<span class='dollar-sign'>$</span>")
+ if number.nil?
+ '?'
+ else
+ number_to_currency(number, :unit => "<span class='dollar-sign'>$</span>")
+ end
end
def user_name
View
5 app/models/purchase.rb
@@ -40,9 +40,8 @@ def item_name_changed?
end
def cheapest_price
- item.
- cheapest_purchase_in_stores(user.nearby_stores).
- price
+ purchase = item.cheapest_purchase_in_stores(user.nearby_stores)
+ purchase.nil? ? nil : purchase.price
end
def self.per_page
View
4 test/unit/helpers/application_helper_test.rb
@@ -6,6 +6,10 @@ class ApplicationHelperTest < ActionView::TestCase
assert_match /<span class='dollar-sign'>\$<\/span>3.25/, number_to_formatted_currency(3.25)
end
+ should "produce '?' when sent #number_to_formatted_currency with nil" do
+ assert_equal '?', number_to_formatted_currency(nil)
+ end
+
context "when sent #autocomplete_field" do
context "from XHR" do
setup do
View
22 test/unit/purchase_test.rb
@@ -195,4 +195,26 @@ class PurchaseTest < Test::Unit::TestCase
assert_equal price, purchase.cheapest_price
end
+
+ context "with no nearby Stores" do
+ setup do
+ @purchase = Factory(:purchase)
+ stores = mock('stores')
+ price = 100.00
+
+ @purchase.item.
+ expects(:cheapest_purchase_in_stores).
+ with(stores).
+ returns(nil)
+
+ @purchase.user.
+ expects(:nearby_stores).
+ with().
+ returns(stores)
+ end
+
+ should "produce nil when sent #cheapest_price" do
+ assert_nil @purchase.cheapest_price
+ end
+ end
end

0 comments on commit 8d6f944

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