Forum: Ruby on Rails Restful authentication refactoring

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bd0ecd69dba2aab1a0261ee46e090139?d=identicon&s=25 Nicholas Wieland (Guest)
on 2008-12-29 00:15
(Received via mailing list)
Hi guys, this is more an RFC then a request for help ...
this is the code generated by restful authentication:

   def create
     @user =[:user])
     @user.register! if @user && @user.valid?
     success = @user && @user.valid?
     if success && @user.errors.empty?
       flash[:notice] = "blabla"
       flash[:error]  = "blabla"
       render :action => 'new'

It seems incredibly redundant to me ...
This is how I refactored:

   def create
     @user = params[ :user ] )
     if @user && @user.valid?
       redirect_back_or_default( '/' )
       flash[ :notice ] = "blabla"
       flash[ :error ]  = "blabla"
       render :action => 'new'

I can't understand why it checks 3 times if the object is valid, and
even less that weird "state" variable it sets.
I don't see where it saves, but from my understanding of AASM the
object is saved when the state change happens, probably using
register! is a good idea.
Maybe technoweenie is reading and can enlight me about the code
duplication, if it's really needed, and people with more experience
with restful_authentication can give me a suggestion.
I imported specs and all tests pass, so everything should be fine.

059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2008-12-29 11:02
I also noticed that "strange" redundancy, and refactored it the same way
you did.
This topic is locked and can not be replied to.