Sha256: e02614a476d6e47ace2a36b6590d771e9b6e7a5779a955710fb6463bd7a70058

Contents?: true

Size: 1.44 KB

Versions: 10

Compression:

Stored size: 1.44 KB

Contents

class Account < ::Sequel::Model

  plugin :validation_helpers

  attr_accessor :password, :password_confirmation

  def validate
    validates_presence     :email
    validates_presence     :role
    validates_presence     :password if password_required
    validates_presence     :password_confirmation if password_required
    validates_length_range 4..40, :password unless password.blank?
    errors.add(:password_confirmation, 'must confirm password') if !password.blank? && password != password_confirmation
    validates_length_range 3..100, :email unless email.blank?
    validates_unique       :email unless email.blank?
    validates_format       /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :email unless email.blank?
    validates_format       /[A-Za-z]/, :role unless role.blank?
  end

  # Callbacks
  def before_save
    encrypt_password
  end

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

  ##
  # Replace AR method
  #
  def self.find_by_id(id)
    self[id] rescue nil
  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.present?
    end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
padrino-admin-0.10.5 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.10.4 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.10.3 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.10.2 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.9.27 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.9.26 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.9.24 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.9.25 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.9.23 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.9.22 lib/padrino-admin/generators/templates/account/sequel.rb.tt