Skip to content

Instantly share code, notes, and snippets.

@twe4ked
Created June 19, 2012 02:58
Show Gist options
  • Save twe4ked/2952055 to your computer and use it in GitHub Desktop.
Save twe4ked/2952055 to your computer and use it in GitHub Desktop.
Beta code system.
class InviteCode < ActiveRecord::Base
validate :invite_code, :presence => true
def to_s
invite_code
end
def self.generate
8.times.map { [*?a..?z,*?A..?Z].sample }.join
end
end
class User < ActiveRecord::Base
# ...
attr_accessor :invite_code
validates :invite_code, :inclusion => {
:in => proc { InviteCode.all.map { |code| code.invite_code.to_s } },
:message => '%{value} is not a valid invite code'
}, :on => :create
after_create :destroy_invite_code
# ...
private
def destroy_invite_code
InviteCode.find_by_invite_code(invite_code).destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment