foodsoft/app/models/invite.rb

36 lines
1011 B
Ruby
Raw Normal View History

2009-01-06 11:49:19 +01:00
require 'digest/sha1'
# Invites are created by foodcoop users to invite a new user into the foodcoop and their order group.
class Invite < ActiveRecord::Base
belongs_to :user
belongs_to :group
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
2009-01-06 11:49:19 +01:00
validates_presence_of :user
validates_presence_of :group
validates_presence_of :token
validates_presence_of :expires_at
2011-05-11 11:17:02 +02:00
validate :email_not_already_registered, :on => :create
before_validation :set_token_and_expires_at
2009-01-06 11:49:19 +01:00
protected
# Before validation, set token and expires_at.
def set_token_and_expires_at
2009-01-06 11:49:19 +01:00
self.token = Digest::SHA1.hexdigest(Time.now.to_s + rand(100).to_s)
self.expires_at = Time.now.advance(:days => 7)
2009-01-06 11:49:19 +01:00
end
private
# Custom validation: check that email does not already belong to a registered user.
2011-05-11 11:17:02 +02:00
def email_not_already_registered
unless User.find_by_email(self.email).nil?
2013-02-27 11:59:03 +01:00
errors.add(:email, I18n.t('invites.errors.already_member'))
end
2009-01-06 11:49:19 +01:00
end
end