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

View File

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

View File

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

View File

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

View File

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

View File

@ -29,7 +29,18 @@ module Devise
def find_or_initialize_with_error_by_perishable_token(perishable_token)
perishable = find_or_initialize_by_perishable_token(perishable_token)
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
perishable
end

View File

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