feat: Add promotion code functionality to ticket orders
This commit is contained in:
23
app/models/promotion_code.rb
Normal file
23
app/models/promotion_code.rb
Normal 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
|
||||
Reference in New Issue
Block a user