Open cyri113 opened 7 years ago
class Cart
has_many :discount_items, dependent: :destroy
has_many :discounts, through: :discount_items
def total_discount
total_discount = discounts.sum(&total_discount)
total_discount = total_price if discount > total_price
total_discount
end
end
class Discount
has_many :discount_items
has_many :carts, through: :discount_items
before_destroy :ensure_not_referenced_by_any_discount_item
private
# ensure that there are not discount items referencing this product
def ensure_not_referenced_by_any_discount_item
unless discount_items.empty?
errors.add(:base, 'Discount Items present')
throw :abort
end
end
end
class DiscountItem
belongs_to :discount
belongs_to :cart
def total_discount
total_discount = discount.amount
total_discount = discount.amount * cart.total_price if discount.discount_type == :percentage
total_discount
end
end
Future:
Use Cases:
Rules:
Models:
Discount:
DiscountItem:
Frequency