From abcba35a71eb46bb669beb877b36a8af3d09bf9e Mon Sep 17 00:00:00 2001 From: Skud Date: Wed, 15 May 2013 23:09:35 +1000 Subject: [PATCH] Converted prices to integers/cents to avoid floating point trouble --- app/helpers/application_helper.rb | 10 +++++++ app/models/product.rb | 1 + app/views/orders/index.html.haml | 3 +- app/views/orders/show.html.haml | 9 ++---- app/views/shop/index.html.haml | 3 +- ...0130515122301_change_prices_to_integers.rb | 11 ++++++++ db/schema.rb | 28 ++----------------- spec/factories/order_items.rb | 2 +- spec/factories/products.rb | 2 +- spec/helpers/application_helper.rb | 7 +++++ spec/models/product_spec.rb | 1 - spec/views/orders/show.html.haml_spec.rb | 2 +- 12 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 app/helpers/application_helper.rb create mode 100644 db/migrate/20130515122301_change_prices_to_integers.rb create mode 100644 spec/helpers/application_helper.rb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb new file mode 100644 index 000000000..b9d601cfc --- /dev/null +++ b/app/helpers/application_helper.rb @@ -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 + diff --git a/app/models/product.rb b/app/models/product.rb index cc20fe696..5ad15a2e8 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -1,4 +1,5 @@ class Product < ActiveRecord::Base attr_accessible :description, :min_price, :name has_and_belongs_to_many :orders + end diff --git a/app/views/orders/index.html.haml b/app/views/orders/index.html.haml index 6626b0f2f..f3b5ca922 100644 --- a/app/views/orders/index.html.haml +++ b/app/views/orders/index.html.haml @@ -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' diff --git a/app/views/orders/show.html.haml b/app/views/orders/show.html.haml index 262fb1149..e18fddc18 100644 --- a/app/views/orders/show.html.haml +++ b/app/views/orders/show.html.haml @@ -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 diff --git a/app/views/shop/index.html.haml b/app/views/shop/index.html.haml index 566644663..89eea100e 100644 --- a/app/views/shop/index.html.haml +++ b/app/views/shop/index.html.haml @@ -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 diff --git a/db/migrate/20130515122301_change_prices_to_integers.rb b/db/migrate/20130515122301_change_prices_to_integers.rb new file mode 100644 index 000000000..f07d45dea --- /dev/null +++ b/db/migrate/20130515122301_change_prices_to_integers.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 392658f7e..d4bf0005f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/factories/order_items.rb b/spec/factories/order_items.rb index d99c324a5..94e001450 100644 --- a/spec/factories/order_items.rb +++ b/spec/factories/order_items.rb @@ -4,7 +4,7 @@ FactoryGirl.define do factory :order_item do order product - price "9.99" + price "999" quantity 1 end end diff --git a/spec/factories/products.rb b/spec/factories/products.rb index 405924518..84ca3163f 100644 --- a/spec/factories/products.rb +++ b/spec/factories/products.rb @@ -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 diff --git a/spec/helpers/application_helper.rb b/spec/helpers/application_helper.rb new file mode 100644 index 000000000..c1a9fcfb2 --- /dev/null +++ b/spec/helpers/application_helper.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe ApplicationHelper do + it "formats prices" do + format_price(999).should eq '9.99 AUD' + end +end diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index 267720887..88c9c9938 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' describe Product do - pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/views/orders/show.html.haml_spec.rb b/spec/views/orders/show.html.haml_spec.rb index 89971acd2..75915bffb 100644 --- a/spec/views/orders/show.html.haml_spec.rb +++ b/spec/views/orders/show.html.haml_spec.rb @@ -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