Refactor a bit to avoid two model attributes lookup

Tidy up tests a bit.
This commit is contained in:
Carlos Antonio da Silva 2012-12-04 17:37:12 -02:00
parent e80e0c9a89
commit 2261005ed5
4 changed files with 10 additions and 19 deletions

View File

@ -172,11 +172,7 @@ module Devise
end end
def apply_to_attribute_or_variable(attr, method) def apply_to_attribute_or_variable(attr, method)
if self[attr] (self[attr] || send(attr)).try(method)
self[attr].try(method)
else
send(attr).try(method)
end
end end
module ClassMethods module ClassMethods
@ -207,7 +203,7 @@ module Devise
# it may be wrapped as well. For instance, database authenticatable # it may be wrapped as well. For instance, database authenticatable
# provides a `find_for_database_authentication` that wraps a call to # provides a `find_for_database_authentication` that wraps a call to
# this method. This allows you to customize both database authenticatable # this method. This allows you to customize both database authenticatable
# or the whole authenticate stack by customize `find_for_authentication.` # or the whole authenticate stack by customize `find_for_authentication.`
# #
# Overwrite to add customized conditions, create a join, or maybe use a # Overwrite to add customized conditions, create a join, or maybe use a
# namedscope to filter records while authenticating. # namedscope to filter records while authenticating.

View File

@ -14,18 +14,18 @@ class DatabaseAuthenticatableTest < ActiveSupport::TestCase
end end
test 'should downcase case insensitive keys that refer to virtual attributes when saving' do test 'should downcase case insensitive keys that refer to virtual attributes when saving' do
email = 'Foo@Bar1.com' email = 'Foo@Bar1.com'
confirmation = 'Foo@Bar1.com' confirmation = 'Foo@Bar1.com'
pw = '12345678' attributes = valid_attributes(:email => email, :email_confirmation => confirmation)
user = UserWithVirtualAttributes.new(
:email => email,
:email_confirmation => confirmation,
:password => pw,
:password_confirmation => pw)
assert_nothing_raised "ActiveRecord::RecordInvalid" do user = UserWithVirtualAttributes.new(attributes)
assert_equal confirmation, user.email_confirmation
assert_nothing_raised ActiveRecord::RecordInvalid do
user.save! user.save!
end end
assert_equal confirmation.downcase, user.email_confirmation
end end
test 'should remove whitespace from strip whitespace keys when saving' do test 'should remove whitespace from strip whitespace keys when saving' do

View File

@ -29,10 +29,6 @@ class ActiveSupport::TestCase
:password_confirmation => '12345678' }.update(attributes) :password_confirmation => '12345678' }.update(attributes)
end end
def new_user_with_class(klass=User, attributes={})
klass.new(valid_attributes(attributes))
end
def new_user(attributes={}) def new_user(attributes={})
User.new(valid_attributes(attributes)) User.new(valid_attributes(attributes))
end end

View File

@ -25,4 +25,3 @@ end
class Inheritable < Admin class Inheritable < Admin
end end