Sha256: 9dfca60c3129a2d621e31b89b46c408404ad0f5ad0d8f62d6b48cba998131d11

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

class Admin < ActiveRecord::Base
  # Virtual attributes
  attr_accessor :is_generated_password

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable,
         :async,
         :recoverable,
         :rememberable,
         :trackable,
         :validatable

  # Helpers
  audited except: [:password]

  # Validations
  validates_presence_of :name, :email, :surname
  validates :email, uniqueness: true

  # Callbacks
  after_commit :send_login_info, on: :create
  before_validation :create_password, on: :create
  after_initialize do |obj|
    obj.is_generated_password = false
  end

  def active_for_authentication?
    super && self.is_active
  end

  def full_name
    "#{self.name} #{self.surname}"
  end

  private

  def create_password
    if self.password.nil?
      password                    = Devise.friendly_token.first(8)
      self.password               = password
      self.password_confirmation  = password
      self.is_generated_password  = true
    end
  end

  def send_login_info
    AdminMailer.login_info(self.id, self.password).deliver_later! if self.is_generated_password
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cybele-1.9.2 templates/app/models/admin.rb
cybele-1.9.1 templates/app/models/admin.rb
cybele-1.9.0 templates/app/models/admin.rb