diff options
author | HombreLaser <sebastian-440@live.com> | 2023-04-14 20:49:46 -0600 |
---|---|---|
committer | HombreLaser <sebastian-440@live.com> | 2023-04-14 20:49:46 -0600 |
commit | 8fbeea986d06a0052ce0132717f22b6ff8bd1a04 (patch) | |
tree | 75cd090f680853574ce36865089cb2775ed0fe75 /app/models | |
parent | 4b0527efc4b7703c99043013fd455f496d32329c (diff) |
Añade validador de existencias a ProductCart
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/product_cart.rb | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/app/models/product_cart.rb b/app/models/product_cart.rb index 09e7a25..d4232cb 100644 --- a/app/models/product_cart.rb +++ b/app/models/product_cart.rb @@ -8,10 +8,17 @@ class ProductCart < ApplicationRecord validates :quantity, presence: true, comparison: { greater_than: 0 } validate :sole_product_in_cart + validate :product_quantity def sole_product_in_cart return if cart.nil? || cart.products.find_by(id: product_id).nil? errors.add(:product_id, "cart already has product with id #{product_id}") end + + def product_quantity + return if product.nil? || product.available_quantity > quantity + + errors.add(:product_id, 'not enough in existence') + end end |