Created
April 5, 2009 21:31
-
-
Save mchung/90543 to your computer and use it in GitHub Desktop.
This file contains 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
require 'digest/sha1' | |
class Invite < ActiveRecord::Base | |
validates_presence_of :email, :on => :save, :message => "can't be blank" | |
validates_uniqueness_of :email, :on => :save, :message => "is already registered" | |
named_scope :unsent_invitations, :conditions => {:redeemed_at => nil, :invite_code => nil} | |
def invited? | |
!!self.invite_code && !!self.invited_at | |
end | |
def invite! | |
self.invite_code = Digest::SHA1.hexdigest("--#{Time.now.utc.to_s}--#{self.email}--") | |
self.invited_at = Time.now.utc | |
self.save! | |
end | |
def self.find_redeemable(invite_code) | |
self.find(:first, :conditions => {:redeemed_at => nil, :invite_code => invite_code}) | |
end | |
def redeemed! | |
self.redeemed_at = Time.now.utc | |
self.save! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment