Skip to content

Commit 38b1ce3

Browse files
committed
Merge pull request #309 from rzane/rails-console-env
Respect -e and --environment options for rails console
2 parents cb0fb6f + ea3009c commit 38b1ce3

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Next Version
2+
3+
* Accept -e and --environment options for `rails console`.
4+
15
## 1.1.3
26

37
* The `rails runner` command no longer passes environment switches to

lib/spring/commands/rails.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@ def description
1313

1414
class RailsConsole < Rails
1515
def env(args)
16-
args.first if args.first && !args.first.index("-")
16+
return args.first if args.first && !args.first.index("-")
17+
18+
environment = nil
19+
20+
args.each.with_index do |arg, i|
21+
if arg =~ /--environment=(\w+)/
22+
environment = $1
23+
elsif i > 0 && args[i - 1] == "-e"
24+
environment = arg
25+
end
26+
end
27+
28+
environment
1729
end
1830

1931
def command_name

test/unit/commands_test.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ class CommandsTest < ActiveSupport::TestCase
77
assert_equal 'test', command.env(['test'])
88
end
99

10-
test 'console command ignores first argument if it is a flag' do
10+
test 'console command sets rails environment from -e option' do
11+
command = Spring::Commands::RailsConsole.new
12+
assert_equal 'test', command.env(['-e', 'test'])
13+
end
14+
15+
test 'console command sets rails environment from --environment option' do
16+
command = Spring::Commands::RailsConsole.new
17+
assert_equal 'test', command.env(['--environment=test'])
18+
end
19+
20+
test 'console command ignores first argument if it is a flag except -e and --environment' do
1121
command = Spring::Commands::RailsConsole.new
1222
assert_nil command.env(['--sandbox'])
1323
end

0 commit comments

Comments
 (0)