I was wondering if there is a way to easily access methods/classes from the lib folder in migrations? I found a way to require them with
require File.expand_path("../../lib/my_library")
I don't really like this and I was wondering if there is a more common way to include those libraries required by the migration?
rake db:migrate load all rails environment, so you can just add lib in autoload in application.rb file like this and it should work.
config.autoload_paths += Dir["#{config.root}/lib/**/"]
Related
I want to include some additional non-rails classes in my Rails App. I created a lib directory under app/ and a lib directory under test/ I am using minitest.
How do I make it so rails test also runs the tests under the new test/lib directory? How do I make it so my app includes app/lib in the autoload mechanism?
In the application.rb file add the following configuration which will load files from the app/lib folder
config.autoload_paths += "#{Rails.root}/app/lib/"
Is there a environment variable and some command-line options to check my rails application's directories that are set automatically or configured somewhere so that rails know where to load libraries etc.?
As a compare, ruby has a command line option (-I):
ruby -I path:another/path:/usr/lib rubyfile.rb arg1 arg2
such that users can set the directories where libs can be looked up by Ruby.
Similarly, here I am asking if for rails there is a similar way to check the loading paths for Rails applications? Or if there is certain variable set somewhere?
Highlight: I am using Rails version 4.2.0. The config/application.rb sample code doesn't appear to contain config.autoload_paths, not even in comments, which used to be the one for Rails 3 and Rails 4.1.x.
And, how would the Rails application know where to look up for Gems, etc.?
Thanks!
See this file:
config/application.rb
Specifically, you add this setting:
config.autoload_paths
You can add to the autoload paths like this:
config.autoload_paths << Rails.root.join('mydir')
The config.autoload_paths accepts an array of paths from which Rails will autoload constants.
The default is all directories under ./app, such as controllers, models, etc.
The default does not autoload from the typical top-level ./lib directory. If you want to autoload from lib:
config.autoload_paths << Rails.root.join('lib')
See the Rails Guides for Configuring
Rails knows where to look for gems a variety of ways: typically your environment GEM_PATH or the bundler gem bundle exec command. For example, I personally bundle my Rails gems into ./vendor/bundle/.
At the moment I'm having to restart the server if I change files in the lib/ and app/helpers/ directories. Is there a way to configure either Rails or Webrick such that it hotswaps the files in those directories as well?
In your config/application.rb:
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/helpers)
There should be a commented line which explains what autoload_paths is for.
In my project, I have written few classes under lib folder but rails is not detecting those classes in production environment. I get the uninitalized Constant error.
I use Apache in the production environment and rails script/server in the development environment.
Is anything wrong with RAILS_ROOT environment? Can anyone suggest how to overcome this problem?
I am not sure about Rails, but you achieve that in Ruby by this: (it will work in rails too, but rails must be having some elegant way)
require File.join(File.dirname(__FILE__), "lib",'your_module_name')
include your_module_name
Try this in config/application.rb (I assume you have rails3)
config.load_paths += %W( #{config.root}/lib )
Update: Rails - why would a model inside RAILS_ROOT/lib not be available in production mode?
Ensure that the name of your file matches the name of the class or module defined in it, accounting for any directories.
ie:
lib/my_new_class.rb
class MyNewClass
end
Or if you have a directory hierarchy:
lib/my_files/my_module.rb
module MyFiles
module MyModule
end
end
I've added a few modules and dropped them in my /lib directory and I think the lib directory is loaded magically by Rails (unless I loaded the lib directory somewhere early in my project and forgot about it). However, when I run unit tests that require my additional modules, they are not loaded.
Should the lib directory be loaded automatically when running tests, or is there an elegant way to do so for testing? I had hoped that the rake scripts + Test::Unit would've loaded up my Rails environment exactly, but this doesn't seem to be the case. I'm left with doing adding something like this to test_helper.rb:
require File.expand_path(File.dirname(__FILE__) + "/../lib/foo")
I'm running my tests with the standard rake scripts like:
rake test
rake test:units
rake test:functionals
Your lib directory is not automatically loaded by rails. Loading occurs through ActiveSupport::Dependencies overriding const_missing. When you use a constant for the first time, if it is undefined, Rails attempts to find it in the lib directory (and other places in the load path). To accomplish this, it uses a naming scheme where something called SomeClass is expected to be in some_class.rb. Rails in test mode uses the same mechanism. Check your config/environments/test.rb and config/environments/development.rb to see if you do something funny with requires. In short, check your naming scheme.