Skip to content

Commit

Permalink
Add before_save :downcase_email filter
Browse files Browse the repository at this point in the history
  • Loading branch information
maprihoda committed Dec 19, 2011
1 parent ff558e4 commit d01ee6c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
8 changes: 6 additions & 2 deletions app/models/extensions/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ module Authentication
validates_confirmation_of :password, :allow_blank => true
validates_length_of :password, :within => 6..40, :allow_blank => true

before_save :prepare_password
before_save :prepare_password, :downcase_email
before_create { generate_token(:remember_token) }
before_create { generate_token(:confirmation_token) unless authenticated_with_omniauth? }
after_create { send_confirmation_instructions_and_save unless authenticated_with_omniauth? }
end

module ClassMethods
def authenticate(email, pass)
user = where('email = ? AND confirmed_at IS NOT NULL', email).first
user = where('email = ? AND confirmed_at IS NOT NULL', email.downcase).first
return user if user && user.password_hash == user.encrypt_password(pass)
end

Expand Down Expand Up @@ -122,6 +122,10 @@ def prepare_password
end
end

def downcase_email
email.downcase! if email.present?
end

def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/controller_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ActionController::Metal
def self.controller_name
# @controller_name ||= self.name.demodulize.sub(/Controller$/, '').underscore
@controller_name ||= self.name.sub(/Controller$/, '').underscore
end
end

8 changes: 7 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

context 'authentication' do
it 'should return the user object if the credentials match' do
User.authenticate(user.email, user.password).should == user
User.authenticate(user.email.upcase, user.password).should == user
end

it 'should return nil if the credentials do not match' do
Expand Down Expand Up @@ -104,5 +104,11 @@
end
end

it 'downcases email on user#save' do
user.email = 'JOE@EXAMPLE.COM'
user.save
user.reload
user.email.should == 'joe@example.com'
end
end

2 changes: 1 addition & 1 deletion spec/requests/rememberable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
current_path.should == dashboard_path

# simulate closing the browser
# this deletes the session cookie and any expired cookies so our remember_me cookie stays untouched
# this deletes the session cookie and any expired cookies, but our remember_me cookie stays untouched
# (see the show_me_the_cookies gem)
expire_cookies

Expand Down

0 comments on commit d01ee6c

Please sign in to comment.