Module: Recognition::Models::Redeemable
Instance Method Summary collapse
- #bucket ⇒ Object
- #expired? ⇒ Boolean
- #get_user_counter(id) ⇒ Object
- #is_redeemable?(recognizable) ⇒ Boolean
- #redeem(recognizable) ⇒ Object
- #regenerate_code ⇒ Object
- #transactions(page = 0, per = 20) ⇒ Object
Instance Method Details
#bucket ⇒ Object
80 81 82 |
# File 'lib/recognition/models/redeemable.rb', line 80 def bucket "#{self.class.to_s.downcase}:#{ self.code }" end |
#expired? ⇒ Boolean
20 21 22 |
# File 'lib/recognition/models/redeemable.rb', line 20 def expired? defined?(self.expires_at) && self.expires_at.present? && self.expires_at < DateTime.now end |
#get_user_counter(id) ⇒ Object
84 85 86 |
# File 'lib/recognition/models/redeemable.rb', line 84 def get_user_counter id Recognition::Database.get_user_counter id, bucket end |
#is_redeemable?(recognizable) ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/recognition/models/redeemable.rb', line 24 def is_redeemable? recognizable # default: not redeemable pass = false # only check if the redeemable did not expire if expired? Recognition.log self.class.to_s.downcase.to_sym, "validation error for #{self.class.to_s}##{self.id}: expired" errors.add(:base, "#{self.class.to_s} expired") else # has the redeemable ever been redeemed? if transactions.any? Recognition.log 'debug', get_user_counter(recognizable.id) # has the redeemable ever been redeemed by this user? if get_user_counter(recognizable.id) != 0 Recognition.log self.class.to_s.downcase.to_sym, "validation error for #{self.class.to_s}##{self.id}: user has already redeemed the voucher" errors.add(:base, "#{self.class.to_s} has already been redeemed") pass = false # is the redeemable reusable? elsif defined?(self.reusable?) && self.reusable? pass = true else errors.add(:base, "#{self.class.to_s} has already been redeemed") pass = false end else pass = true end end pass end |
#redeem(recognizable) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/recognition/models/redeemable.rb', line 54 def redeem recognizable pass = false # Make sure we have an amount beforehand if defined? self.amount if self.redeemable? recognizable # Call custom validators if self.class.redemption_validators.present? self.class.redemption_validators.each do |validator| # quit if any validator returned false if send(validator) == false Recognition.log self.class.to_s.downcase.to_sym, "validation error for #{self.class.to_s}##{self.id}: custom validation error" return pass end end end # If all went well: execute_redemption recognizable.id pass = true end else Recognition.log self.class.to_s.downcase.to_sym, "validation error for #{self.class.to_s}##{self.id}: amount is nil" end pass end |
#regenerate_code ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/recognition/models/redeemable.rb', line 4 def regenerate_code self.class.recognitions[:code_length] = 10 if self.class.recognitions[:code_length].nil? prefix = Recognition::Parser.parse_code_part(self.class.recognitions[:prefix], self) suffix = Recognition::Parser.parse_code_part(self.class.recognitions[:suffix], self) l = (self.class.recognitions[:code_length] - (prefix.length + suffix.length)) dict = [('a'..'z'),('A'..'Z'),(0..9)].map{|i| i.to_a}.flatten code = (1..l).map{ dict[rand(dict.length)] }.prepend(prefix).append(suffix).join # Prevent code collision at all costs if self.class.to_s.constantize.find_all_by_code(code).any? regenerate_code else self.code = code self end end |
#transactions(page = 0, per = 20) ⇒ Object
88 89 90 91 92 |
# File 'lib/recognition/models/redeemable.rb', line 88 def transactions page = 0, per = 20 start = page * per stop = (1 + page) * per Recognition::Database.get_transactions bucket, start, stop end |