journalduhacker/app/models/invitation.rb

30 lines
566 B
Ruby
Raw Normal View History

2012-07-01 20:31:31 +02:00
class Invitation < ActiveRecord::Base
belongs_to :user
validate do
unless email.to_s.match(/\A[^@ ]+@[^ @]+\.[^ @]+\z/)
2012-07-01 20:31:31 +02:00
errors.add(:email, "is not valid")
end
end
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
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_now
2012-07-01 20:31:31 +02:00
end
end