Sha256: 94a111c3241b1a599b02ad89def9644cd8a782fb0c0148f0392488099bb38686

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 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
    generate_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 generate_password
      self.crypted_password = ::BCrypt::Password.create(password)
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
padrino-admin-0.9.21 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.9.20 lib/padrino-admin/generators/templates/account/sequel.rb.tt
padrino-admin-0.9.19 lib/padrino-admin/generators/templates/account/sequel.rb.tt