Rails: Nested Namespace'd Controller in Rails 3.2 fails to load (fine in 3.0) - ruby-on-rails

I have a controller named Reports::Accountant::ApprovedTimeOffRequestsController. It's in the proper directory, and the class name is correct in the file its self. Rails console and unicorn load just fine, but rspec can't seem to load the file during testing.
Here's the error.
/Users/mdarby/.rvm/gems/ruby-1.9.3-p125-perf/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:503:in load_missing_constant': Expected /Users/mdarby/Documents/Code/ccw_32/app/controllers/reports/accountant/approved_time_off_requests_controller.rb to define Reports::Accountant::ApprovedTimeOffRequestsController (LoadError)
from /Users/mdarby/.rvm/gems/ruby-1.9.3-p125-perf/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:192:in block in const_missing'
This file was find on Rails 3.0 stack, but now that I've upgraded to 3.2/rspec 2.9 this is bombing out.
The definition of the class:
class Reports::Accountant::ApprovedTimeOffRequestsController < ApplicationController
EDIT:
If anyone comes across this, I solved it by removing the specs :)

I bet you're using Spork. I ran into this exact problem. I removed Spork and I was on my way. Give that a whack and let me know if it fixed it. Might be worth filing an issue on spork if that's the case.

Are you doing anything fancy with your tests? I had a case in the app itself where multiple threads were trying to load the same module. I can only guess that one marks it as loaded before it actually runs it, and then the constant isn't available to the next. In any case, my solution was to add a prepare block outside the threading.
development.rb (you might need this in test.rb)
ActionDispatch::Reloader.to_prepare do
Reports::Accountant::ApprovedTimeOffRequestsController
end

Related

There is a programmatical way to detect Zeitwerk::NameError when upgrading to Rails 6?

