From f6a37a80cf2c503d6dc8ca1fd531d27b448f21e6 Mon Sep 17 00:00:00 2001 From: Benjamin Cousaert Date: Wed, 10 Dec 2014 10:00:00 +0100 Subject: [PATCH] Fix balance and order paying --- app/models/order.rb | 2 +- app/models/user.rb | 12 +++++++++++- app/views/users/_user.html.erb | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/models/order.rb b/app/models/order.rb index 3ab14ca..9307b9e 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -33,7 +33,7 @@ class Order < ActiveRecord::Base def price price = 0 products.each do |p| - price += p.price * p.count(self) + price += p.read_attribute(:price) * p.count(self) end price end diff --git a/app/models/user.rb b/app/models/user.rb index e6fd81d..4468325 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -39,6 +39,16 @@ class User < ActiveRecord::Base end def pay(amount) - self.increment!(:balance, - amount) + write_attribute(:balance, read_attribute(:balance) - amount) + self.save + end + + def balance + (read_attribute(:balance) || 0) / 100.0 + end + + def balance=(value) + if value.is_a? String then value.sub!(',', '.') end + write_attribute(:balance, (value.to_f * 100).to_int) end end diff --git a/app/views/users/_user.html.erb b/app/views/users/_user.html.erb index e940fb3..6f8436e 100644 --- a/app/views/users/_user.html.erb +++ b/app/views/users/_user.html.erb @@ -6,7 +6,7 @@

Name: <%= user.name %>

Last name: <%= user.last_name %>

Nickname: <%= user.nickname %>

-

Balance: <%= user.balance %>

+

Balance: <%= euro(user.balance) %>