Sha256: 507a0d00adda398cc3eabfc7da25cb1f99b8ecf67cfa1db4a5136a5824e1a85e

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

class <%= @model_name %> < ActiveRecord::Base
  attr_accessor :password, :password_confirmation

  # Validations
  validates_presence_of     :email, :role
  validates_presence_of     :password,                   :if => :password_required
  validates_presence_of     :password_confirmation,      :if => :password_required
  validates_length_of       :password, :within => 4..40, :if => :password_required
  validates_confirmation_of :password,                   :if => :password_required
  validates_length_of       :email,    :within => 3..100
  validates_uniqueness_of   :email,    :case_sensitive => false
  validates_format_of       :email,    :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  validates_format_of       :role,     :with => /[A-Za-z]/

  # Callbacks
  before_save :encrypt_password, :if => :password_required

  ##
  # This method is for authentication purpose.
  #
  def self.authenticate(email, password)
    account = first(:conditions => ["lower(email) = lower(?)", email]) if email.present?
    account && account.has_password?(password) ? account : nil
  end

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

  private

  def encrypt_password
    value = ::BCrypt::Password.create(password)
    value = value.force_encoding(Encoding::UTF_8) if value.encoding == Encoding::ASCII_8BIT
    self.crypted_password = value
  end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
padrino-admin-0.12.1 lib/padrino-admin/generators/templates/account/activerecord.rb.tt
padrino-admin-0.12.0 lib/padrino-admin/generators/templates/account/activerecord.rb.tt
padrino-admin-0.12.0.rc3 lib/padrino-admin/generators/templates/account/activerecord.rb.tt
padrino-admin-0.12.0.rc2 lib/padrino-admin/generators/templates/account/activerecord.rb.tt
padrino-admin-0.12.0.rc1 lib/padrino-admin/generators/templates/account/activerecord.rb.tt