From 2261005ed506df251073a6b1e4eee7f29cdfe6f0 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Tue, 4 Dec 2012 17:37:12 -0200 Subject: [PATCH] Refactor a bit to avoid two model attributes lookup Tidy up tests a bit. --- lib/devise/models/authenticatable.rb | 8 ++------ test/models/database_authenticatable_test.rb | 16 ++++++++-------- test/support/helpers.rb | 4 ---- test/test_models.rb | 1 - 4 files changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/devise/models/authenticatable.rb b/lib/devise/models/authenticatable.rb index 1e676a01..6600f526 100644 --- a/lib/devise/models/authenticatable.rb +++ b/lib/devise/models/authenticatable.rb @@ -172,11 +172,7 @@ module Devise end def apply_to_attribute_or_variable(attr, method) - if self[attr] - self[attr].try(method) - else - send(attr).try(method) - end + (self[attr] || send(attr)).try(method) end module ClassMethods @@ -207,7 +203,7 @@ module Devise # it may be wrapped as well. For instance, database authenticatable # provides a `find_for_database_authentication` that wraps a call to # 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 # namedscope to filter records while authenticating. diff --git a/test/models/database_authenticatable_test.rb b/test/models/database_authenticatable_test.rb index 0986be07..7693b828 100644 --- a/test/models/database_authenticatable_test.rb +++ b/test/models/database_authenticatable_test.rb @@ -14,18 +14,18 @@ class DatabaseAuthenticatableTest < ActiveSupport::TestCase end 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' - pw = '12345678' - user = UserWithVirtualAttributes.new( - :email => email, - :email_confirmation => confirmation, - :password => pw, - :password_confirmation => pw) + attributes = valid_attributes(:email => email, :email_confirmation => confirmation) - assert_nothing_raised "ActiveRecord::RecordInvalid" do + user = UserWithVirtualAttributes.new(attributes) + assert_equal confirmation, user.email_confirmation + + assert_nothing_raised ActiveRecord::RecordInvalid do user.save! end + + assert_equal confirmation.downcase, user.email_confirmation end test 'should remove whitespace from strip whitespace keys when saving' do diff --git a/test/support/helpers.rb b/test/support/helpers.rb index 355001d9..a2554485 100644 --- a/test/support/helpers.rb +++ b/test/support/helpers.rb @@ -29,10 +29,6 @@ class ActiveSupport::TestCase :password_confirmation => '12345678' }.update(attributes) end - def new_user_with_class(klass=User, attributes={}) - klass.new(valid_attributes(attributes)) - end - def new_user(attributes={}) User.new(valid_attributes(attributes)) end diff --git a/test/test_models.rb b/test/test_models.rb index b17d3773..fb65d53c 100644 --- a/test/test_models.rb +++ b/test/test_models.rb @@ -25,4 +25,3 @@ end class Inheritable < Admin end -