Sha256: 44df33f29da4b31117bb07aaff23f5f45ef3bb56101781091f6277714ce0f557

Contents?: true

Size: 941 Bytes

Versions: 2

Compression:

Stored size: 941 Bytes

Contents

# An invitation, tracks the sender and recipient, and what the recipient is invited to.
# Generates a unique token for each invitation. The token is used in the invite url
# to (more) securely identify the invite when a new user clicks to register.
#
class Invite < ActiveRecord::Base
  belongs_to :invitable, polymorphic: true
  belongs_to :sender, class_name: Invitation.configuration.user_model_class_name
  belongs_to :recipient, class_name: Invitation.configuration.user_model_class_name

  before_create :generate_token
  before_save :check_recipient_existence

  validates :email, presence: true
  validates :invitable, presence: true
  validates :sender, presence: true


  def generate_token
    self.token = SecureRandom.hex(20).encode('UTF-8')
  end

  def check_recipient_existence
    recipient = Invitation.configuration.user_model.find_by_email(email)
    if recipient
      self.recipient_id = recipient.id
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
invitation-0.0.2 app/models/invite.rb
invitation-0.0.1 app/models/invite.rb