Module: MuckCommerce::Models::MuckCoupon

Extended by:
ActiveSupport::Concern
Defined in:
lib/muck-commerce/models/coupon.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#calculate_discount(amount) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/muck-commerce/models/coupon.rb', line 92

def calculate_discount(amount)
  
  coupon_amount = 0
  
  if self.invalid?
    raise MuckCommerce::Exceptions::CouponError, 'The coupon you entered has expired or is no longer valid'
  else
    if self.amount > 0
      coupon_amount = self.amount
    elsif self.percent > 0
      coupon_amount = amount * (0.01 * self.percent)
    end
  end
  
  coupon_amount = 0 if coupon_amount < 0    
  coupon_amount
end

#expired?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/muck-commerce/models/coupon.rb', line 118

def expired?
  self.expires_at? && self.expires_at < Time.now
end

#give_referral_benefitObject



79
80
81
# File 'lib/muck-commerce/models/coupon.rb', line 79

def give_referral_benefit
  raise MuckCommerce::Exceptions::CouponError, "Implement the 'give_referral_benefit' method in coupon.rb."
end

#invalid?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/muck-commerce/models/coupon.rb', line 114

def invalid?
  expired? || overused?
end

#overused?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/muck-commerce/models/coupon.rb', line 122

def overused?
  (self.uses >= self.use_limit) && !self.unlimited
end

#redeemObject



110
111
112
# File 'lib/muck-commerce/models/coupon.rb', line 110

def redeem
  self.update_attribute(:uses, self.uses + 1)
end

#validate_discountObject



83
84
85
86
87
88
89
90
# File 'lib/muck-commerce/models/coupon.rb', line 83

def validate_discount
  if self.percent.blank? && self.amount.blank? 
    errors.add_to_base('Please enter a value for either the coupon amount or percent')
    false
  else
    true
  end
end