0

What is best way to refactor in Ruby on Rails. I have a project where i would like to refactor several of my objects - extra properties and renaming of existing properties. How do i do this this the easiest way?? I created the different objects with scaffolding "rails generate scaffold foo name:string...." I do start from scratch or is there some cool ruby command, like "rails refactor foo name:string"..

4 Answers 4

2

As far as I am aware, there is no automated way to do this. The key things as far as I'm concerned is to make sure that your test coverage is good in the relevant area (and preferably across your whole app) before you start.

Once that's done, I've tended to apply fairly brute force tactics to do that actual refactor:

  • Run all tests.
  • Searching for relevant file names and strings within files, making a value judgement as to whether you're changing the right thing (don't just do search/replace).
  • Refactor your tests along side the code itself.
  • Run all tests again.
  • Repeat until it works.

The reason that refactoring isn't very automated in Ruby (in my view at least) is because the language is so dynamic, so it's very hard for any automated tools to make sure they've covered all the bases.

Sign up to request clarification or add additional context in comments.

Comments

1

Refactoring is based on human perception. Sure, omission of some redundant things could be figured out by a computer, but otherwise when you refactor you're essentially improving your own hand-written code. Unless a computer has the intelligence and the ability to discern code like a human, then no, it's not possible.

Perhaps you're referring to something else?

2 Comments

I think the OP want's some thing like this: eclipse.org/articles/… for Rails.
I am looking for the practical way of doing it/most efficient way of doing it.
1

You should learn some Rails best practices and use them to refactor your code

Comments

0

Firstly a good knowledge of your application and then start in a procedure and serialize it.

  • Use helpers to optimize.Methods should be small enough and minimize the possiblity of repetition of codes.
  • Use application files which helps a lot
  • You can also write methods in private to reduce the lines in methods.
  • Alwayz be sure where refactoring is done and its dependencies..

Thanx

.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.