Merge pull request #1458 from kirs/update-attrubutes-as

Added support for rails 3.1 new mass assignment conventions
This commit is contained in:
José Valim 2011-11-24 00:59:13 -08:00
commit 5570929b56
2 changed files with 20 additions and 4 deletions

View File

@ -51,7 +51,8 @@ module Devise
# Update record attributes when :current_password matches, otherwise returns
# error on :current_password. It also automatically rejects :password and
# :password_confirmation if they are blank.
def update_with_password(params={})
def update_with_password(params, *options)
as = options.first[:as] rescue nil
current_password = params.delete(:current_password)
if params[:password].blank?
@ -60,7 +61,7 @@ module Devise
end
result = if valid_password?(current_password)
update_attributes(params)
update_attributes(params, :as => as)
else
self.attributes = params
self.valid?
@ -84,11 +85,13 @@ module Devise
# super(params)
# end
#
def update_without_password(params={})
def update_without_password(params, *options)
as = options.first[:as] rescue nil
params.delete(:password)
params.delete(:password_confirmation)
result = update_attributes(params)
result = update_attributes(params, :as => as)
clean_up_passwords
result
end

View File

@ -87,6 +87,13 @@ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
assert user.reload.valid_password?('pass321')
end
test 'should update password with valid current password and :as option' do
user = create_user
assert user.update_with_password(:current_password => '123456',
:password => 'pass321', :password_confirmation => 'pass321', :as => :admin)
assert user.reload.valid_password?('pass321')
end
test 'should add an error to current password when it is invalid' do
user = create_user
assert_not user.update_with_password(:current_password => 'other',
@ -138,6 +145,12 @@ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
user.update_without_password(:email => 'new@example.com')
assert_equal 'new@example.com', user.email
end
test 'should update the user without password with :as option' do
user = create_user
user.update_without_password(:email => 'new@example.com', :as => :admin)
assert_equal 'new@example.com', user.email
end
test 'should not update password without password' do
user = create_user