Created
February 6, 2012 18:04
-
-
Save jqr/1753741 to your computer and use it in GitHub Desktop.
Instrumental Invitation
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 Invitation < ActiveRecord::Base | |
belongs_to :project | |
belongs_to :invited_by, class_name: 'User' | |
belongs_to :invited, class_name: 'User' | |
before_create :set_token | |
before_create :auto_accept | |
after_create :send_email | |
def set_token | |
self.token ||= SecureRandom.hex(16) | |
end | |
def auto_accept | |
if user = User.find_by_email(email) | |
accept!(user) | |
end | |
end | |
def send_email | |
if accepted? | |
# TODO: send notice of addition | |
else | |
CustomerMailer.async_email(:invitation, id) | |
end | |
end | |
def accept!(user) | |
if !accepted? | |
transaction do | |
self.token = nil | |
self.invited = user | |
self.accepted_at = Time.now | |
project.memberships.create!(:user => user) | |
save! unless new_record? | |
end | |
end | |
end | |
def accepted? | |
accepted_at? | |
end | |
def to_param | |
token | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment