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) %>