Advice on using modules with Ruby on Rails
I am using Ruby on Rails 3 and I would know in which case is good to use Modules.
I have a controller including a lot of private methods that I use in this way:
class UsersController < ApplicationController
def update
params[:option1] = get_user_option1
params[:option2] = get_user_option2
if params[:option2]
params[:saving_success] = update_user
end
...
if params[:saving_success]
flash[:notice] = another_method_1
else
flash[:error] = another_method_2
end
end
private
def update_user
if params[:option1] == something
@user.save
end
end
def another_method_1
params[...] = ...
...
end
As you can see, in private methods I have things like ActiveRecords and params methods. I know that in a Module you can not use those ActiveRecords or params methods directly, but you can pass they as arguments like in this example:
# In the controller file
class UsersController < ApplicationController
include Users
def update
params[:option] = "true"
@users = Users.find(1)
Users::Validations.name (@user, params[:option])
...
end
end
# In the module file
module Users
module Validations
def Validations.name(user, param)
user == "Test_name" if param
# Normally the following is not possible:
# @user == "Test_name" if params[:option]
end
end
end
So, what do yo开发者_开发技巧u advice in my case? Is it good to use separate Modules?
Questions of secondary importance (for now...):
- What about performance?
P.S. I: Pay no attention to the simplicity of the examples. They are written just to understand my dilemma about passing ActiveRecords and params methods.
P.S. II: If you need to have some other information, let me know.
Modules have 2 primary purposes :
- Namespacing
- Mixins
Module Namespacing is generally used to organize code better and to facilitate safer and coherent scoping.
But modules find their major use as mixins. Its Ruby's way of providing multiple inheritance. For example, lets say you have methods that need to be accessed across Classes (for instance across different models/controllers etc). Instead of repeating those methods in each class, that don't necessarily apply just to that class, you abstract those methods out into a module and include or extend the module in the appropriate class.
It depends on how tightly coupled a module is with the app directory to decide as to where to store the module. A few patterns in storing modules :
- The /lib directory, if the module does not particularly 'interact' with app/.
- The app/models directory, but may cause confusion with actual ActiveRecord models.
- 37 Signals introduced a pattern of treating them as 'concerns' and storing them in app/concerns.
However, if in case you have a method that you're using just for your Users Controller, it is recommended that you put that code into your User model, because that's where the business logic for the 'User' should be.
By 'ActiveRecords', I'm assuming you mean Model classes (such as User). You CAN access model classes and perform ActiveRecord operations (such as User.find(:all)) on them in modules.
However, as you guessed right, you can't use params, you'll have to pass that as an argument to the module's method.
精彩评论