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
|
|
|
|
|
2013-07-20 05:05:00 +02:00
|
|
|
before_validation :create_code,
|
|
|
|
:on => :create
|
2012-07-01 20:31:31 +02:00
|
|
|
|
|
|
|
def create_code
|
|
|
|
(1...10).each do |tries|
|
|
|
|
if tries == 10
|
|
|
|
raise "too many hash collisions"
|
|
|
|
end
|
|
|
|
|
2013-12-24 04:20:06 +01:00
|
|
|
self.code = Utils.random_str(15)
|
|
|
|
unless Invitation.exists?(:code => self.code)
|
2012-07-01 20:31:31 +02:00
|
|
|
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
|