Make system unable to check out already checked out items
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
index 1516498..4445c07 100644 (file)
@@ -50,6 +50,20 @@ class PurchaseController < ApplicationController
       return
     end
 
+    if @rentable.checkedout?
+      flash[:error] = "This #{@rentable.type} is already checked out!"
+      redirect_to :action => :rent_begin
+      return
+    end
+
+    # Check out the item
+    checkout = Coitem.new
+    checkout.customer = @customer
+    checkout.rentable = @rentable
+    checkout.out_date = Time.now.to_date
+    checkout.due_date = @rentable.due_date
+    checkout.save!
+
     # Actually record the purchase
     purchase = RentablePurchase.new
     purchase.customer_id = session[:customer_id]