method
new
v6.1.3.1 -
Show latest stable
-
0 notes -
Class: InstanceMethodsOnActivation
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
new(attribute)
public
Hide source
# 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.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