Sha256: bdd43640e47d1d5ad7b5eb5db5a6e1c776b7a0ff6bd20c92cc54dadc50233a7b

Contents?: true

Size: 1014 Bytes

Versions: 14

Compression:

Stored size: 1014 Bytes

Contents

class Account < ActiveRecord::Base
  ROLES = %w[admin guest]

  devise :database_authenticatable, :registerable, :recoverable, :rememberable,
         :trackable, :timeoutable, :lockable, :validatable, :async

  before_validation :ensure_password, on: :create

  after_save :invalidate_cache

  validates :role, inclusion: {in: ROLES}

  def self.serialize_from_session(key, salt)
    # store the current_account in the cache so we do not perform a db lookup on each authenticated page
    single_key = key.is_a?(Array) ? key.first : key

    Rails.cache.fetch("account:#{single_key}") do
      Account.where(id: single_key).entries.first
    end
  end

  def self.generate_password(length = 10)
    Devise.friendly_token.first(length)
  end

  def is?(role_check)
    role.to_sym == role_check
  end

  private

  def ensure_password
    # only generate a password if it does not exist
    self.password ||= Account.generate_password
  end

  def invalidate_cache
    Rails.cache.delete("account:#{id}")
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
orats-0.9.7 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.9.6 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.9.5 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.9.4 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.9.3 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.9.2 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.9.1 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.9.0 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.8.1 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.8.0 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.7.3 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.7.2 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.7.1 lib/orats/templates/includes/new/rails/app/models/account.rb
orats-0.7.0 lib/orats/templates/includes/new/rails/app/models/account.rb