Module: ActiveModel::SecurePassword::ClassMethods

### Home Classes Methods

### In Files

- lib/active_model/secure_password.rb

### Methods

- ::attributes_protected_by_default
- #has_secure_password

### Included Modules

- ActiveModel::SecurePassword::InstanceMethodsOnActivation

### Class/Module Index

Quicksearch

- ActiveModel
- ActiveModel::AttributeMethods
- ActiveModel::AttributeMethods::ClassMethods
- ActiveModel::AttributeMethods::ClassMethods::AttributeMethodMatcher
- ActiveModel::BlockValidator
- ActiveModel::Callbacks
- ActiveModel::Conversion
- ActiveModel::Dirty
- ActiveModel::EachValidator
- ActiveModel::Errors
- ActiveModel::Lint
- ActiveModel::Lint::Tests
- ActiveModel::MassAssignmentSecurity
- ActiveModel::MassAssignmentSecurity::BlackList
- ActiveModel::MassAssignmentSecurity::ClassMethods
- ActiveModel::MassAssignmentSecurity::Error
- ActiveModel::MassAssignmentSecurity::LoggerSanitizer
- ActiveModel::MassAssignmentSecurity::PermissionSet
- ActiveModel::MassAssignmentSecurity::Sanitizer
- ActiveModel::MassAssignmentSecurity::StrictSanitizer
- ActiveModel::MassAssignmentSecurity::WhiteList
- ActiveModel::MissingAttributeError
- ActiveModel::Name
- ActiveModel::Naming
- ActiveModel::Observer
- ActiveModel::ObserverArray
- ActiveModel::Observing
- ActiveModel::Observing::ClassMethods
- ActiveModel::SecurePassword
- ActiveModel::SecurePassword::ClassMethods
- ActiveModel::SecurePassword::InstanceMethodsOnActivation
- ActiveModel::Serialization
- ActiveModel::Serializers
- ActiveModel::Serializers::JSON
- ActiveModel::Serializers::Xml
- ActiveModel::StrictValidationFailed
- ActiveModel::Translation
- ActiveModel::Validations
- ActiveModel::Validations::AcceptanceValidator
- ActiveModel::Validations::Callbacks
- ActiveModel::Validations::Callbacks::ClassMethods
- ActiveModel::Validations::ClassMethods
- ActiveModel::Validations::ConfirmationValidator
- ActiveModel::Validations::ExclusionValidator
- ActiveModel::Validations::FormatValidator
- ActiveModel::Validations::HelperMethods
- ActiveModel::Validations::InclusionValidator
- ActiveModel::Validations::LengthValidator
- ActiveModel::Validations::NumericalityValidator
- ActiveModel::Validations::PresenceValidator
- ActiveModel::Validations::WithValidator
- ActiveModel::Validator

No matching classes.

# ActiveModel::SecurePassword::ClassMethods

### Public Class Methods

attributes_protected_by_default() click to toggle source

    # File lib/active_model/secure_password.rb, line 48
    def self.attributes_protected_by_default
      super + ['password_digest']
    end

### Public Instance Methods

has_secure_password() click to toggle source

Adds methods to set and authenticate against a BCrypt password. This mechanism requires you to have a password_digest attribute.

Validations for presence of password, confirmation of password (using a "password_confirmation" attribute) are automatically added. You can add more validations by hand if need be.

You need to add bcrypt-ruby (~> 3.0.0) to Gemfile to use has_secure_password:

    gem 'bcrypt-ruby', '~> 3.0.0'

Example using Active Record (which automatically includes ActiveModel::SecurePassword):

    # Schema: User(name:string, password_digest:string)
    class User < ActiveRecord::Base
      has_secure_password
    end

    user = User.new(:name => "david", :password => "", :password_confirmation => "nomatch")
    user.save                                                      # => false, password required
    user.password = "mUc3m00RsqyRe"
    user.save                                                      # => false, confirmation doesn't match
    user.password_confirmation = "mUc3m00RsqyRe"
    user.save                                                      # => true
    user.authenticate("notright")                                  # => false
    user.authenticate("mUc3m00RsqyRe")                             # => user
    User.find_by_name("david").try(:authenticate, "notright")      # => nil
    User.find_by_name("david").try(:authenticate, "mUc3m00RsqyRe") # => user

    # File lib/active_model/secure_password.rb, line 34
    def has_secure_password
      # Load bcrypt-ruby only when has_secure_password is used.
      # This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library.
      gem 'bcrypt-ruby', '~> 3.0.0'
      require 'bcrypt'

      attr_reader :password

      validates_confirmation_of :password
      validates_presence_of     :password_digest

      include InstanceMethodsOnActivation

      if respond_to?(:attributes_protected_by_default)
        def self.attributes_protected_by_default
          super + ['password_digest']
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.