Class: ActiveModel::SecurePassword::InstanceMethodsOnActivation

Inherits:
Module
  • Object
show all
Defined in:
activemodel/lib/active_model/secure_password.rb

Constant Summary

Constants inherited from Module

Module::DelegationError

Instance Method Summary collapse

Methods inherited from Module

#alias_attribute, #anonymous?, #as_json, #attr_internal_accessor, #attr_internal_reader, #attr_internal_writer, #deep_dup, #delegate, #delegate_missing_to, #deprecate, #mattr_accessor, #mattr_reader, #mattr_writer, #method_visibility, #module_parent, #module_parent_name, #module_parents, #redefine_method, #redefine_singleton_method, #remove_possible_method, #remove_possible_singleton_method, #silence_redefinition_of_method, #thread_mattr_accessor, #thread_mattr_reader, #thread_mattr_writer

Methods included from Module::Concerning

#concern, #concerning

Constructor Details

#initialize(attribute, reset_token:, algorithm:) ⇒ InstanceMethodsOnActivation

Returns a new instance of InstanceMethodsOnActivation.



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'activemodel/lib/active_model/secure_password.rb', line 265

def initialize(attribute, reset_token:, algorithm:)
  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)
      password_digest = algorithm.hash_password(unencrypted_password)
      self.public_send("#{attribute}_digest=", password_digest)
    end
  end

  attr_accessor :"#{attribute}_confirmation", :"#{attribute}_challenge"

  # 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")
    attribute_digest.present? && algorithm.verify_password(unencrypted_password, attribute_digest) && self
  end

  # Returns the salt, a small chunk of random data added to the password before it's hashed.
  define_method("#{attribute}_salt") do
    attribute_digest = public_send("#{attribute}_digest")
    attribute_digest.present? ? algorithm.password_salt(attribute_digest) : nil
  end

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

  if reset_token
    # Returns the class-level configured reset token for the password.
    define_method("#{attribute}_reset_token") do
      generate_token_for(:"#{attribute}_reset")
    end
  end

  define_method("#{attribute}_algorithm") do
    algorithm.algorithm_name
  end
end