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::DELEGATION_RESERVED_KEYWORDS, Module::DELEGATION_RESERVED_METHOD_NAMES, Module::RUBY_RESERVED_KEYWORDS

Instance Method Summary collapse

Methods inherited from Module

#alias_attribute, #anonymous?, #attr_internal_accessor, #attr_internal_reader, #attr_internal_writer, #delegate, #delegate_missing_to, #deprecate, #mattr_accessor, #mattr_reader, #mattr_writer, #method_visibility, #module_parent, #module_parent_name, #module_parents, #parent, #parent_name, #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) ⇒ InstanceMethodsOnActivation

Returns a new instance of InstanceMethodsOnActivation.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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?
      self.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.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 = send("#{attribute}_digest")
    BCrypt::Password.new(attribute_digest).is_password?(unencrypted_password) && self
  end

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