Skip to content

Commit 75f3f86

Browse files
committed
Support and require Rails 7.2
1 parent 402a775 commit 75f3f86

File tree

8 files changed

+96
-11
lines changed

8 files changed

+96
-11
lines changed

lib/rails_console_commands.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# frozen_string_literal: true
22

3-
require 'rails/console/app'
4-
require_relative './rails_console_commands/console_delegation'
3+
require_relative './rails_console_commands/test_command'
4+
require_relative './rails_console_commands/rake_command'
5+
require_relative './rails_console_commands/generate_command'
6+
require_relative './rails_console_commands/destroy_command'
7+
require_relative './rails_console_commands/update_command'
58
require_relative './rails_console_commands/version'
69

7-
Rails::ConsoleMethods.send :include, RailsConsoleCommands::ConsoleDelegation
10+
IRB::Command.register(:test, RailsConsoleCommands::TestCommand)
11+
IRB::Command.register(:rake, RailsConsoleCommands::RakeCommand)
12+
IRB::Command.register(:generate, RailsConsoleCommands::GenerateCommand)
13+
IRB::Command.register(:destroy, RailsConsoleCommands::DestroyCommand)
14+
IRB::Command.register(:update, RailsConsoleCommands::UpdateCommand)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module RailsConsoleCommands
4+
module ArgParser
5+
def parse_arg(arg)
6+
# IRB parses the arg differently for single and double quotes strings. E.g.
7+
# test 'foo' -> "'foo'"
8+
# test "foo" -> "\"foo\""
9+
# test 'foo', 10 -> "'foo', 10"
10+
# test "foo", 10 -> "\"foo\", 10"
11+
arg = arg.strip
12+
arg = arg.delete("'") # handle single quote case
13+
begin
14+
JSON.parse(arg) # handle double quote case
15+
rescue JSON::ParserError
16+
arg
17+
end
18+
end
19+
end
20+
end

lib/rails_console_commands/commander.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
module RailsConsoleCommands
88
class Commander
9+
def self.commander
10+
@commander ||= Commander.new
11+
end
12+
913
delegate :rake, to: :raker
1014
delegate :test, to: :tester
1115
delegate :generate, :destroy, :update, to: :generator
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require_relative './arg_parser'
4+
require_relative './commander'
5+
6+
module RailsConsoleCommands
7+
class DestroyCommand < IRB::Command::Base
8+
include ArgParser
9+
10+
def execute(arg)
11+
Commander.commander.destroy(parse_arg(arg))
12+
end
13+
end
14+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require_relative './arg_parser'
4+
require_relative './commander'
5+
6+
module RailsConsoleCommands
7+
class GenerateCommand < IRB::Command::Base
8+
include ArgParser
9+
10+
def execute(arg)
11+
Commander.commander.generate(parse_arg(arg))
12+
end
13+
end
14+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require_relative './arg_parser'
4+
require_relative './commander'
5+
6+
module RailsConsoleCommands
7+
class RakeCommand < IRB::Command::Base
8+
include ArgParser
9+
10+
def execute(arg)
11+
Commander.commander.rake(parse_arg(arg))
12+
end
13+
end
14+
end
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
# frozen_string_literal: true
22

3+
require_relative './arg_parser'
34
require_relative './commander'
45

56
module RailsConsoleCommands
6-
module ConsoleDelegation
7-
def commander
8-
@commander ||= Commander.new
9-
end
7+
class TestCommand < IRB::Command::Base
8+
include ArgParser
109

11-
def test(*args)
10+
def execute(arg)
1211
if Rails.env.test?
13-
commander.test(*args)
12+
what, line = arg.split(',').map{ |a| parse_arg(a) }
13+
Commander.commander.test(what, line)
1414
else
1515
puts 'You can only run tests in a console started in the test environment. ' \
1616
'Use `rails console test` to start such a console'
1717
end
1818
end
19-
20-
delegate :rake, :generate, :destroy, :update, to: :commander
2119
end
2220
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require_relative './arg_parser'
4+
require_relative './commander'
5+
6+
module RailsConsoleCommands
7+
class UpdateCommand < IRB::Command::Base
8+
include ArgParser
9+
10+
def execute(arg)
11+
Commander.commander.update(parse_arg(arg))
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)