From aba212e13062c43a1192ef885b05145ac1cc9fe7 Mon Sep 17 00:00:00 2001 From: David Gay Date: Sun, 23 May 2021 17:06:34 -0400 Subject: Bazaar with buy orders, sell orders, and order cancellation --- app/models/concerns/destroy_if_zero_quantity.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 app/models/concerns/destroy_if_zero_quantity.rb (limited to 'app/models/concerns') diff --git a/app/models/concerns/destroy_if_zero_quantity.rb b/app/models/concerns/destroy_if_zero_quantity.rb new file mode 100644 index 0000000..6fdfb3e --- /dev/null +++ b/app/models/concerns/destroy_if_zero_quantity.rb @@ -0,0 +1,18 @@ +module DestroyIfZeroQuantity + extend ActiveSupport::Concern + + included do + after_save :destroy_if_zero_quantity + end + + private + def destroy_if_zero_quantity + byebug + if self.quantity == 0 + destroy + elsif self.quantity < 0 + # TODO: Can improve this (at the least, with reporting, later). + raise ItemQuantityError + end + end +end -- cgit v1.2.3