diff options
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model/secure_password.rb | 74 |
1 files changed, 38 insertions, 36 deletions
diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index cc1368d3a0..5f409326bd 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -69,42 +69,7 @@ module ActiveModel raise end - mod = Module.new do - 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 - - include mod + include InstanceMethodsOnActivation.new(attribute) if validations include ActiveModel::Validations @@ -122,5 +87,42 @@ module ActiveModel end end end + + class InstanceMethodsOnActivation < Module + 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 + end end end |