2013-05-06 23:39:01 -05:00
|
|
|
require_dependency 'trashable'
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
class Invite < ActiveRecord::Base
|
2013-05-06 23:39:01 -05:00
|
|
|
include Trashable
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
2013-02-28 12:54:12 -06:00
|
|
|
belongs_to :invited_by, class_name: 'User'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
has_many :topic_invites
|
2013-02-07 09:45:24 -06:00
|
|
|
has_many :topics, through: :topic_invites, source: :topic
|
2013-02-05 13:16:51 -06:00
|
|
|
validates_presence_of :email
|
|
|
|
validates_presence_of :invited_by_id
|
|
|
|
|
|
|
|
before_create do
|
|
|
|
self.invite_key ||= SecureRandom.hex
|
|
|
|
end
|
|
|
|
|
|
|
|
before_save do
|
2013-04-14 19:20:33 -05:00
|
|
|
self.email = Email.downcase(email)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
validate :user_doesnt_already_exist
|
|
|
|
attr_accessor :email_already_exists
|
2013-02-07 09:45:24 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def user_doesnt_already_exist
|
|
|
|
@email_already_exists = false
|
|
|
|
return if email.blank?
|
2013-04-14 19:20:33 -05:00
|
|
|
if User.where("email = ?", Email.downcase(email)).exists?
|
2013-02-05 13:16:51 -06:00
|
|
|
@email_already_exists = true
|
|
|
|
errors.add(:email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redeemed?
|
|
|
|
redeemed_at.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def expired?
|
|
|
|
created_at < SiteSetting.invite_expiry_days.days.ago
|
|
|
|
end
|
|
|
|
|
|
|
|
def redeem
|
2013-05-30 10:33:11 -05:00
|
|
|
InviteRedeemer.new(self).redeem unless expired? || destroyed?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-05-23 21:48:32 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: invites
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# invite_key :string(32) not null
|
|
|
|
# email :string(255) not null
|
|
|
|
# invited_by_id :integer not null
|
|
|
|
# user_id :integer
|
|
|
|
# redeemed_at :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# deleted_at :datetime
|
2013-07-13 20:24:16 -05:00
|
|
|
# deleted_by_id :integer
|
2013-05-23 21:48:32 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_invites_on_email_and_invited_by_id (email,invited_by_id) UNIQUE
|
|
|
|
# index_invites_on_invite_key (invite_key) UNIQUE
|
|
|
|
#
|
|
|
|
|