Class: ShopBunny::Coupon
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ShopBunny::Coupon
- Defined in:
- app/models/shop_bunny/coupon.rb
Constant Summary collapse
- InvalidEvent =
Class.new(NoMethodError)
Instance Method Summary collapse
- #activate! ⇒ Object
- #expired? ⇒ Boolean
- #has_expired? ⇒ Boolean
- #not_yet_valid? ⇒ Boolean
- #redeem! ⇒ Object
- #redeemable? ⇒ Boolean
- #used_up? ⇒ Boolean
Instance Method Details
#activate! ⇒ Object
36 37 38 39 40 |
# File 'app/models/shop_bunny/coupon.rb', line 36 def activate! raise InvalidEvent unless state == 'inactive' self.state = 'active' save! end |
#expired? ⇒ Boolean
16 17 18 |
# File 'app/models/shop_bunny/coupon.rb', line 16 def expired? not_yet_valid? || has_expired? end |
#has_expired? ⇒ Boolean
24 25 26 |
# File 'app/models/shop_bunny/coupon.rb', line 24 def has_expired? Time.now > self.valid_until if self.valid_until end |
#not_yet_valid? ⇒ Boolean
20 21 22 |
# File 'app/models/shop_bunny/coupon.rb', line 20 def not_yet_valid? Time.now < self.valid_from if self.valid_from end |
#redeem! ⇒ Object
42 43 44 45 46 |
# File 'app/models/shop_bunny/coupon.rb', line 42 def redeem! raise InvalidEvent unless redeemable? self.state = 'redeemed' save! end |
#redeemable? ⇒ Boolean
32 33 34 |
# File 'app/models/shop_bunny/coupon.rb', line 32 def redeemable? !not_yet_valid? && !has_expired? && state == 'active' && !used_up? end |
#used_up? ⇒ Boolean
28 29 30 |
# File 'app/models/shop_bunny/coupon.rb', line 28 def used_up? max_uses && max_uses <= coupon_uses.count end |