From 88a8f1a66db58a1d87fc4e8655a0290a49783c41 Mon Sep 17 00:00:00 2001 From: Meliq Pilosyan Date: Sat, 11 May 2024 03:39:37 +0400 Subject: [PATCH] Adds support for Rails version >= 7.2 After upgrading [Rails console using IRB's latest official extension APIs](https://github.com/rails/rails/pull/51705) pry-rails is failing to open the console. This change takes Rails' console helper methods defined in IRB::HelperMethod.helper_methods and adds to the main object. --- lib/pry-rails/railtie.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/pry-rails/railtie.rb b/lib/pry-rails/railtie.rb index 4d5beb1..0d8e72a 100644 --- a/lib/pry-rails/railtie.rb +++ b/lib/pry-rails/railtie.rb @@ -18,12 +18,33 @@ class Railtie < Rails::Railtie Rails.application.config.console = Pry end - if (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR >= 2) || - Rails::VERSION::MAJOR >= 4 + major = Rails::VERSION::MAJOR + minor = Rails::VERSION::MINOR + + if (major == 3 && minor >= 2) || (major >= 4 && (major < 7 || (major == 7 && minor < 2))) require "rails/console/app" require "rails/console/helpers" TOPLEVEL_BINDING.eval('self').extend ::Rails::ConsoleMethods end + + if major > 7 || (major == 7 && minor >= 2) + require "rails/commands/console/irb_console" + + Module.new do + def reload! + puts "Reloading..." + Rails.application.reloader.reload! + end + + ::IRB::HelperMethod.helper_methods.each do |name, helper_method_class| + define_method name do |*args, **opts, &block| + helper_method_class.instance.execute(*args, **opts, &block) + end + end + + TOPLEVEL_BINDING.eval("self").extend self + end + end end end end