Skip to content

Commit

Permalink
Rails::ConsoleMethods deprecation warning should point to the source
Browse files Browse the repository at this point in the history
Before:

```
 (called from Rails::ConsoleMethods.include at /home/zzak/code/rails/railties/lib/rails/console/methods.rb:6)
```

After:

```
 (called from block in <class:Engine> at /home/zzak/.rbenv/versions/3.4.0/lib/ruby/gems/3.4.0+0/bundler/gems/mission_control-jobs-7295d75ed735/lib/mission_control/jobs/engine.rb:73)
```

Co-authored-by: Wojciech Wnętrzak <[email protected]>
  • Loading branch information
zzak and morgoth committed Jun 7, 2024
1 parent 63db7d5 commit 132bd8f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion railties/lib/rails/console/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.method_added(_method_name)
end

def self.raise_deprecation_warning
Rails.deprecator.warn(<<~MSG, caller_locations(1..1))
Rails.deprecator.warn(<<~MSG, caller_locations(2..2))
Extending Rails console through `Rails::ConsoleMethods` is deprecated and will be removed in Rails 7.3.
Please directly use IRB's extension API to add new commands or helpers to the console.
For more details, please visit: https://github.com/ruby/irb/blob/master/EXTEND_IRB.md
Expand Down
26 changes: 26 additions & 0 deletions railties/test/application/console_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,32 @@ def foo
write_prompt "foo", "=> \"this is foo\""
end

def test_rails_console_methods_patch_with_module_inclusion_deprecation_caller_location
add_to_config <<-RUBY
module MyConsole
def foo
"this is foo"
end
end
console do
::Rails::ConsoleMethods.include(MyConsole)
end
RUBY

spawn_console("-e development", wait_for_prompt: false)

line_number = 0
app = File.readlines("#{app_path}/config/application.rb")
app.each_with_index do |line, index|
if line.include?("Rails::ConsoleMethods.include(MyConsole)")
line_number = index + 1
end
end

assert_output "(called from block in <class:Application> at #{app_path}/config/application.rb:#{line_number})", @primary, 30
end

def test_reload_command_reload_constants
app_file "app/models/user.rb", <<-MODEL
class User
Expand Down

0 comments on commit 132bd8f

Please sign in to comment.