No such file to load, Model/Lib naming conflict? - ruby-on-rails

I'm working on a Rails application. I have a Module called Animals. Inside this Module is a Class with the same name as one of my Models (Dog).
show_animal action:
def show_animal
require 'Animals/Bear.rb' #Works
require 'Animals/Dog.rb' #Fails
end
So the first require definitely works, the seconds fails.
MissingSourceFile (no such file to load -- Animals/Dog.rb):
I noticed that Dog.rb is the same file name as one of my models, is that what's causing this? I'm using Webrick.

Try using the full path:
require File.join(RAILS_ROOT, 'lib', 'Animals', 'Dog.rb')

Related

Rails autoloader not loading module

I have the following structure in my /lib folder
/lib
/example
/foo
bar.rb
foo.rb
foo.rb
module Example
module Foo
def self.some_methods
end
end
end
bar.rb
module Example
module Foo
class Bar
...
end
end
end
I then have in application.rb
config.autoload_paths << Rails.root.join('lib')
and in an initializer
...
Example::Foo.some_methods
...
I'm running into problems when trying to run my test suite in that it seems to be failing when setting up the Rails environment. It's complaining that some_methods does not exist on Example::Foo. I can boot a local development server up without any errors, however.
Digging into the problem using a breakpoint, the Example::Foo module is defined at the point of failure, but it's empty. I am assuming that the autoloader is loading bar.rb first (I can access that class in the initializer no problems)? It seems to be the case that Rails doesn't try to load foo.rb as Example::Foo has already been defined by bar.rb?
What's happening? If I require foo.rb in the initializer it works, but it feels like a bad solution. Also, any idea why this problem would only surface when running rake spec?
The whole point of the autoloader is that it loads missing constants by turning a module/class hierarchy into a path. As you've defined your constant in two files, it will never automatically load the second file.
You need to manually require your foo.rb.

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

In my Rails project, I want to add services directory in app folder and include some service objects.
So let's say I want to add app/services/foo/test.rb which looks like:
module Services
module Foo
class Test
end
end
end
In my config/application.rb I added:
config.paths.add File.join('app', 'services'), glob: File.join('**', '*.rb')
config.autoload_paths += Dir[Rails.root.join('app', 'services', '*')]
However when I try to load the files in console it doesn't work:
⇒ rails c
Loading development environment (Rails 4.1.4)
[1] pry(main)> Services::Foo::Test
NameError: uninitialized constant Services
Any help how can I solve this issue?
After add new dir, reload spring
spring stop
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
My problem was because of Rails naming conventions, I suppose. I just renamed class to not use module Services and it worked.

Rails 3, app/lib not loaded with rspec

I have a controller "A" which requires a file in a subdirectory of app/lib (ex: app/lib/a_folder/my_class.rb).
So I did something like this :
require 'a_folder/my_class'
class AController < ApplicationController
# Some stuff using MyClass
end
It works well when I use the application, but doesn't work when I launch RSpec.
This is how my RSpec file looks:
require 'spec_helper'
require 'lib/import_functions' # Rails.root/spec/lib/import_functions.rb
RSpec.describe AController, type: controller do
# My tests routines
end
When I start rspec it tells me the require in the controller file doesn't found the file (`require': cannot load such file), while it works well when I start the app.
I've added a puts $LOAD_PATH just before the require and it appears that Rails.root/app/lib is not present.
I use Rails 3.2 and rspec-rails 3.2.
Does anyone have any idea why it happens and how to fix it please ?
Thank you for your future answers.
lib files are not auto loaded. You can put the following configuration in your application.rb, it has a problem also it will load all files under lib directory.
config.autoload_paths += "#{Rails.root}/lib/"
Or you can load your lib files in your RSpec as following code
require_relative "../../lib/a_folder/my_class.rb"
or
require 'lib/a_folder/my_class.rb'

How to require some lib files from anywhere

I'll explain my situation.
Here is my file tree in my rails application :
lib/my_module.rb
require 'my_module/my_file'
module My_module
end
lib/my_module/my_file.rb
class Tweetag::Collector
(...)
end
I've made a ruby script that I've put in config/jobs/
I really don't understand how I am supposed to require the file my_file.rb in this file.
require '../../my_module/my_file.rb'
It gives me `require': cannot load such file
Same error with just require 'my_module' which is what I do in my controllers...
Someone here to explain to me ? Thanks a lot
You can autoinclude everything under the lib folderand avoid these problems:
Type this your file in config/application.rb
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
If you want to require only a specific file then,
do something relative to Rails root like this
for example: --
lib/plan.rb
module Plan
...some code...
end
and if you want to require it only in some model, say app/models/user.rb
do in user model
require "#{Rails.root}/lib/plan"
class User < ActiveRecord::Base
include Plan
end
if you want it to be available everywhere
one solution is given by #VecchiaSpugna
or you can create a ruby file in config/initializers folder
and require all file over there one by one
OR
try this
require '../../my_module/my_file'
instead of
require '../../my_module/my_file.rb'
You don't need to specify extension for a file in require.
I think there are two solutions.
1) Add the lib path to the search path.
In ruby:
$:.unshift('../../my_module/lib')
Then you can require 'my_module.rb'
I think Vecchia Spugna answer is the rails-version of my ruby-answer. (I'm not familiar with rails).
2) Another solution:
In your lib/my_module.rb you require my_file. This file is located relative to your my_module.rb? Then use require_relative:
require_relative './my_module/my_file'
Just chiming in because it took me forever to figure this out because very little solutions worked.
• I had to use plain old require. I put it in the config/application.rb file.
patching_file_path = File.expand_path("./lib", Dir.pwd)
Dir[patching_file_path+'/*.rb'].each {|file| require file }
• I also put a temporary puts "I'm Working! in the file I'm trying to require so I can check the console to see if it's actually loading.
• Also, if you're using spring loader, before you start your console you should do bin/spring stop in your terminal before you start your rails console. Otherwise, it won't load new files.
Similar to require_relative,
# inside lib/my_module.rb
require File.expand_path('./my_module/my_file', File.dirname(__FILE__))
This expands the path of current file directory and add the relative file path to be required.

Where is the best place to extend the functionality of built in classes in Rails?

I have a few methods I am adding to Ruby's Array class for my Rails application. Where is the best place to put them?
Right now I have them in config/environment.rb.
config/environment.rb isn't really the best place, since you can run into serious load ordering-problems if try to extend classes that haven't been resolved at the time environment.rb is executed.
Better to put a file into config/initializers. Any script placed there will be executed after the rails runtime is loaded.
What you could do is to create a file lib/my_extensions.rb
module MyExtensions
end
then in lib/my_extensions/array.rb :
module MyExtensions::Array
def join_with_commas
join(", ")
end
end
and in config/initializers/load_my_extensions.rb
class Array
include MyExtensions::Array
end
This will cause MyExtensions::Array to be auto-reloaded each time you invoke a request in development mode. This is nicer than restarting your application everytime you make a change to your code.
It would probably be cleaner to add a lib/ directory with all your extensions. Then add a line in config/environment.rb that loads the file:
require File.join(RAILS_ROOT, 'lib', 'array.rb')

Resources