Forum: Ruby on Rails Code refactoring is the key

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
37f7b5a79147b25dab424948e9d96fc7?d=identicon&s=25 vlain (Guest)
on 2008-12-30 17:39
(Received via mailing list)
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.

And now? Some general hints?

I start with 2 simple questions:
1 - Where I put some common methods? Inside application.rb
(controller) is ugly...
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?
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-12-30 17:49
(Received via mailing list)
On Tue, Dec 30, 2008 at 11:39 AM, vlain <rudi.verago@gmail.com> wrote:

> 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?
>

I'll chime in with the obvious
I would start by making sure that the application passes all of your
tests.
If you have have not done this yet, be prepared for a lot of work.  Then
be
prepared for more work as you write tests to cover everything you've
written.

--wpd
6fabb84e727dee98d1277df56458cbac?d=identicon&s=25 Andrew Bloom (Guest)
on 2008-12-30 17:51
(Received via mailing list)
You can break up common methods and place them in modules. Then simply
"include" the module in ApplicationController (or any other set or sub
set of controllers as needed).
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2008-12-30 19:13
(Received via mailing list)
vlain wrote:
> 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
much
sooner, with fewer bugs.

> And now? Some general hints?

If you do not have unit tests (or "functional" tests, which are really
just unit
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
that
only talk about models, move them to models. Eventually your common
methods will
DRY up as if by themselves. Find the easiest possible refactors first -
never
attempt the hard ones while a bunch of easy ones are in the way.
Eventually the
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
apparently useless.

--
   Phlip
This topic is locked and can not be replied to.