Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have seen quite a few threads about where the best place is to put your custom validation classes (extending ActiveModel::EachValidator), but can't figure out which one is best practice. Some threads and tutorials say put them in 'lib/' and modify '/config/application.rb', other threads say if you just put them in 'app/validators' they will be automatically included... any ideas on this? What is the best practice for this?

The 'libs/' solution has a downside because you have to modify '/config/application.rb', but as far as I understand, placing it in 'app/validators' will include it, although you have to add a directory to the app folder. Is this right? Thanks!

like image 558
botbot Avatar asked Feb 13 '12 23:02

botbot


1 Answers

Yes, when you place your custom validator into "app/validators", it will be automatically included. When you use directory "lib" or "lib/validators", you will have to place this line into config/application.rb file:

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

or

config.autoload_paths += %W(#{config.root}/lib/validators)
like image 125
bender Avatar answered Nov 15 '22 07:11

bender