Updating a gem having issues with zeitwerk - ruby-on-rails

I'm trying to update this gem to work with rails 7 and I think that the error is due to zeitwerk. The error that's popping up is the following
/Users/nate/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/impressionist-2.0.0/lib/impressionist/engine.rb:15:in `block (2 levels) in <class:Engine>': uninitialized constant Impressionist::Engine::ImpressionistController (NameError)
and It's originating from lib/impressionist/engine.rb here from the lines below
module Impressionist
class Engine < ::Rails::Engine
attr_accessor :orm
initializer 'impressionist.model' do |app|
#orm = Impressionist.orm
include_orm
end
initializer 'impressionist.controller' do
require "impressionist/controllers/mongoid/impressionist_controller" if orm == :mongoid.to_s
ActiveSupport.on_load(:action_controller) do
include ImpressionistController::InstanceMethods <--- HERE
extend ImpressionistController::ClassMethods <--- HERE
end
end
private
def include_orm
require "#{root}/app/models/impressionist/impressionable.rb"
require "impressionist/models/#{orm}/impression.rb"
require "impressionist/models/#{orm}/impressionist/impressionable.rb"
end
end
end
I think that this first include is trying to call app/controllers/impressionist_controller.rb, but for some reason doesn't load with rails 7. Any ideas on how to approach fixing this? I suspect that I have to somehow incorporate zeitwerk here.

Related

Sudden "uninitialized constant PurIssue" error on STI_preload while upgrading rails to 7.0.1

I am in the process of upgrading a ~2 year old Rails app from 6.1 to 7.0.1
I have an STI setup where Pursuit is the main class and it has several other types as descendants:
# app/models/pursuit.rb
require 'sti_preload'
class Pursuit < ApplicationRecord
# ...
end
Descendant classes all look like this:
# app/models/pur_issue.rb
class PurIssue < Pursuit
# ...
end
--
# app/models/pur_goal.rb
class PurGoal < Pursuit
# ...
end
--
# app/models/pur_tracker.rb
class PurTracker < Pursuit
# ...
end
I have been using STI preload snippet as recommended by the Ruby Guides, and all worked well under Rails 6.0 and 6.1.
But now that I am upgrading to Rails 7.0.1, I suddenly get an "Uninitialized Constant" error for only the PurIssue subclass whereas all the other subclasses load fine:
Showing /Users/me/gits/rffvp/app/views/pursuits/_stats.html.slim where line #1 raised:
uninitialized constant PurIssue
Extracted source (around line #33):
33 types_in_db.each do |type|
34 logger.debug("Preloading STI type #{type}")
35 type.constantize
36 end
37 logger.debug("Types in database #{types_in_db}")
Trace of template inclusion: #<ActionView::Template app/views/layouts/_footer.html.slim locals=[]>, #<ActionView::Template app/views/layouts/application.html.slim locals=[]>
Rails.root: /Users/me/gits/rffvp
Application Trace | Framework Trace | Full Trace
lib/sti_preload.rb:33:in `block in preload_sti'
lib/sti_preload.rb:31:in `each'
lib/sti_preload.rb:31:in `preload_sti'
lib/sti_preload.rb:13:in `descendants'
app/models/pursuit.rb:71:in `<class:Pursuit>'
app/models/pursuit.rb:54:in `<main>'
app/models/pur_issue.rb:52:in `<main>'
app/views/pursuits/_stats.html.slim:1
app/views/layouts/_footer.html.slim:14
app/views/layouts/application.html.slim:13
I can't seem to figure out why PurIssue will not load anymore whereas all the other subclasses will. This is breaking my entire app since PurIssues are the most important data points.
Can someone point me to a Rails configuration change between 6.0, 6.1 and 7.0 that might cause this different behavior?
# lib/sti_preload.rb
module StiPreload
unless Rails.application.config.eager_load
extend ActiveSupport::Concern
included do
cattr_accessor :preloaded, instance_accessor: false
end
class_methods do
def descendants
preload_sti unless preloaded
super
end
# Constantizes all types present in the database. There might be more on
# disk, but that does not matter in practice as far as the STI API is
# concerned.
#
# Assumes store_full_sti_class is true, the default.
def preload_sti
types_in_db = \
base_class
.unscoped
.select(inheritance_column)
.distinct
.pluck(inheritance_column)
.compact
types_in_db.each do |type|
logger.debug("Preloading STI type #{type}")
type.constantize
end
logger.debug("Types in database #{types_in_db}")
self.preloaded = true
end
end
end
end
Zeitwerk shows the same error by the way:
$ rails zeitwerk:check
Hold on, I am eager loading the application.
rails aborted!
NameError: uninitialized constant PurIssue
/Users/me/gits/rffvp/lib/sti_preload.rb:33:in `block in preload_sti'
/Users/me/gits/rffvp/lib/sti_preload.rb:31:in `each'
/Users/me/gits/rffvp/lib/sti_preload.rb:31:in `preload_sti'
/Users/me/gits/rffvp/lib/sti_preload.rb:13:in `descendants'
/Users/me/gits/rffvp/app/models/concerns/validateable.rb:10:in `block in <module:Validateable>'
/Users/me/gits/rffvp/app/models/pursuit.rb:68:in `include'
/Users/me/gits/rffvp/app/models/pursuit.rb:68:in `<class:Pursuit>'
/Users/me/gits/rffvp/app/models/pursuit.rb:54:in `<main>'
/Users/me/gits/rffvp/app/models/pur_issue.rb:1:in `<main>'
Tasks: TOP => zeitwerk:check
(See full trace by running task with --trace)
I saw the same thing trying to do an upgrade to rails 7. I believe it originates with this change: https://github.com/rails/rails/commit/ffae3bd8d69f9ed1ae185e960d7a38ec17118a4d
Effectively the change to invoke Class.descendants directly in the internal association callback methods exposes a more longstanding implicit issue with invoking Class.descendants at all during the autoload, as the StiPreload implementation may result in a problem where it circularly attempts to constantize the original class being autoloaded. I've added an issue in the Rails repo here https://github.com/rails/rails/issues/44252

Rails 7 Engine Initializer Hook Not Working

trying to get my gem working in Rails 7. I have confirmed it works in Rails 6.1.4.1 (latest).
In my engine's engine.rb file I have this...
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace MyEngine
initializer "my_engine.include_controller" do |app|
ActionController::Base.send :include, MyEngine::MyController
end
end
end
Upon server start or running a console I get...
uninitialized constant MyEngine::MyController (NameError)
I have the gem controllers in their namespaced directory and to reiterate this works in Rails 6.1.
I have also tried these variations with the same error...
ActionController::Base.include MyEngine::MyController
ActiveSupport.on_load :action_controller_base do
include MyEngine::MyController
end
If I put the following in the main app's ApplicationController instead then it works...
include MyEngine::MyController
Does anyone have any insight to how these hooks need to be called or should I report this as a bug to the rails team?
I was able to fix the same issue by pushing the required files to the autoload_once_paths configuration. Here an example of a possible fix.
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace MyEngine
config.autoload_once_paths << "#{root}/app/controllers"
initializer "my_engine.include_controller" do |app|
ActionController::Base.send :include, MyEngine::MyController
end
end
end
Sources:
https://edgeguides.rubyonrails.org/autoloading_and_reloading_constants.html#config-autoload-once-paths
https://github.com/charlotte-ruby/impressionist/issues/305
https://github.com/hotwired/turbo-rails/blob/main/lib/turbo/engine.rb

Rails Minitest include to test_helper - uninitialized constant ActiveSupport::TestCase

I want to add helper from test/helpers/auth_request_helpers.rb into test/test_helper.rb to have it available in all tests. I thought all I had to do was to include this helper inside of the test_helper like below:
module ActiveSupport
class TestCase
include AuthRequestHelpers
# some other things
end
end
But when I running a minitest I'm getting an error:
uninitialized constant ActiveSupport::TestCase::AuthRequestHelpers (NameError)
Did I missed something? I'm not using RSpec
Helper which I tried to add below:
module AuthRequestHelpers
def hmac_code(data, secret_key)
Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', secret_key, data))
end
end

ActiveJob custom serializer - uninitialized constant NameError

I am trying to add a custom serializer to ActiveJob following the ActiveJob Rails Guide. I have the following class, originally in the file app/lib/money_serializer.rb,
class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
# ...
end
And in config/application.rb
# ...
config.active_job.custom_serializers << MoneySerializer
# ...
I keep getting uninitialized constant MoneySerializer (NameError) which suggests to me that the Serializer is not being loaded on boot and tried placing it in different locations (including under config/initializers) without any luck.
What am I missing here? Where should I place an ActiveJob serializer?
Based on the discussion here, the issue is best solved as follows:
Rails.autoloaders.main.ignore(Rails.root.join("app/serializers"))
Dir[Rails.root.join("app/serializers/**/*.rb")].each { |f| require f }
Rails.application.config.active_job.custom_serializers << MoneySerializer
Update: In Rails 7, you can simply add the following to config/application.rb:
config.autoload_once_paths += Dir[Rails.root.join("app/serializers")]
If it helps anyone,
It seems to work when I put both the Serializer and the config in an initializer
class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
# ...
end
config.active_job.custom_serializers << MoneySerializer
Feels very odd to have a class here. Any other suggestions?

How to require and auto load files properly

I'm running into quite a few errors around how to require files property. Hoping for some insight.
There are files as so:
app/models
model.rb
app/workers
parent_worker.rb
app/workers/directory_1
directory_worker.rb
foo_worker.rb
bar_worker.rb
class DirectoryWorker < ParentWorker
end
class FooWorker < DirectoryWorker
def method_called_by_model
end
end
When I call the method, method_called_by_model I get the following error:
NameError: uninitialized constant Model::FooWorker
I've added the following to application.rb, didn't add app/workers since it should be loaded automatically according to the documentation.
config.autoload_paths << "#{Rails.root}/app/workers/directory_1"
When I require_relative the worker files in the model I get the following error referring to the inherited class being unknown:
NameError: uninitialized constant DirectoryWorker
from project/app/workers/directory_1/FooWorker.rb:2:in `<top (required)>'
Any have any ideas what I can do?
You need to namespace those workers since they are inside a directory.
First remove the autoload call you added.
Here's how the files should be named and what they should look like inside.
# app/workers/parent_worker.rb
class ParentWorker
end
# app/workers/directory_1/directory_worker.rb
class Directory1::DirectoryWorker < ParentWorker
end
# app/workers/directory_1/foo_worker.rb
class Directory1::FooWorker < Directory1::DirectoryWorker
def method_called_by_model
end
end
# app/workers/directory_1/bar_worker.rb
class Directory1::BarWorker < Directory1::DirectoryWorker
end

Resources