journalduhacker/app/models/invitation.rb
joshua stein ccb8094da1 change some before_create's to before_validation, :on => :create
random tokens need to be filled in before validation, otherwise the
validation is just checking a blank value
2013-07-19 22:05:00 -05:00

31 lines
562 B
Ruby

class Invitation < ActiveRecord::Base
belongs_to :user
attr_accessible nil
validate do
unless email.to_s.match(/\A[^@ ]+@[^ @]+\.[^ @]+\z/)
errors.add(:email, "is not valid")
end
end
before_validation :create_code,
:on => :create
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