Recommendations for Using Modules with Ruby on Rails

I am using Ruby on Rails 3, and I would know in which case it is useful to use modules.

I have a controller that includes many private methods, which I use as follows:

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 the ActiveRecords and params methods. I know that in a module you cannot use those ActiveRecords or params methods directly, but you can pass them as arguments, as 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 you recommend in my case? Is it good to use separate modules?


Questions of secondary importance (at the moment ...):

  • What about performance?

PS I: Ignore the simplicity of the examples. They are written only to understand my dilemma about passing ActiveRecords and params methods.

PS II: If you need other information, let me know.

+4
source share
1 answer

Modules have 2 main goals:

  • Namespaces
  • Impurities

Namespacing is typically used to streamline code more efficiently and provide more secure and consistent coverage.

But modules find their main application as mixins. Its a Ruby way to provide multiple inheritance. For example, let's say you have methods that you need to access through classes (for example, for different models / controllers, etc.). Instead of repeating these methods in each class, which do not necessarily apply only to this class, you abstract these methods into a module and include or extend the module into the corresponding class.

It depends on how tightly the module is connected to the application directory to decide how to store the module. Several templates when storing modules:

  • The / lib directory, if the module does not particularly "interact" with the / application.
  • Application / model catalog, but can lead to confusion with actual ActiveRecord models.
  • 37 Signals entered a pattern , treating them as β€œproblems” and storing them in the application / problems.

However, if you have a method that you use only for your User Controller, it is recommended to put this code in the user model, because this should be the business logic for "User".

By 'ActiveRecords', I assume that you mean model classes (e.g. User). You can access model classes and perform ActiveRecord operations (such as User.find (: all)) on them in modules.

However, as you already guessed, you cannot use params, you will have to pass this as an argument to the modular method.

+9
source

Source: https://habr.com/ru/post/1338826/


All Articles