Some minor refactorings in mailer, confirmable and recoverable. Also some route docs inside controllers.

This commit is contained in:
Carlos A. da Silva 2009-10-08 18:43:29 -03:00
parent 1e2ffc4104
commit 5c210d725f
7 changed files with 42 additions and 15 deletions

View File

@ -1,9 +1,13 @@
class ConfirmationsController < ApplicationController class ConfirmationsController < ApplicationController
before_filter :require_no_authentication before_filter :require_no_authentication
# GET /confirmation/new
#
def new def new
end end
# POST /confirmation
#
def create def create
@confirmation = User.send_confirmation_instructions(params[:confirmation]) @confirmation = User.send_confirmation_instructions(params[:confirmation])
if @confirmation.errors.empty? if @confirmation.errors.empty?
@ -14,6 +18,8 @@ class ConfirmationsController < ApplicationController
end end
end end
# GET /confirmation?perishable_token=abcdef
#
def show def show
@confirmation = User.confirm!(:perishable_token => params[:perishable_token]) @confirmation = User.confirm!(:perishable_token => params[:perishable_token])
if @confirmation.errors.empty? if @confirmation.errors.empty?

View File

@ -1,9 +1,13 @@
class PasswordsController < ApplicationController class PasswordsController < ApplicationController
before_filter :require_no_authentication before_filter :require_no_authentication
# GET /password/new
#
def new def new
end end
# POST /password
#
def create def create
@password = User.send_reset_password_instructions(params[:password]) @password = User.send_reset_password_instructions(params[:password])
if @password.errors.empty? if @password.errors.empty?
@ -14,11 +18,15 @@ class PasswordsController < ApplicationController
end end
end end
# GET /password/edit?perishable_token=abcdef
#
def edit def edit
@password = User.new @password = User.new
@password.perishable_token = params[:perishable_token] @password.perishable_token = params[:perishable_token]
end end
# PUT /password
#
def update def update
@password = User.reset_password!(params[:password]) @password = User.reset_password!(params[:password])
if @password.errors.empty? if @password.errors.empty?

View File

@ -2,13 +2,19 @@ class SessionsController < ApplicationController
before_filter :authenticate!, :except => :new before_filter :authenticate!, :except => :new
before_filter :require_no_authentication, :only => :new before_filter :require_no_authentication, :only => :new
# GET /session/new
#
def new def new
end end
# POST /session
#
def create def create
redirect_to root_path if authenticated? redirect_to root_path if authenticated?
end end
# DELETE /session
#
def destroy def destroy
redirect_to :action => :new if logout redirect_to :action => :new if logout
end end

View File

@ -5,14 +5,14 @@ class Notifier < ::ActionMailer::Base
# is manually requested # is manually requested
# #
def confirmation_instructions(record) def confirmation_instructions(record)
subject I18n.t(:confirmation_instructions, :scope => [:devise, :notifier], :default => 'Confirmation instructions') subject translate(:confirmation_instructions, :default => 'Confirmation instructions')
setup_mail(record) setup_mail(record)
end end
# Deliver reset password instructions when manually requested # Deliver reset password instructions when manually requested
# #
def reset_password_instructions(record) def reset_password_instructions(record)
subject I18n.t(:reset_password_instructions, :scope => [:devise, :notifier], :default => 'Reset password instructions') subject translate(:reset_password_instructions, :default => 'Reset password instructions')
setup_mail(record) setup_mail(record)
end end
@ -25,4 +25,8 @@ class Notifier < ::ActionMailer::Base
content_type 'text/html' content_type 'text/html'
body record.class.name.downcase.to_sym => record body record.class.name.downcase.to_sym => record
end end
def translate(key, options={})
I18n.t(key, {:scope => [:devise, :notifier]}.merge(options))
end
end end

View File

@ -52,12 +52,8 @@ module Devise
# Options must contain the user email # Options must contain the user email
# #
def send_confirmation_instructions(options={}) def send_confirmation_instructions(options={})
confirmable = find_or_initialize_by_email(options[:email]) confirmable = find_or_initialize_with_error_by_email(options[:email])
unless confirmable.new_record? confirmable.send_confirmation_instructions unless confirmable.new_record?
confirmable.send_confirmation_instructions
else
confirmable.errors.add(:email, :not_found, :default => 'not found')
end
confirmable confirmable
end end

View File

@ -29,7 +29,18 @@ module Devise
def find_or_initialize_with_error_by_perishable_token(perishable_token) def find_or_initialize_with_error_by_perishable_token(perishable_token)
perishable = find_or_initialize_by_perishable_token(perishable_token) perishable = find_or_initialize_by_perishable_token(perishable_token)
if perishable.new_record? if perishable.new_record?
perishable.errors.add(:perishable_token, :invalid, :default => "invalid confirmation") perishable.errors.add(:perishable_token, :invalid, :default => 'invalid confirmation')
end
perishable
end
# Attempt to find a user by it's email. If not user is found, returns a
# new user with an email not found error.
#
def find_or_initialize_with_error_by_email(email)
perishable = find_or_initialize_by_email(email)
if perishable.new_record?
perishable.errors.add(:email, :not_found, :default => 'not found')
end end
perishable perishable
end end

View File

@ -37,12 +37,8 @@ module Devise
# Options must contain the user email # Options must contain the user email
# #
def send_reset_password_instructions(options={}) def send_reset_password_instructions(options={})
recoverable = find_or_initialize_by_email(options[:email]) recoverable = find_or_initialize_with_error_by_email(options[:email])
unless recoverable.new_record? recoverable.send_reset_password_instructions unless recoverable.new_record?
recoverable.send_reset_password_instructions
else
recoverable.errors.add(:email, :not_found, :default => 'not found')
end
recoverable recoverable
end end