After six month of development and a first release we decide to
refactor our application. Since it was my first Ruby/Rails application
there are a lot of horrible things inside it.
Next time, use TDD and refactoring as you go. You would have shipped
sooner, with fewer bugs.
And now? Some general hints?
If you do not have unit tests (or “functional” tests, which are really
tests with a suggestive name), then do this:
- start a new Rails project
- pick the most important single tiny feature in the old app
- use TDD and refactoring to implement it int the new app
- use the old app as a cheat sheet, but refrain from copying code out
- repeat with each feature until done.
You will have a much cleaner application, with an exemplary design.
I start with 2 simple questions:
1 - Where I put some common methods? Inside application.rb
(controller) is ugly…
Refactor low hanging fruit. If your common methods have redundant lines
only talk about models, move them to models. Eventually your common
DRY up as if by themselves. Find the easiest possible refactors first -
attempt the hard ones while a bunch of easy ones are in the way.
hard ones will take care of themselves.
2 - In order to make skinny controller I write a lot of class method
inside the model, maybe too much? Is this a common practice?
Yes - your models should fill up with lots of tiny methods, all