I’m using Ruby 3.0.2 and Rails 6.1.4.4. I have installed a gem, octoshark v 0.3.0, that doesn’t play well with Ruby 3. The gem file is
~/.rvm/gems/ruby-3.0.2@cfs-web/gems/octoshark-0.3.0/lib/octoshark/active_record_extensions.rb
And the specific line is line 5 in its ConnectionHandler.establish_connection method. I want to monkey-patch this method but putting my code into a file, lib/ext/octoshark/active_record_extensions.rb, and then loading it in config/initializers/ext.rb with
Dir.glob(Rails.root.join('lib/ext/**/*.rb')).sort.each do |filename|
require filename
end
But when I start my server, it appears my Octoshark gem code is getting called before the monkey patch code. (I proved this by putting a statement in the “ext.rb” file that would intentionally raise an error). What’s the right way to monkey patch this file?
Edit: Per the suggestion given, I created a file, lib/core_extensions/octoshark/connection_handler.rb, with this
module CoreExtensions
module Octoshark
module ConnectionHandler
def establish_connection(config, **parameters)
Octoshark::ConnectionPoolsManager.reset_connection_managers!
super(config, **parameters)
end
end
end
end