refactoring

B

barjunk

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.
 
B

barjunk

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.


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.
 
D

David L Altenburg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top