feat: Add promotion code functionality to ticket orders

This commit is contained in:
kbe
2025-09-28 20:20:22 +02:00
parent e5ed1a34dd
commit a69ddb4012
10 changed files with 258 additions and 22 deletions

View File

@@ -0,0 +1,23 @@
class PromotionCode < ApplicationRecord
# Validations
validates :code, presence: true, uniqueness: true
validates :discount_amount_cents, numericality: { greater_than_or_equal_to: 0 }
# Scopes
scope :active, -> { where(active: true) }
scope :expired, -> { where("expires_at < ? OR active = ?", Time.current, false) }
scope :valid, -> { active.where("expires_at > ? OR expires_at IS NULL", Time.current) }
# Callbacks
before_create :increment_uses_count
# Associations
has_many :order_promotion_codes
has_many :orders, through: :order_promotion_codes
private
def increment_uses_count
self.uses_count ||= 0
end
end