Converted prices to integers/cents to avoid floating point trouble

This commit is contained in:
Skud
2013-05-15 23:09:35 +10:00
parent afb4e21adc
commit abcba35a71
12 changed files with 40 additions and 39 deletions

View File

@@ -0,0 +1,10 @@
module ApplicationHelper
# 999 cents becomes 9.99 AUD -- for products/orders/etc
def format_price(price)
return sprintf('%.2f %s', price / 100.0,
Growstuff::Application.config.currency)
end
end

View File

@@ -1,4 +1,5 @@
class Product < ActiveRecord::Base
attr_accessible :description, :min_price, :name
has_and_belongs_to_many :orders
end

View File

@@ -26,7 +26,6 @@
x
= o.product.name
@
= number_with_precision(o.price, :precision => 2)
= Growstuff::Application.config.currency
= format_price(o.price)
%br/
%td= link_to 'Details', order, :class => 'btn btn-mini'

View File

@@ -32,14 +32,12 @@
%tr
%td= i.product.name
%td
= number_with_precision(i.price, :precision => 2)
= Growstuff::Application.config.currency
= format_price(i.price)
%td= i.quantity
%td
- subtotal = i.price * i.quantity
- total += subtotal
= number_with_precision(subtotal, :precision => 2)
= Growstuff::Application.config.currency
= format_price(subtotal)
%tr
%td
@@ -48,8 +46,7 @@
%strong Total:
%td
%strong
= number_with_precision(total, :precision => 2)
= Growstuff::Application.config.currency
= format_price(total)
%p
= link_to "View other orders/order history", orders_path

View File

@@ -22,8 +22,7 @@
%div
Base price:
= number_with_precision(p.min_price, :precision => 2)
= Growstuff::Application.config.currency
=format_price(p.min_price)
%div
- if can? :create, Order

View File

@@ -0,0 +1,11 @@
class ChangePricesToIntegers < ActiveRecord::Migration
def up
change_column :order_items, :price, :integer
change_column :products, :min_price, :integer
end
def down
change_column :order_items, :price, :decimal
change_column :products, :min_price, :decimal
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130515054017) do
ActiveRecord::Schema.define(:version => 20130515122301) do
create_table "authentications", :force => true do |t|
t.integer "member_id", :null => false
@@ -40,7 +40,6 @@ ActiveRecord::Schema.define(:version => 20130515054017) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "slug"
t.integer "parent_id"
end
add_index "crops", ["slug"], :name => "index_crops_on_slug", :unique => true
@@ -124,7 +123,7 @@ ActiveRecord::Schema.define(:version => 20130515054017) do
create_table "order_items", :force => true do |t|
t.integer "order_id"
t.integer "product_id"
t.decimal "price"
t.integer "price"
t.integer "quantity"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
@@ -142,27 +141,6 @@ ActiveRecord::Schema.define(:version => 20130515054017) do
t.integer "product_id"
end
create_table "payments", :force => true do |t|
t.integer "payer_id"
t.string "payment_type"
t.decimal "amount"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "photos", :force => true do |t|
t.integer "owner_id", :null => false
t.integer "flickr_photo_id", :null => false
t.string "thumbnail_url", :null => false
t.string "fullsize_url", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "title", :null => false
t.string "license_name", :null => false
t.string "license_url"
t.string "link_url", :null => false
end
create_table "plantings", :force => true do |t|
t.integer "garden_id", :null => false
t.integer "crop_id", :null => false
@@ -193,7 +171,7 @@ ActiveRecord::Schema.define(:version => 20130515054017) do
create_table "products", :force => true do |t|
t.string "name", :null => false
t.string "description", :null => false
t.decimal "min_price", :null => false
t.integer "min_price", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

View File

@@ -4,7 +4,7 @@ FactoryGirl.define do
factory :order_item do
order
product
price "9.99"
price "999"
quantity 1
end
end

View File

@@ -4,6 +4,6 @@ FactoryGirl.define do
factory :product do
name "annual subscription"
description "paid membership, renewing yearly"
min_price "9.99"
min_price "999"
end
end

View File

@@ -0,0 +1,7 @@
require 'spec_helper'
describe ApplicationHelper do
it "formats prices" do
format_price(999).should eq '9.99 AUD'
end
end

View File

@@ -1,5 +1,4 @@
require 'spec_helper'
describe Product do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -9,7 +9,7 @@ describe "orders/show" do
@order_item = FactoryGirl.create(:order_item,
:order => @order,
:quantity => 2,
:price => 99.00
:price => 9900
)
render
end