Show Menu
Cheatography

Rails Refactoring Recipes Cheat Sheet by

Rails refactoring techniques from Fearless Refactoring: Rails Controllers

Explicitly render views with locals

1. Go to the view, replace all
@ivar
with
var

2. Do the same in all partials that are called from the view and always pass the params to partials explicitly with
render "­pro­duc­ts/­for­m", {product: product}

3. At the end of the action add an explicit render call with a full path and the locals:
render "­pro­duc­ts/­new­", :locals => {product: product}

4. Find all contro­llers that were using the views/­par­tials that you changed and apply the same.

Extract render­/re­direct methods

1. Identify all
render
and
redirect
calls in your contro­ller's actions.
2. Extract a private method for each
render
and
redirect
call you found with descri­ptive name that shows your intention.
3. Find and remove any duplicated methods you might created during this refact­oring in the same contro­ller.

Extract a Single Action Controller class

1. A new route declar­ation above the previous (first wins)
2. Create an empty controller which inherits from the previous
3. Copy the action content to the new controller
4. Remove the action from the previous controller
5. Copy the filter­s/m­ethods that are used by the action to the new controller
6. Make the new controller inherit from the
Applic­ati­onC­ont­roller

7. Change routes to add
except­: [­:fo­o_a­ction]

Extract routing constraint

1. Go to the contro­ller, duplicate existing action method under different name.
2. Create a constraint that can recognize which action should be called. Put it in
routes.rb

3. Duplicate the relevant routing rule in
routes.rb

4. Protect first routing rule with the constr­aint.
5. Change the second routing rule so it delegates to the new controller action. If necessary, protect it with similar constr­aint.
6. Remove the irrelevant code from controller actions. Make them do only one thing.
7. (Optio­nally) Move the constr­aint(s) to separate file(s).
 

Extract a repository object

1. Create a class called
Produc­tsR­epo­sitory
inside the same file as the controller
2. Find all calls to typical
Produc­t.f­ind­/al­l/n­ew/­sav­e/c­reate
methods in the controller
3. Create those methods in the repo object
4. Add a private method, called repo in the controller (possibly in the
Applic­ati­onC­ont­roller
) where you instan­tiate the repo.
5. Move the repository class to
app/repos/

Extract a service object using the Simple­Del­egator

1. Move the action definition into new class and inherit from
Simple­Del­egator
.
2. Step by step bring back controller respon­sib­ilities into the contro­ller.
3. Remove inheriting from
Simple­Del­egator
.
4. (Optional) Use exceptions for control flow in unhappy paths.

Extract an adapter object

1. Extract external library code to private methods of your controller
2. Parame­trize these methods - remove explicit request / params / session statements
3. Pack return values from external lib calls into simple data struct­ures.
4. Create an adapter class inside the same file as the controller
5. Move newly created controller methods to adapter (one by one), replace these method calls with calls to adapter object
6. Pack exceptions raised by an external library to your exceptions
7. Move your adapter to another file (ex. app/ad­apt­ers­/yo­ur_­ada­pte­r.rb)
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          vim-rails Cheat Sheet
            Ruby on Rails Cheat Sheet by Dave Child and addedbytes.com
          FactoryBot Cheat Sheet