Platinum Partner

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.
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}