2009-01-08 16:33:27 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: invites
|
|
|
|
#
|
2009-02-02 16:35:43 +01:00
|
|
|
# id :integer not null, primary key
|
2009-01-08 16:33:27 +01:00
|
|
|
# token :string(255) default(""), not null
|
|
|
|
# expires_at :datetime not null
|
2009-02-02 16:35:43 +01:00
|
|
|
# group_id :integer default(0), not null
|
|
|
|
# user_id :integer default(0), not null
|
2009-01-08 16:33:27 +01:00
|
|
|
# email :string(255) default(""), not null
|
|
|
|
#
|
|
|
|
|
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
|
|
|
|
|
2009-01-15 12:14:01 +01:00
|
|
|
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/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
|
2009-08-02 19:41:09 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
protected
|
|
|
|
|
|
|
|
# Before validation, set token and expires_at.
|
|
|
|
def before_validation
|
|
|
|
self.token = Digest::SHA1.hexdigest(Time.now.to_s + rand(100).to_s)
|
|
|
|
self.expires_at = Time.now.advance(:days => 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sends an email to the invited user.
|
|
|
|
def after_create
|
|
|
|
Mailer.deliver_invite(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Custom validation: check that email does not already belong to a registered user.
|
|
|
|
def validate_on_create
|
2009-01-15 12:14:01 +01:00
|
|
|
unless User.find_by_email(self.email).nil?
|
|
|
|
errors.add(:email, 'ist bereits in Verwendung. Person ist schon Mitglied der Foodcoop.')
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|