Skip to content

Commit

Permalink
Support and require Rails 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuay03 committed May 7, 2024
1 parent 402a775 commit 96278ae
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 12 deletions.
13 changes: 10 additions & 3 deletions lib/rails_console_commands.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# frozen_string_literal: true

require 'rails/console/app'
require_relative './rails_console_commands/console_delegation'
require_relative './rails_console_commands/test_command'
require_relative './rails_console_commands/rake_command'
require_relative './rails_console_commands/generate_command'
require_relative './rails_console_commands/destroy_command'
require_relative './rails_console_commands/update_command'
require_relative './rails_console_commands/version'

Rails::ConsoleMethods.send :include, RailsConsoleCommands::ConsoleDelegation
IRB::Command.register(:test, RailsConsoleCommands::TestCommand)
IRB::Command.register(:rake, RailsConsoleCommands::RakeCommand)
IRB::Command.register(:generate, RailsConsoleCommands::GenerateCommand)
IRB::Command.register(:destroy, RailsConsoleCommands::DestroyCommand)
IRB::Command.register(:update, RailsConsoleCommands::UpdateCommand)
20 changes: 20 additions & 0 deletions lib/rails_console_commands/arg_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module RailsConsoleCommands
module ArgParser
def parse_arg(arg)
# IRB parses the arg differently for single and double quotes strings. E.g.
# test 'foo' -> "'foo'"
# test "foo" -> "\"foo\""
# test 'foo', 10 -> "'foo', 10"
# test "foo", 10 -> "\"foo\", 10"
arg = arg.strip
arg = arg.delete("'") # handle single quote case
begin
JSON.parse(arg) # handle double quote case
rescue JSON::ParserError
arg
end
end
end
end
4 changes: 4 additions & 0 deletions lib/rails_console_commands/commander.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

module RailsConsoleCommands
class Commander
def self.commander
@commander ||= Commander.new
end

delegate :rake, to: :raker
delegate :test, to: :tester
delegate :generate, :destroy, :update, to: :generator
Expand Down
14 changes: 14 additions & 0 deletions lib/rails_console_commands/destroy_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require_relative './arg_parser'
require_relative './commander'

module RailsConsoleCommands
class DestroyCommand < IRB::Command::Base
include ArgParser

def execute(arg)
Commander.commander.destroy(parse_arg(arg))
end
end
end
14 changes: 14 additions & 0 deletions lib/rails_console_commands/generate_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require_relative './arg_parser'
require_relative './commander'

module RailsConsoleCommands
class GenerateCommand < IRB::Command::Base
include ArgParser

def execute(arg)
Commander.commander.generate(parse_arg(arg))
end
end
end
14 changes: 14 additions & 0 deletions lib/rails_console_commands/rake_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require_relative './arg_parser'
require_relative './commander'

module RailsConsoleCommands
class RakeCommand < IRB::Command::Base
include ArgParser

def execute(arg)
Commander.commander.rake(parse_arg(arg))
end
end
end
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# frozen_string_literal: true

require_relative './arg_parser'
require_relative './commander'

module RailsConsoleCommands
module ConsoleDelegation
def commander
@commander ||= Commander.new
end
class TestCommand < IRB::Command::Base
include ArgParser

def test(*args)
def execute(arg)
if Rails.env.test?
commander.test(*args)
what, line = arg.split(',').map{ |a| parse_arg(a) }
Commander.commander.test(what, line)
else
puts 'You can only run tests in a console started in the test environment. ' \
'Use `rails console test` to start such a console'
end
end

delegate :rake, :generate, :destroy, :update, to: :commander
end
end
14 changes: 14 additions & 0 deletions lib/rails_console_commands/update_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require_relative './arg_parser'
require_relative './commander'

module RailsConsoleCommands
class UpdateCommand < IRB::Command::Base
include ArgParser

def execute(arg)
Commander.commander.update(parse_arg(arg))
end
end
end
2 changes: 1 addition & 1 deletion rails_console_commands.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rubocop', '~> 0.49'

spec.add_dependency 'railties', '>= 5'
spec.add_dependency 'railties', '>= 7.2'
end

0 comments on commit 96278ae

Please sign in to comment.