Sha256: eb9355b284cb0b11e15a95e5e40dba3d56cf52469c0f544cdd40807e5710c11c

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

class Account < Ohm::Model
  attr_accessor :password, :password_confirmation

  # Keys
  attribute :name
  attribute :surname
  attribute :email
  attribute :crypted_password
  attribute :role

  index  :email
  unique :email

  # Validations
  def validate
    assert_present  :email
    assert_present  :role
    if password_required
      assert_present :password
      assert_present :password_confirmation
      assert_length  :password, 4..40
      assert self.password == self.password_confirmation, [:password_not_confirmed]
    end
    assert_email :email
    assert_format :role, /[A-Za-z]/
  end

  # Callbacks
  def save!
    encrypt_password
    super
  end

  ##
  # This method is for authentication purpose
  #
  def self.authenticate(email, password)
    account = with(:email, email) if email.present?
    account && account.has_password?(password) ? account : nil
  end

  ##
  # This method is used by AuthenticationHelper
  #
  def self.find_by_id(id)
    self[id]
  end

  def has_password?(password)
    ::BCrypt::Password.new(crypted_password) == password
  end

  private
  def encrypt_password
    self.crypted_password = ::BCrypt::Password.create(password)  if password_required
  end

  def password_required
    crypted_password.blank? || password.present?
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
padrino-admin-0.11.1 lib/padrino-admin/generators/templates/account/ohm.rb.tt
padrino-admin-0.11.0 lib/padrino-admin/generators/templates/account/ohm.rb.tt