I am currently migrating an old Rails application to Rails 6.
It seems that some files in the project are not consistent with the classes defined in it.
I don't see this error when running the tests of the application, but after deploy I receive errors like:
Zeitwerk::NameError: expected file /app/my?_/app/lib/multi_io.rb to define constant MultiIo, but didn't
In previous SO questions I could find suggestions to:
Rails 6: Zeitwerk::NameError doesn't load class from module set config.autoloader = :classic
How to ignore a folder in Zeitwerk for Rails 6? ignore a given path
Autoloading and Zeitwerk Mode in Rails 6 tweak config.autoload_path
I am not looking for a workaround so I corrected the file raising the exception.
I would like to find a programmatical way to detect all inconsistent path/class names without running the application in production mode.
So far, three options came to my mind:
A rake task to validate the autoloading (which is not provided by the gem). Does it exists? Would you have a snippet to run from the rails console in development env instead?
A rubocop cop or another static code analyser. There is any?
Force the eager load in the test environment
What it the suggested approach to validate my code without requiring several iterations/deployments?
thank you very much in advance
You can check the autoload compatibility of a project with the zeitwerk::check task:
$ bin/rails zeitwerk:check
Hold on, I am eager loading the application.
All is good!
It exercises all the autoload paths and will flag issues such as those caused by the change in constant name inference (such as those with acronyms). The task doesn't appear to be widely advertised or documented except in the upgrade guide. But, I actually use it sometimes to just flush out constant naming issues in the project generally (i.e., where the class doesn't match the file).
#rmlockerd Described one of the best tools bin/rails zeitwerk:check for this in his answer here.
The other articles I found informative were:
Understanding Zeitwerk in Rails
9 Tips & Tricks To Help You Enable The Zeitwerk Autoloader In Your
Rails App
Rails doc on autoloading
Rails doc on upgrading from Rails 5.2 to 6.0
The process I took was to run bin/rails zeitwerk:check fix an error, run it again until I saw "All is good". After that worked through errors reported by our spec tests.
One general problem to fix is if a folder is not within the Rails default file structure, Zeitwerk will not be able to find it when autoloading. We had a folder named "services" (app/services) that needed to be added to the autoload path within the application.rb file like:
config.autoload_paths += %W(#{config.root}/app/services/)
Error:
Expected file path/file_name.rb to define constant FileName
Fix:
remove any require or require_relative in other files referencing where the class is defined (Zeitwerk doesn't need them).
Error (class is defined within a module of a sub directory):
NameError: uninitialized constant SubFile::ClassName
Fix:
Detailed in another S/O answer. Whenever method is used within sub folders structure the call as Folder::SubDirectory::Class.method_call
Goodluck!

After installing paper_trail, get "irb: warn: can't alias context from irb_context." from rails console

I've tested this by running rails c both before and after git stash. On Rails 4.1 in Mavericks, after following the instructions to add the versions table and adding has_paper_trail to three models, whenever I run rails c I get
irb: warn: can't alias context from irb_context.
I've spent some time Googling without much luck, there's old threads talking about rspec, but I don't see how that's relevant since I'm not using it. Any ideas why this is happening?
RSpec used to polute provide Object top-level methods, e.g. describe, context, etc. Fortunately they've got rid of all the monkey patching in version 3, and now all these methods are namespaced under RSpec.
One can change this behaviour through the expose_dsl_globally config flag. For backwards compatibility, it defaults to true.
The warning shows up when you open the console because paper_trail automatically loads its rspec helpers when rspec is found. And it calls RSpec.configure before you have the chance to tweak your own configuration.
One possible solution would be paper_trail to disable the automatically loading and let users to load it themselves when they see fit. However, I am not aware of the internals of the library, so I can't guarantee this wouldn't break other things.
Best!
This is now fixed in papertrail 4.0.0, here's the commit.

Rails: Reloading code in development environment gives a "ApplicationHelper has been removed from the module tree but is still active!" error

I upgraded to Rails 4 and now when some changes are made to the code, or when I do a reload! in rails console, I will get an ArgumentError: A copy of ApplicationHelper has been removed from the module tree but is still active! error.
I'm not the only one with this problem but still cant find existing solutions that work yet.
I've never encountered that problem before. I'm using Rails4 as well.
I think you have to call your controller(which is shown in the ArgumentError) explicitly in your dependent controllers.
Can you try adding this line on top inside the class?
require "_controller"
Then try rails server -e production
See if this helps. Sorry I wish I could give you more detailed solution.
If all else fails, rollback might be an option to consider.

Models not reloading in development in Rails (3.2.11) project

I've searched fairly extensively for any advice and have yet to find it so, here goes:
My Rails project fails to automatically reload models in development. Reloading them currently requires a full server restart.
Previous instances of this issue have been related to non-activerecord files placed in the models directory, though this is not the case for me.
config.cache_classes is properly set to false in my development config file. Views and controllers reload without issue.
All of my rails components are version 3.2.11. I have tried disabling all of my development-specific gems to no avail. This is obviously not a productivity stopper, but it is quite an annoyance. Any help appreciated and I am happy to provide more information if it would help, though I am not using any exotic gems.
Thanks!
Some possibilities:
You are not really running on developement environment
You are changing a model within a namespace and didn't told rails to autoload the path
You are changing a file that is included in your class, not your class directly (or any of the many variants for this)
You are caching classes
Considerations:
Things might change according to the webserver you are using
How do you know it's not reloading?
I ask my question because I was having the exact same issue when I was trying to insert a debugger into what I thought was a piece of code that was being executed. I assumed the model wasn't being reloaded since it was not hitting the debugger but it was actually a call back that was redirecting me around the code with the debugger line in it.
So, it might be something other than your models not being reloaded.

Rails moving from 2.2.2 to 2.3.8 - rake not loading the environment same as the console or my app

I have an app that has been around a while that I'm migrating for
Rails 2.3.8. There have been a lot of interesting issues, but I'm on
the home stretch, but have now run into some very odd errors.
The gist is when I run a task with rake it fails, but when I run the
same code form the console it works fine. The 'fix' I devised was to
move my include ModuleNameA, include ModuleNameB, etc to the bottom of
the AR file.
I don't like this 'fix' as I don't understand it. Before I moved them
the AR object (we will call it Bob) kept throwing undefined method
errors when it was called through the rake invocation. It was invoked
in the rake task, but in another AR object.
So the rake task was running something like Worker.work, and it would
fail as described above. When I run Worker.work from the console it
would pass. Once I moved the includes to the bottom of the file both
would work.
Anyone ever seen anything like this? I feel like something
fundamental isn't correct. Like I have broken something basic to the
functionality and my 'fix' is some kind of weird patch.
thanks.
Erik
PS:
There is a module that I'm including in my AR object. It adds class and instance methods by doing:
def self.included(base)
base.extend(ClassMethods)
end
All of the methods in this module are available in the console, but not through rake tasks.
Update:
I noticed that if i take the :work => :environment part out of the
rake task and instead in the task do block require the environment
file by hand (e.g. require(File.join(RAILS_ROOT, 'config',
'environment'))) it works fine. All of a sudden all of my class
methods are available. this is very disturbing.
So I figured it out....
We had a directory under app/models called deals that was not included in the load path. That is where my module was that was giving me all the issues. Why it was being loaded OK from the console and not with rake...I don't know.
Once I added this to the config.load_path everything has started working fine. I never noticed this because in 2.2.2 everything worked fine, the app in production or dev mode, the console and the rake tasks in dev, test, and production mode.
I figured it was something just basic i was doing wrong as the error was obtuse and my 'fixes' looked like really bad ideas.

Resources