Refactoring


#1

Although this particular code is part of some goofing around I am
doing in with Rails, I have what I think is some specific ruby
questions. If I should take this to the rails list, please let me
know.

Alos, I am referencing pastie…if there is a better way for me to
have done this, that would be good to know as well.

So the main code is here: http://pastie.caboo.se/64700

So as you can see it is fairly straight forward. It seems to me that
it needs some refactoring.

This is where the problem comes in.

I’m fairly new to ruby and am hoping for some suggestions on how to
refactor this code.

My initial thought was to add another class called Role. Except I’m
not really sure what steps to take to make that happen.

I’m most interested in the thinking behind this process. You
experienced guys seem to just know the answers, but I’d like to know
what led you to whatever decision you made.

Maybe I’m not even asking the right questions here…but I know it
will be an adventure.

Thanks for any insight.

Mike B.


#2

On May 25, 3:27 pm, barjunk removed_email_address@domain.invalid wrote:

So as you can see it is fairly straight forward. It seems to me that
I’m most interested in the thinking behind this process. You
experienced guys seem to just know the answers, but I’d like to know
what led you to whatever decision you made.

Maybe I’m not even asking the right questions here…but I know it
will be an adventure.

Thanks for any insight.

Mike B.

One other thing…the Myuser model does not have a row associated with
it. These records will come from a non-database source.

Mike B.


#3

Mike,

There’s a Rails-Talk list for Ruby on Rails questions. You may find
that helpful for Rails-specific tips on structuring your
controllers. See http://groups.google.com/group/rubyonrails-talk.

Feel free to give me an email off-list or send an email to that list
for the Rails-specific stuff. A few tips for now:

  • If Myuser isn’t coming from the database, you probably don’t
    want to inherit from ActiveRecord
  • I wouldn’t expect your index method to work as written - you’ll
    probably end up with double render errors
    — which leads me to suggest that your first step may be to get
    familiar with Test::Unit or Rspec so you can easily test your code,
    which will help you refactor with confidence. If you need help
    getting started with either of these, let me know.

Here’s a general Ruby tip for you, though:

Instead of this:

@myuser = session[:user] ? session[:user] : Myuser.new

you can accomplish the same with this:

@myuser = session[:user] || Myuser.new

Both forms are fine, IMO, but the second is more idiomatic Ruby.

Hope this helps, and good luck getting started!

David