Sha256: 5d294c56d22cd184f126cda0f0dcfda6a87a7b0ee5f6a8a4a204947c1e23460a

Contents?: true

Size: 1.59 KB

Versions: 39

Compression:

Stored size: 1.59 KB

Contents

class <%= @model_name %>
  include Dynamoid::Document
  attr_accessor :password, :password_confirmation

  # Fields
  field :name
  field :surname
  field :email
  field :crypted_password
  field :role

  # 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]/

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

  ##
  # This method is for authentication purpose.
  #
  def self.authenticate(email, password)
    account = find_by_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, options = {})
    id ? super(id, options) : 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

39 entries across 39 versions & 2 rubygems

Version Path
padrino-admin-0.16.0.pre3 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.16.0.pre2 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.15.3 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.15.2 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.15.1 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.15.0 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.4 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.3 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.12.9 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.2 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.13.3.4 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.1.1 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.1 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.0.2 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.0.1 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.0 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.0.rc2 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.14.0.rc1 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.13.3.3 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt
padrino-admin-0.13.3.2 lib/padrino-admin/generators/templates/account/dynamoid.rb.tt