Class: Coupon

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/coupon.rb

Instance Method Summary collapse

Instance Method Details

#create_discount(order) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/coupon.rb', line 15

def create_discount(order)
  if eligible?(order) and amount = calculator.compute(order)
    amount = order.item_total if amount > order.item_total
    order.coupon_credits.reload.clear unless combine? and order.coupon_credits.all? { |credit| credit.adjustment_source.combine? }
    order.save
    credits.create({
        :order => order, 
        :amount => amount,
        :description => "#{I18n.t(:coupon)} (#{code})"
      })
  end
end

#eligible?(order) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
# File 'app/models/coupon.rb', line 7

def eligible?(order)
  return false if expires_at and Time.now > expires_at
  return false if usage_limit and credits.count >= usage_limit
  return false if starts_at and Time.now < starts_at
  # TODO - also check items in the order (once we support product groups for coupons)
  true
end