Rails 3: Where is the best place to host custom validator classes?

I have seen quite a few topics about where it is best to place your own validation classes (ActiveModel :: EveryValidator extension), but cannot figure out which one is best suited. Some threads and tutorials say they put them in "lib /" and change "/config/application.rb", other threads say that if you just put them in "application / validators", they will be automatically included ... any ideas on this? What is the best practice for this?

The solution "libs /" has a drawback because you need to change "/config/application.rb", but as I understand it, including it in the "application / validators" will include it, although you need to add the directory to the application folder. It is right? Thanks!

+6
source share
1 answer

Yes, when you put your own validator in the "application / validators", it will be automatically enabled. When you use the "lib" or "lib / validators" directory, you need to put this line in the config / application.rb file:

config.autoload_paths += %W(#{config.root}/lib) 

or

 config.autoload_paths += %W(#{config.root}/lib/validators) 
+4
source

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


All Articles