Permalink
Browse files

Return right rounded price

  • Loading branch information...
1 parent 94bb679 commit 5048c14df5b255173b11a0efe89cca58d5a15691 @jeroeningen committed May 2, 2011
View
@@ -18,7 +18,7 @@ def acts_as_price column_name, options = {}
#setters
define_method("#{column_name}=") do |val|
- super((val.to_s.gsub(",", ".").to_f * 100).to_s)
+ super(((val.to_s.gsub(",", ".").to_f * 100) + 0.5).to_s)
end
alias_method("price=", "#{column_name}=")
@@ -29,7 +29,7 @@ def acts_as_price column_name, options = {}
#getters
define_method(column_name) do
- if super == 0.0
+ if super.blank? or super == 0.0
""
else
if comma
@@ -28,8 +28,12 @@ def test_acts_as_price_methods column_name, seperator, options = {}
#test for special cases
#test if 1.5 is returned as 1.50
test_setter_in_doubles "#{currency}. 1.5", seperator
+ test_setter_in_doubles "#{currency}. 1.505", seperator
+ test_setter_in_doubles "#{currency}. 1.504", seperator
#test if 1,5 is returned as 1.50
test_setter_in_doubles "#{currency}. 1,5", seperator
+ test_setter_in_doubles "#{currency}. 1,505", seperator
+ test_setter_in_doubles "#{currency}. 1,504", seperator
#test if float returns 2.05 correctly
#float can return 2.05 as 2.04 in some cases
@@ -65,7 +69,7 @@ def test_setter_in_cents cents, seperator
columns_in_cents.each do |setter|
@acts_as_price_model.send("#{setter}=", cents)
columns_in_cents.each do |getter|
- @acts_as_price_model.send(getter).should == cents.to_i
+ @acts_as_price_model.send(getter).should == (cents.to_f + 0.5).to_i
end
columns_in_doubles.each do |getter|
if cents == ""
@@ -89,16 +93,16 @@ def test_setter_in_doubles double, seperator
columns_in_doubles.each do |setter|
@acts_as_price_model.send("#{setter}=", double)
columns_in_cents.each do |getter|
- @acts_as_price_model.send(getter).should == (double.gsub(",", ".").to_f * 100).to_s.to_i
+ @acts_as_price_model.send(getter).should == ((double.gsub(",", ".").to_f * 100) + 0.5).to_s.to_i
end
columns_in_doubles.each do |getter|
if double == ""
@acts_as_price_model.send(getter).should == ""
else
if currency
- @acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
+ @acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", double.gsub(",", ".").to_s).gsub(".", seperator)
else
- @acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
+ @acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".").to_s).gsub(".", seperator)
end
end
end
@@ -3,14 +3,15 @@
describe Car do
before(:each) do
@column_name = :price
- @acts_as_price_model = stub_model(Car, :brand => "Ford", :cartype => "Focus", :price => "23995,99")
+ @acts_as_price_model = stub_model(Car, :brand => "Ford", :cartype => "Stationwagon", :model => "Focus", :price => "23995,99")
end
context "given an empty car" do
it "should return an invalid car" do
car = Car.new
should validate_presence_of :brand
should validate_presence_of :cartype
+ should validate_presence_of :model
car.valid?.should be_false
car.save.should be_false
end
@@ -52,6 +53,8 @@
it "should convert it to the right price in cents" do
test_setter_in_doubles "EUR. 25500,5", ","
test_setter_in_doubles "EUR. 21599,05", ","
+ test_setter_in_doubles "EUR. 21599,055", ","
+ test_setter_in_doubles "EUR. 21599,054", ","
end
end
@@ -53,13 +53,15 @@
it "should convert it to the right price in cents" do
test_setter_in_doubles "1.5", @seperator
test_setter_in_doubles "2.05", @seperator
+ test_setter_in_doubles "2.055", @seperator
+ test_setter_in_doubles "2.054", @seperator
end
end
- context "given the price is zero" do
- it "should return an empty price per liter" do
- test_setter_in_cents "", @seperator
- test_setter_in_doubles "", @seperator
- end
- end
+# context "given the price is zero" do
+# it "should return an empty price per liter" do
+# test_setter_in_cents "", @seperator
+# test_setter_in_doubles "", @seperator
+# end
+# end
end
@@ -11,6 +11,12 @@
car = stub_model(Car, :price => "23000,59")
car.price_in_cents.should == 2300059
end
+ it "should return the right rounded price" do
+ car = stub_model(Car, :price => "23000,595")
+ car.price.should == "EUR. 23000,60"
+ car = stub_model(Car, :price => "23000,594")
+ car.price.should == "EUR. 23000,59"
+ end
end
context "adding the price as a dot seperated value" do
@@ -35,6 +41,13 @@
car.currency.should == "EUR"
end
end
+
+ context "empty car" do
+ it "should return no price" do
+ car = Car.new
+ car.price.should == ""
+ end
+ end
end
#Fueltype returns the price as a dot seperated value
@@ -45,6 +58,12 @@
fueltype.price.should == "1.59"
fueltype.price_per_liter.should == "1.59"
end
+ it "should return the right rounded price" do
+ fueltype = stub_model(Fueltype, :price => "1,595")
+ fueltype.price.should == "1.60"
+ fueltype = stub_model(Fueltype, :price => "1,594")
+ fueltype.price.should == "1.59"
+ end
it "should return the price in cents" do
fueltype = stub_model(Fueltype, :price_per_liter => "1,59")
fueltype.price_per_liter_in_cents.should == 159
@@ -80,4 +99,17 @@
fueltype.price_per_liter_in_cents.should == 205
end
end
+ context "using no currency" do
+ it "should return no currency" do
+ fueltype = stub_model(Fueltype)
+ fueltype.currency.should be_nil
+ end
+ end
+
+ context "empty fueltype" do
+ it "should return no price" do
+ fueltype = Fueltype.new
+ fueltype.price.should == ""
+ end
+ end
end

0 comments on commit 5048c14

Please sign in to comment.