有关在Ruby on Rails中使用模块的建议 [英] Advice on using modules with Ruby on Rails
问题描述
我正在使用Ruby on Rails 3,我会知道在哪种情况下使用Modules是很好的.
I am using Ruby on Rails 3 and I would know in which case is good to use Modules.
我有一个控制器,其中包含许多以这种方式使用的 private 方法:
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
如您所见,在私有方法中,我有诸如ActiveRecords和params方法之类的东西.我知道在模块中您不能直接使用那些 ActiveRecords 或 params方法,但是您可以像下面的示例一样将它们作为参数传递:
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
那么,对于我来说,您有何建议?使用单独的模块好吗?
次要问题(目前...):
Questions of secondary importance (for now...):
- 那性能呢?
P.S. I:不注意示例的简单性.编写它们的目的只是为了了解我关于传递ActiveRecords和params方法的难题.
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:如果您需要其他信息,请告诉我.
P.S. II: If you need to have some other information, let me know.
推荐答案
模块有2个主要用途:
- 名称间隔
- Mixins
模块命名空间通常用于更好地组织代码并促进更安全和一致的作用域.
Module Namespacing is generally used to organize code better and to facilitate safer and coherent scoping.
但是模块发现它们主要用作mixin.它的Ruby提供多重继承的方式.例如,假设您有一些需要跨类访问的方法(例如,跨不同的模型/控制器等).您不必在每个类中重复这些方法,这些方法不一定只适用于该类,而是将这些方法抽象到模块中,然后在模块中 include 或 extend 合适的班级.
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 :
- /lib目录,如果模块未特别与app/进行交互".
- app/models目录,但可能与实际的ActiveRecord模型混淆.
- 37 Signals引入了一种将其视为关注"并将其存储在应用/关注中.
但是,如果您有一种仅用于用户控制器的方法,建议您将该代码放入用户模型,因为这就是用户"的业务逻辑所在.
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.
通过"ActiveRecords",我假设您指的是Model类(例如User). 您可以在模块中访问模型类并对其执行ActiveRecord操作(例如User.find(:all)).
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.
但是,正如您猜对的那样,您不能使用params,必须将其作为参数传递给模块的方法.
However, as you guessed right, you can't use params, you'll have to pass that as an argument to the module's method.
这篇关于有关在Ruby on Rails中使用模块的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!