Sha256: dafd793f117a6e8b969aae538ddd97d0822b5e2aa389d88436ac4637d54eb568

Contents?: true

Size: 1.35 KB

Versions: 18

Compression:

Stored size: 1.35 KB

Contents

class UserInvitation < ActiveRecord::Base
  include ActiveUUID::UUID

  validates :email, :email => true, :presence => true
  validates :roles, presence: true
  validate :email_already_used?
  before_save :set_token
  before_create :set_sent_at
  after_create :send_email

  belongs_to :account

  attr_accessible :email, :roles, :account_id, :invited_by

  def self.find_by_invitation_token(invitation_token)
    begin
      user_invitation = UserInvitation.find(id = invitation_token.gsub(/.{27}$/, ''))
      if user_invitation.token == invitation_token.gsub(/^#{id}/, '')
        return user_invitation
      else
        return nil
      end
    rescue
      return nil
    end
  end

  def accept(user)
    if account.account_users.where(:user_id => user.id).empty?
      account_user = account.account_users.create(:user => user)
      account_user.set_roles(roles.split(',')) if roles
      self.used = true
      save
      true
    else
      false
    end
  end

  private

  def set_token
    self.token = SecureRandom.urlsafe_base64(20) unless token
  end

  def set_sent_at
    self.sent_at = Time.now
  end

  def send_email
    IuguMailer.invitation(self).deliver
  end

  def email_already_used?
    if !AccountUser.joins(:user).where(:account_id => account_id, 'users.email' => email).empty?
      errors.add(:email, "already used in this account")
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
iugusdk-1.0.11 app/models/user_invitation.rb
iugusdk-1.0.10 app/models/user_invitation.rb
iugusdk-1.0.9 app/models/user_invitation.rb
iugusdk-1.0.8 app/models/user_invitation.rb
iugusdk-1.0.7 app/models/user_invitation.rb
iugusdk-1.0.6 app/models/user_invitation.rb
iugusdk-1.0.5 app/models/user_invitation.rb
iugusdk-1.0.4 app/models/user_invitation.rb
iugusdk-1.0.3 app/models/user_invitation.rb
iugusdk-1.0.2 app/models/user_invitation.rb
iugusdk-1.0.1 app/models/user_invitation.rb
iugusdk-1.0.0.alpha.32 app/models/user_invitation.rb
iugusdk-1.0.0.alpha.31 app/models/user_invitation.rb
iugusdk-1.0.0.alpha.30 app/models/user_invitation.rb
iugusdk-1.0.0.alpha.29 app/models/user_invitation.rb
iugusdk-1.0.0.alpha.28 app/models/user_invitation.rb
iugusdk-1.0.0.alpha.27 app/models/user_invitation.rb
iugusdk-1.0.0.alpha.26 app/models/user_invitation.rb