diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/invite_code.rb | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb index 13c3a39..5666a4f 100644 --- a/app/models/invite_code.rb +++ b/app/models/invite_code.rb @@ -1,4 +1,5 @@ -require 'coupon_code' +require 'base64' +require 'securerandom' class InviteCode < CouchRest::Model::Base use_database 'invite_codes' @@ -14,10 +15,18 @@ class InviteCode < CouchRest::Model::Base end def initialize(attributes = {}, options = {}) + if !attributes.has_key?("_id") + attributes[:id] = InviteCode.generate_invite + end + super(attributes, options) - write_attribute('invite_code', CouponCode.generate) if new? + + write_attribute('invite_code', attributes[:id]) if new? end + def self.generate_invite + Base64.encode64(SecureRandom.random_bytes).downcase.gsub(/[0oil1+_\/]/,'')[0..7].scan(/..../).join('-') + end end |