DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Quick And Dirty Rails Code
This is one of the methods in a small CMS I'm writing, I thought I'd post here since it's just a mess. Take a look at the obfuscation:
def updatesettings
# This method is probably one of the most messiest things I have ever written.
# It's a piece of garbage and an example of how NOT to code something like this.
@settings = User.find(params[:id])
@settings.attributes = params[:settings]
if request.post?
oldpassword = @settings.password
@settings.login = params[:settings][:login]
@settings.email = params[:settings][:email]
if params[:password][:newpassword1] != "" || params[:password][:newpassword2] != ""
if params[:password][:newpassword1] == params[:password][:newpassword2]
@settings.password = Digest::SHA1.hexdigest("FAKE CODE HERE--#{params[:password][:newpassword1]}--")
else
flash[:sucess] = "Your new passwords don't match."
redirect = "settings"
end
end
@settings.save
if params[:id] != nil
if redirect == nil && "#{params[:id]}" != "#{session[:user_id]}"
flash[:sucess] = "User has been sucessfully updated."
redirect_to :controller => "users"
else
redirect_to :action => "index", :id => params[:id]
end
else
flash[:sucess] = "Your settings have been updated."
redirect_to :action => "users"
end
end
end
edit: I fixed it up so all of the code is in the model now.
settings = @settings.updatesettings(params[:id], params[:settings][:login], params[:settings][:email], params[:password][:newpassword1], params[:password][:newpassword2])
I'd say that's a little bit better.





Comments
fdfdfd dfdf replied on Fri, 2009/05/22 - 3:55am
fgfgf dfdfd replied on Wed, 2009/05/20 - 12:08am
drage sheter replied on Tue, 2009/04/28 - 8:36am
Snippets Manager replied on Fri, 2006/10/20 - 2:36am
Snippets Manager replied on Tue, 2006/08/15 - 7:37pm
Snippets Manager replied on Fri, 2006/10/20 - 8:29am