From ee5ebd7ba452dfffd91c79cb1fcf7389dab68acf Mon Sep 17 00:00:00 2001 From: Benjamin Cousaert Date: Tue, 9 Dec 2014 10:57:38 +0100 Subject: [PATCH] Add payment after order create for users --- app/controllers/orders_controller.rb | 1 + app/models/order.rb | 8 ++++++++ app/models/user.rb | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index fc6bc2f..e774a1d 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -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' diff --git a/app/models/order.rb b/app/models/order.rb index b9fabf5..3ab14ca 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 2008e88..008a430 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,5 +23,9 @@ class User < ActiveRecord::Base "#{name} #{last_name}" end + def pay(amount) + self.increment!(:balance, - amount) + end + has_secure_password end