Adding a Directory to the Load Path in Rails

Adding a directory to the load path in Rails?

In the current version of Rails (3.2.8), this has been changed in the application.rb file.

The code is currently commented out as:

  # Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)

Will need to update the autoload_paths value. Attempting to modify the the former load_paths variable causes this error.

/configuration.rb:85:in `method_missing': undefined method `load_paths' for #<Rails::Application::Configuration:0xac670b4> (NoMethodError)

for an example, for each path to add to autoload_paths config, add a line similar to the following:

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

config.autoload_paths accepts an array of paths from which Rails will autoload constants. Default is all directories under app.

http://guides.rubyonrails.org/configuring.html


From commentor (hakunin) below:

If the directory is under app/, you don't need to add it anywhere, it should just work by default (definitely in 3.2.12). Rails has eager_load_paths that acts as autoload_paths in development, and eager load in production. All app/* directories are automatically added there.

How can I add a services directory to the load path in Rails?

First of all, the code under app folder will be loaded without any config.

I think the problem was the folder structure doesn't match with your class definition.

So this config will work:

app/services/foo/test.rb

module Foo
class Test
end
end

My clue is, for example we have app/controllers/api/v1/users_controllers.rb and the class constant will be Api::V1::UsersController, not Controllers::Api::V1::UsersController

Update

Conventionally, we usually use FooServices instead of Foo, it is clearer, for example:

app/services/foo_services/bar_parser.rb

module FooServices
class BarParser
# Do stuff
end
end

So we understand that every class inside foo_services folder is a service which related to Foo

Adding a directory to $LOAD_PATH (Ruby)

I would say go with $:.unshift File.dirname(__FILE__) over the other one, simply because I've seen much more usage of it in code than the $LOAD_PATH one, and it's shorter too!

how can I add current directory to ruby loadpath permanently

Set the RUBYLIB environment variable for your shell to include the current path .. If you want multiple paths to search from, you can separate each path with :.

export RUBYLIB=.

Test:

$ RUBYLIB='.' ruby -e "p $:"

UPDATE: Put the environment variable settings in your shell's initialization script so that it gets set every time you launch your shell.

Add folder to asset pipeline path?

Was able to do it in the application.rb:

require "#{Rails.root}/app/models/settings.rb"
config.assets.paths << "#{Rails.root}/app/themes/#{Settings.theme}/assets/stylesheets"
config.assets.paths << "#{Rails.root}/app/themes/#{Settings.theme}/assets/images"
config.assets.paths << "#{Rails.root}/app/themes/#{Settings.theme}/assets/javascripts"

Way to load folder as module constant in rails app directory

Auto loading

You need to define Services::User::Foo inside app/services/services/user/foo.rb

If you don't want this weird subfolder duplication, you could also move services to app/models/services or lib/services.

You could also leave foo.rb in app/services/user/foo.rb, but it should define User::Foo.

Eager loading

If you don't need any magic with namespaces and class names, it is pretty straightforward :

Dir[Rails.root.join('app/services/**/*.rb')].each{|rb| require rb}

This will eagerly load any Ruby script inside app/services and any subfolder.



Related Topics



Leave a reply



Submit