I am using Ruby on Rails 3.2.9. A way to solve "Trouble on extending Rails in a sub-directory of the 'lib' directory" related to Custom Validators was to state the following code (note the directory where the file is located)
# lib/extension/rails/custom_validator.rb
class CustomValidator < ActiveModel::EachValidator
# ...
end
and to put the config.autoload_paths += %W(#{config.root}/lib/extension/rails)
statement in the config/application.rb
file. This way the CustomValidator
is correctly loaded when starting the server and my lib/
directory is organized as I'd expect.
However, I would like to know if that is a "proper" / "right" way to make these kind of things in RoR. In other words, is there a better approach to accomplish the same?
Note: In order to organize code in files I am thinking to namespace classes as-like the following:
# lib/extension/rails/custom_validator.rb
module Extension
module Rails
class CustomValidator < ActiveModel::EachValidator
# ...
end
end
end
But, by using the above code, it seems that there isn't an easy way to make the CustomValidator
to work, even if I state config.autoload_paths += %W(#{config.root}/lib)
in the config/application.rb
file.