Skip to content

Instantly share code, notes, and snippets.

@ricardodovalle
Created November 27, 2013 08:55
Show Gist options
  • Save ricardodovalle/7672677 to your computer and use it in GitHub Desktop.
Save ricardodovalle/7672677 to your computer and use it in GitHub Desktop.
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string(255) default(""), not null
# encrypted_password :string(255) default("")
# reset_password_token :string(255)
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default(0)
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# confirmation_token :string(255)
# confirmed_at :datetime
# confirmation_sent_at :datetime
# unconfirmed_email :string(255)
# failed_attempts :integer default(0)
# unlock_token :string(255)
# locked_at :datetime
# authentication_token :string(255)
# deleted_at :datetime
# created_at :datetime
# updated_at :datetime
# name :string(255)
# invitation_token :string(60)
# invitation_sent_at :datetime
# invitation_accepted_at :datetime
# invitation_limit :integer
# invited_by_id :integer
# invited_by_type :string(255)
# tenant_id :integer
# locale :string(255)
# channel :string(255)
#
class User < ActiveRecord::Base
include Gravtastic
# Include default devise modules. Others available are:
# :omniauthable, :registerable,
devise :invitable, :database_authenticatable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable,
:lockable, :timeoutable, :token_authenticatable
#has_and_belongs_to_many :tenants
belongs_to :tenant
has_paper_trail
rolify
before_create :generate_channel
before_save :ensure_authentication_token
validates :email, presence: true, uniqueness: true
private
def generate_channel
self.channel = loop do
random_token = SecureRandom.urlsafe_base64(nil, false)
break random_token unless self.class.exists?(channel: random_token)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment