Created
June 19, 2012 02:58
-
-
Save twe4ked/2952055 to your computer and use it in GitHub Desktop.
Beta code system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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