Skip to content

Commit 484c026

Browse files
committed
Continue including Rails::ConsoleMethods to IRB's internal
Due to some users/libs relying on Rails::ConsoleMethod to extend Rails console, we need to keep including it to IRB's internal. But to prevent also adding `app` and `helper` methods to the console, which are already registered to IRB through its API, we need to remove them from the Rails::ConsoleMethods. Additionally, we should raise deprecation warning when users try to use Rails::ConsoleMethods in this way. This commit: - Removes all methods from Rails::ConsoleMethods that are already registered to IRB. So it's now essentially empty. - Raises a deprecation warning when modules are included to Rails::ConsoleMethods or methods are added to it. - Adds a test to ensure that the deprecation warning is raised and the methods are available in the console.
1 parent 6c73ee0 commit 484c026

File tree

7 files changed

+81
-65
lines changed

7 files changed

+81
-65
lines changed

railties/lib/rails/commands/console/irb_console.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ControllerHelper < RailsHelperBase
1414

1515
# This method assumes an +ApplicationController+ exists, and that it extends ActionController::Base.
1616
def execute
17-
helper
17+
ApplicationController.helpers
1818
end
1919
end
2020

@@ -23,23 +23,31 @@ class ControllerInstance < RailsHelperBase
2323

2424
# This method assumes an +ApplicationController+ exists, and that it extends ActionController::Base.
2525
def execute
26-
controller
26+
@controller ||= ApplicationController.new
2727
end
2828
end
2929

3030
class NewSession < RailsHelperBase
3131
description "Create a new session. If a block is given, the new session will be yielded to the block before being returned."
3232

33-
def execute
34-
new_session
33+
def execute(*)
34+
app = Rails.application
35+
session = ActionDispatch::Integration::Session.new(app)
36+
37+
# This makes app.url_for and app.foo_path available in the console
38+
session.extend(app.routes.url_helpers)
39+
session.extend(app.routes.mounted_helpers)
40+
41+
session
3542
end
3643
end
3744

38-
class AppInstance < RailsHelperBase
45+
class AppInstance < NewSession
3946
description "Reference the global 'app' instance, created on demand. To recreate the instance, pass a non-false value as the parameter."
4047

4148
def execute(create = false)
42-
app(create)
49+
@app_integration_instance = nil if create
50+
@app_integration_instance ||= super
4351
end
4452
end
4553

@@ -50,7 +58,8 @@ class Reloader < IRB::Command::Base
5058
description "Reloads the environment."
5159

5260
def execute(*)
53-
reload!
61+
puts "Reloading..."
62+
Rails.application.reloader.reload!
5463
end
5564
end
5665

@@ -101,6 +110,10 @@ def start
101110
end
102111
end
103112

113+
# Because some users/libs use Rails::ConsoleMethods to extend Rails console,
114+
# we still include it for backward compatibility.
115+
IRB::ExtendCommandBundle.include ConsoleMethods
116+
104117
# Respect user's choice of prompt mode.
105118
IRB.conf[:PROMPT_MODE] = :RAILS_PROMPT if IRB.conf[:PROMPT_MODE] == :DEFAULT
106119
IRB::Irb.new.run(IRB.conf)

railties/lib/rails/console/app.rb

Lines changed: 0 additions & 35 deletions
This file was deleted.

railties/lib/rails/console/helpers.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

railties/lib/rails/console/methods.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
module Rails
4+
module ConsoleMethods
5+
def self.include(_mod, ...)
6+
raise_deprecation_warning
7+
super
8+
end
9+
10+
def self.method_added(_method_name)
11+
raise_deprecation_warning
12+
super
13+
end
14+
15+
def self.raise_deprecation_warning
16+
ActiveSupport::Deprecation.new.warn(<<~MSG, caller_locations(1..1))
17+
Extending Rails console through `Rails::ConsoleMethods` is deprecated and will be removed in Rails 7.3.
18+
Please directly use IRB's extension API to add new commands or helpers to the console.
19+
For more details, please visit: https://github.com/ruby/irb/blob/master/EXTEND_IRB.md
20+
MSG
21+
end
22+
end
23+
end

railties/lib/rails/engine.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,7 @@ def initialize
451451
# Load console and invoke the registered hooks.
452452
# Check Rails::Railtie.console for more info.
453453
def load_console(app = self)
454-
require "rails/console/app"
455-
require "rails/console/helpers"
454+
require "rails/console/methods"
456455
run_console_blocks(app)
457456
self
458457
end

railties/test/application/console_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,42 @@ def test_reload_command_fires_preparation_and_cleanup_callbacks
184184
write_prompt "c", "=> 3"
185185
end
186186

187+
def test_rails_console_methods_patch_backward_compatibility_with_module_inclusion
188+
add_to_config <<-RUBY
189+
module MyConsole
190+
def foo
191+
"this is foo"
192+
end
193+
end
194+
195+
console do
196+
::Rails::ConsoleMethods.include(MyConsole)
197+
end
198+
RUBY
199+
200+
spawn_console("-e development", wait_for_prompt: false)
201+
202+
assert_output "Extending Rails console through `Rails::ConsoleMethods` is deprecated", @primary, 30
203+
write_prompt "foo", "=> \"this is foo\""
204+
end
205+
206+
def test_rails_console_methods_patch_backward_compatibility_with_module_reopening
207+
add_to_config <<-RUBY
208+
console do
209+
::Rails::ConsoleMethods.module_eval do
210+
def foo
211+
"this is foo"
212+
end
213+
end
214+
end
215+
RUBY
216+
217+
spawn_console("-e development", wait_for_prompt: false)
218+
219+
assert_output "Extending Rails console through `Rails::ConsoleMethods` is deprecated", @primary, 30
220+
write_prompt "foo", "=> \"this is foo\""
221+
end
222+
187223
def test_reload_command_reload_constants
188224
app_file "app/models/user.rb", <<-MODEL
189225
class User

railties/test/commands/console_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ def config
159159
end
160160

161161
def load_console
162-
require "rails/console/app"
163-
require "rails/console/helpers"
162+
require "rails/console/methods"
164163
end
165164
end
166165
mocked_app.new(console)

0 commit comments

Comments
 (0)