[POST UPDATE ON 19/07/2010 - 15:45]
Continuing our analysis of the Rails Best Practices today we’ll see two other tricks to make more readable method of the controller.
1. Methods within model
Suppose we have the classic user registry defined in such a way that an admin user can enable or disable other users.
The deactivate method inside user Controller may be defined as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class UserController < ApplicationController |
What is clear is that this method has a lot of business logic inside. We see then how to define a method in the User Model to implement business logic so we can streamline this method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class User < ActiveRecord:Base |
This rewrite makes the deactivate method within Controller more readable and more easily testable.
2. Factory Method
In a similar way to what has been done for the deactivate method, sometimes can be handy while writing a create method, to define a Factory Method in the model that contains the logic of creation / initialization of the object and then call it from the controller.
As usual, let’s see an example.
Imagine you are developing a new management system where a news must be associated with the user who created it and it must also be visible only for a week.
A solution, without the Factory Method is the following:
1 2 3 4 5 6 7 8 9 10 | class PostController < ApplicationController |
Let us now see how to define a proper method (the Factory Method) of creating the news in the model so, once again, we simplify the controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Post < ActiveRecord:Base |
Once again we managed to write a better code in the Controller by moving the logic in the Model.
This post concludes the first part of this series when I tried to illustrate some best practices that allow to simplify the controller and move the business logic in the Model.
From the next post I will start to illustrate some Best Practices applicable to the Models.