journalduhacker/app/models/invitation.rb
2012-08-24 10:57:11 -05:00

29 lines
536 B
Ruby

class Invitation < ActiveRecord::Base
belongs_to :user
validate do
if !email.to_s.match(/\A[^@]+@[^@]+\.[^@]+\z/)
errors.add(:email, "is not valid")
end
end
before_create :create_code
after_create :send_email
def create_code
(1...10).each do |tries|
if tries == 10
raise "too many hash collisions"
end
if !Invitation.find_by_code(self.code = Utils.random_str(15))
break
end
end
end
def send_email
InvitationMailer.invitation(self).deliver
end
end