From ad7f9cdf008e1261fdcdc8e8152462f69b48c20d Mon Sep 17 00:00:00 2001 From: Robby Grossman Date: Tue, 31 Jul 2012 16:16:21 -0400 Subject: [PATCH] has_secure_password should not raise a 'digest missing' error if the calling class has specified for validations to be skipped. --- activemodel/lib/active_model/secure_password.rb | 4 ++-- activemodel/test/cases/secure_password_test.rb | 8 ++++++++ activemodel/test/models/oauthed_user.rb | 11 +++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 activemodel/test/models/oauthed_user.rb diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb index 3eab745c891b4..dc89efd2d119a 100644 --- a/activemodel/lib/active_model/secure_password.rb +++ b/activemodel/lib/active_model/secure_password.rb @@ -43,9 +43,9 @@ def has_secure_password(options = {}) if options.fetch(:validations, true) validates_confirmation_of :password validates_presence_of :password, :on => :create + + before_create { raise "Password digest missing on new record" if password_digest.blank? } end - - before_create { raise "Password digest missing on new record" if password_digest.blank? } include InstanceMethodsOnActivation diff --git a/activemodel/test/cases/secure_password_test.rb b/activemodel/test/cases/secure_password_test.rb index 5f18909301755..8650b0e495167 100644 --- a/activemodel/test/cases/secure_password_test.rb +++ b/activemodel/test/cases/secure_password_test.rb @@ -1,5 +1,6 @@ require 'cases/helper' require 'models/user' +require 'models/oauthed_user' require 'models/visitor' require 'models/administrator' @@ -8,6 +9,7 @@ class SecurePasswordTest < ActiveModel::TestCase setup do @user = User.new @visitor = Visitor.new + @oauthed_user = OauthedUser.new end test "blank password" do @@ -73,4 +75,10 @@ class SecurePasswordTest < ActiveModel::TestCase @user.run_callbacks :create end end + + test "Oauthed user can be created with blank digest" do + assert_nothing_raised do + @oauthed_user.run_callbacks :create + end + end end diff --git a/activemodel/test/models/oauthed_user.rb b/activemodel/test/models/oauthed_user.rb new file mode 100644 index 0000000000000..9750bc19d490d --- /dev/null +++ b/activemodel/test/models/oauthed_user.rb @@ -0,0 +1,11 @@ +class OauthedUser + extend ActiveModel::Callbacks + include ActiveModel::Validations + include ActiveModel::SecurePassword + + define_model_callbacks :create + + has_secure_password(validations: false) + + attr_accessor :password_digest, :password_salt +end