From dd655d88d660da8c094d20948ee721d29852f723 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 1 Feb 2012 13:36:47 +0100 Subject: Refactor Rails::Console to make it easier to test and add tests for it --- railties/lib/rails/commands/console.rb | 71 ++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 25 deletions(-) (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb index e2956bbef6..86376ac7e6 100644 --- a/railties/lib/rails/commands/console.rb +++ b/railties/lib/rails/commands/console.rb @@ -4,48 +4,69 @@ require 'irb/completion' module Rails class Console - def self.start(app) - new(app).start + attr_reader :options, :app, :console, :arguments + + def self.start(*args) + new(*args).start end - def initialize(app) - @app = app + def initialize(app, arguments = ARGV) + @app = app + @arguments = arguments + app.load_console + @console = app.config.console || IRB end - def start - options = {} + def options + @options ||= begin + options = {} + + OptionParser.new do |opt| + opt.banner = "Usage: console [environment] [options]" + opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v } + opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v } + opt.parse!(arguments) + end - OptionParser.new do |opt| - opt.banner = "Usage: console [environment] [options]" - opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v } - opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v } - opt.parse!(ARGV) + options end + end - @app.sandbox = options[:sandbox] - @app.load_console + def sandbox? + options[:sandbox] + end - if options[:debugger] - begin - require 'ruby-debug' - puts "=> Debugger enabled" - rescue Exception - puts "You need to install ruby-debug19 to run the console in debugging mode. With gems, use 'gem install ruby-debug19'" - exit - end - end + def debugger? + options[:debugger] + end + + def start + app.sandbox = sandbox? + + require_debugger if debugger? - if options[:sandbox] + if sandbox? puts "Loading #{Rails.env} environment in sandbox (Rails #{Rails.version})" puts "Any modifications you make will be rolled back on exit" else puts "Loading #{Rails.env} environment (Rails #{Rails.version})" end - console = Rails.application.config.console || IRB - console::ExtendCommandBundle.send :include, Rails::ConsoleMethods + if defined?(console::ExtendCommandBundle) + console::ExtendCommandBundle.send :include, Rails::ConsoleMethods + end console.start end + + def require_debugger + begin + require 'ruby-debug' + puts "=> Debugger enabled" + rescue Exception + puts "You need to install ruby-debug19 to run the console in debugging mode. With gems, use 'gem install ruby-debug19'" + exit + end + end end end -- cgit v1.2.3