Sha256: 5fa158bff36f4aab892cb535e1d38e82ea084df8f1595b4b3c3785399b651b64

Contents?: true

Size: 1.81 KB

Versions: 11

Compression:

Stored size: 1.81 KB

Contents

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

  # Properties
  property :name
  property :surname
  property :email
  property :crypted_password
  property :role

  view_by :email

  # 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_format_of       :email,    :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  validates_format_of       :role,     :with => /[A-Za-z]/
  validate                  :unique_email_validator

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

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

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

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

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

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

  def unique_email_validator
    account = self.class.find_by_email(email)

    # didn't find email in the database
    return if account.nil?

    # account with same email in database is this account
    return if has_key?('_id') && self['_id'] == account['_id']

    errors.add(:email, "is not unique")
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
bootstrap-on-0.0.9 lib/bootstrap-on/templates/account/couchrest.rb.tt
bootstrap-on-0.0.8 lib/bootstrap-on/templates/account/couchrest.rb.tt
bootstrap-on-0.0.7 lib/bootstrap-on/templates/account/couchrest.rb.tt
bootstrap-on-0.0.6 lib/bootstrap-on/templates/account/couchrest.rb.tt
bootstrap-on-0.0.5 lib/bootstrap-on/templates/account/couchrest.rb.tt
bootstrap-on-0.0.4 lib/bootstrap-on/templates/account/couchrest.rb.tt
bootstrap-on-0.0.3 lib/bootstrap-on/templates/account/couchrest.rb.tt
bootstrap-on-0.0.2 lib/bootstrap-on/templates/account/couchrest.rb.tt
bootstrap-on-0.0.1 lib/bootstrap-on/templates/account/couchrest.rb.tt
bootstrap-on-0.0.0 lib/bootstrap-on/templates/account/couchrest.rb.tt
padrino-admin-0.10.7 lib/padrino-admin/generators/templates/account/couchrest.rb.tt