Skip to content
This repository
Browse code

match rails console environment support, to server

rails server takes `-e` as an argument to specify RAILS_ENV, rails console currently does not have the same interface. This commit fixes this disparity so developers can manually specify `RAILS_ENV` or can pass in an environment with a `-e`.
  • Loading branch information...
commit 7529283732bb56ba1b0125aabab774d01b4057c7 1 parent 0a555dd
Richard Schneeman authored March 22, 2012
13  railties/lib/rails/commands/console.rb
@@ -24,6 +24,9 @@ def options
24 24
         OptionParser.new do |opt|
25 25
           opt.banner = "Usage: console [environment] [options]"
26 26
           opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v }
  27
+          opt.on("-e", "--environment=name", String,
  28
+                  "Specifies the environment to run this console under (test/development/production).",
  29
+                  "Default: development") { |v| options[:environment] = v.strip }
27 30
           opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v }
28 31
           opt.parse!(arguments)
29 32
         end
@@ -36,6 +39,14 @@ def sandbox?
36 39
       options[:sandbox]
37 40
     end
38 41
 
  42
+    def environment?
  43
+      options[:environment]
  44
+    end
  45
+
  46
+    def set_environment!
  47
+      Rails.env = options[:environment]
  48
+    end
  49
+
39 50
     def debugger?
40 51
       options[:debugger]
41 52
     end
@@ -45,6 +56,8 @@ def start
45 56
 
46 57
       require_debugger if debugger?
47 58
 
  59
+      set_environment! if environment?
  60
+
48 61
       if sandbox?
49 62
         puts "Loading #{Rails.env} environment in sandbox (Rails #{Rails.version})"
50 63
         puts "Any modifications you make will be rolled back on exit"
19  railties/test/commands/console_test.rb
@@ -55,6 +55,25 @@ def test_start_with_sandbox
55 55
     assert_match /Loading \w+ environment in sandbox \(Rails/, output
56 56
   end
57 57
 
  58
+  def test_console_with_environment
  59
+    app.expects(:sandbox=).with(nil)
  60
+    FakeConsole.expects(:start)
  61
+
  62
+    start ["-e production"]
  63
+
  64
+    assert_match /production/, output
  65
+  end
  66
+
  67
+  def test_console_with_rails_environment
  68
+    app.expects(:sandbox=).with(nil)
  69
+    FakeConsole.expects(:start)
  70
+
  71
+    start ["RAILS_ENV=production"]
  72
+
  73
+    assert_match /production/, output
  74
+  end
  75
+
  76
+
58 77
   def test_console_defaults_to_IRB
59 78
     config = mock("config", :console => nil)
60 79
     app = mock("app", :config => config)

0 notes on commit 7529283

Please sign in to comment.
Something went wrong with that request. Please try again.