123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::SecurePassword::InstanceMethodsOnActivation

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Module
Instance Chain:
Inherits: Module
  • ::Object
Defined in: activemodel/lib/active_model/secure_password.rb

Constant Summary

::Module - Inherited

DELEGATION_RESERVED_KEYWORDS, DELEGATION_RESERVED_METHOD_NAMES, RUBY_RESERVED_KEYWORDS

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

::Module - Inherited

#anonymous?

A module may or may not have a name.

Instance Method Summary

::Module - Inherited

#alias_attribute

Allows you to make aliases for attributes, which includes getter, setter, and a predicate.

#attr_internal
#attr_internal_accessor

Declares an attribute reader and writer backed by an internally-named instance variable.

#attr_internal_reader

Declares an attribute reader backed by an internally-named instance variable.

#attr_internal_writer

Declares an attribute writer backed by an internally-named instance variable.

#cattr_accessor
#cattr_reader
#cattr_writer
#delegate

Provides a delegate class method to easily expose contained objects’ public methods as your own.

#delegate_missing_to

When building decorators, a common pattern may emerge:

#deprecate

deprecate :foo.

#mattr_accessor

Defines both class and instance accessors for class attributes.

#mattr_reader

Defines a class attribute and creates a class and instance reader methods.

#mattr_writer

Defines a class attribute and creates a class and instance writer methods to allow assignment to the attribute.

#module_parent

Returns the module which contains this one according to its name.

#module_parent_name

Returns the name of the module containing this one.

#module_parents

Returns all the parents of this module according to its name, ordered from nested outwards.

#redefine_method

Replaces the existing method definition, if there is one, with the passed block as its body.

#redefine_singleton_method

Replaces the existing singleton method definition, if there is one, with the passed block as its body.

#remove_possible_method

Removes the named method, if it exists.

#remove_possible_singleton_method

Removes the named singleton method, if it exists.

#silence_redefinition_of_method

Marks the named method as intended to be redefined, if it exists.

#thread_cattr_accessor
#thread_cattr_reader
#thread_cattr_writer
#thread_mattr_accessor

Defines both class and instance accessors for class attributes.

::Module::Concerning - Included

#concern

A low-cruft shortcut to define a concern.

#concerning

Define a new concern and mix it in.

Constructor Details

.new(attribute) ⇒ InstanceMethodsOnActivation

[ GitHub ]

  
# File 'activemodel/lib/active_model/secure_password.rb', line 92

def initialize(attribute)
  attr_reader attribute

  define_method("#{attribute}=") do |unencrypted_password|
    if unencrypted_password.nil?
      instance_variable_set("@#{attribute}", nil)
      self.public_send("#{attribute}_digest=", nil)
    elsif !unencrypted_password.empty?
      instance_variable_set("@#{attribute}", unencrypted_password)
      cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
      self.public_send("#{attribute}_digest=", BCrypt::Password.create(unencrypted_password, cost: cost))
    end
  end

  define_method("#{attribute}_confirmation=") do |unencrypted_password|
    instance_variable_set("@#{attribute}_confirmation", unencrypted_password)
  end

  # Returns self if the password is correct, otherwise false.
  #
  #   class User < ActiveRecord::Base
  #     has_secure_password validations: false
  #   end
  #
  #   user = User.new(name: 'david', password: 'mUc3m00RsqyRe')
  #   user.save
  #   user.authenticate_password('notright')      # => false
  #   user.authenticate_password('mUc3m00RsqyRe') # => user
  define_method("authenticate_#{attribute}") do |unencrypted_password|
    attribute_digest = public_send("#{attribute}_digest")
    BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
  end

  alias_method :authenticate, :authenticate_password if attribute == :password
end