Add payment after order create for users

This commit is contained in:
Benjamin Cousaert 2014-12-09 10:57:38 +01:00
parent 12b3a68972
commit ee5ebd7ba4
3 changed files with 13 additions and 0 deletions

View file

@ -22,6 +22,7 @@ class OrdersController < ApplicationController
@products = Product.all
@order_products = @order.order_products
if @order.save
@user.pay(@order.price)
redirect_to root_path
else
render 'new'

View file

@ -30,4 +30,12 @@ class Order < ActiveRecord::Base
accepts_nested_attributes_for :order_products, reject_if: proc { |op| op[:count].to_i <= 0 }
def price
price = 0
products.each do |p|
price += p.price * p.count(self)
end
price
end
end

View file

@ -23,5 +23,9 @@ class User < ActiveRecord::Base
"#{name} #{last_name}"
end
def pay(amount)
self.increment!(:balance, - amount)
end
has_secure_password
end