Module: MuckCommerce::Models::MuckCoupon::ClassMethods

Defined in:
lib/muck-commerce/models/coupon.rb

Instance Method Summary collapse

Instance Method Details

#active_code?(code) ⇒ Boolean

Checks the database to ensure the specified code is not taken

Returns:

  • (Boolean)


39
40
41
# File 'lib/muck-commerce/models/coupon.rb', line 39

def active_code?(code)
  Coupon.find_by_code(code)
end

#calculate_total_discount(amount, discount_codes) ⇒ Object

Calculate total discount for a given array of coupon codes. amount: amount in cents to calculate discount for discount_codes: a string coupon code or array of coupon codes



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/muck-commerce/models/coupon.rb', line 56

def calculate_total_discount(amount, discount_codes)
  coupon_amount = 0
  coupon_override = false
  coupons = []
  if discount_codes
    discount_codes = [discount_codes] unless discount_codes.is_a?(Array)
    discount_codes.each do |discount_code|
      coupon = Coupon.find_by_code(discount_code)
      if coupon
        coupon_amount = coupon.calculate_discount(amount)
        coupon.redeem
        coupons << coupon
      end
      if amount - coupon_amount == 0
        coupon_override = true
      end
    end
  end
  [coupons, coupon_amount, coupon_override]
end

#check_referrals(coupon_codes) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/muck-commerce/models/coupon.rb', line 43

def check_referrals(coupon_codes)
  # Check to see if the discount code is associated with another user  ie person one invites person two
  # and so both people get a free month of storage.
  users = []
  coupon_codes.each do |code|
    coupon = Coupon.find_by_code(code)
    coupon.give_referral_benefit if coupon
  end
end

#random_codeObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/muck-commerce/models/coupon.rb', line 27

def random_code
  coupon_length = 14 # will generate a code 15 chars long
  letters = ['B', 'C', 'D', 'F', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z']
  numbers = [2, 3, 4, 7, 9]
  promo_set = letters | numbers # combine arrays
  begin
    promo_code = promo_set.sort_by{rand}[0..coupon_length].to_s # randomize array and take the first 15 elements and make them a string
  end until !self.active_code?(promo_code)
  promo_code
end