2012-07-01 20:31:31 +02:00
|
|
|
class Invitation < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
|
2012-07-10 21:44:26 +02:00
|
|
|
attr_accessible nil
|
|
|
|
|
2012-07-01 20:31:31 +02:00
|
|
|
validate do
|
2013-01-23 07:15:05 +01:00
|
|
|
unless email.to_s.match(/\A[^@ ]+@[^ @]+\.[^ @]+\z/)
|
2012-07-01 20:31:31 +02:00
|
|
|
errors.add(:email, "is not valid")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before_create :create_code
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-07-03 18:59:50 +02:00
|
|
|
def send_email
|
|
|
|
InvitationMailer.invitation(self).deliver
|
2012-07-01 20:31:31 +02:00
|
|
|
end
|
|
|
|
end
|