Class: Coupon

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

Overview

Schema Information

Table name: coupons

id            :integer(4)      not null, primary key
type          :string(255)     not null
code          :string(255)     not null
amount        :decimal(8, 2)   default(0.0)
minimum_value :decimal(8, 2)
percent       :integer(4)      default(0)
description   :text            default(""), not null
combine       :boolean(1)      default(FALSE)
starts_at     :datetime
expires_at    :datetime
created_at    :datetime
updated_at    :datetime

Direct Known Subclasses

CouponPercent, CouponValue

Constant Summary

COUPON_TYPES =
['CouponPercent', 'CouponValue','CouponFirstPurchasePercent', 'CouponFirstPurchaseValue']

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) c_type

order must respond to item_prices



53
54
55
# File 'app/models/coupon.rb', line 53

def c_type
  @c_type
end

Instance Method Details

- (Object) display_expires_time(format = :us_date)



75
76
77
# File 'app/models/coupon.rb', line 75

def display_expires_time(format = :us_date)
  expires_at ? I18n.localize(expires_at, :format => format) : 'N/A'
end

- (Object) display_start_time(format = :us_date)



71
72
73
# File 'app/models/coupon.rb', line 71

def display_start_time(format = :us_date)
  starts_at ? I18n.localize(starts_at, :format => format) : 'N/A'
end

- (Boolean) eligible?(order, at = nil)

Returns:

  • (Boolean)


66
67
68
69
# File 'app/models/coupon.rb', line 66

def eligible?(order, at = nil)
  at ||= order.completed_at || Time.zone.now
  starts_at <= at && expires_at >= at
end

- (Boolean) qualified?(item_prices, order, at = nil)

Does the coupon meet the criteria to apply it. (is the order price total over the coupon's minimum value)

Returns:

  • (Boolean)


61
62
63
64
# File 'app/models/coupon.rb', line 61

def qualified?(item_prices, order, at = nil)
  at ||= order.completed_at || Time.zone.now
  item_prices.sum > minimum_value && eligible?(at)
end

- (Object) value(item_prices, order)

amount the coupon will reduce the order



56
57
58
# File 'app/models/coupon.rb', line 56

def value(item_prices, order)
  qualified?(item_prices, order) ? coupon_amount(item_prices) : 0.0
end