Skip to content

Commit

Permalink
Properly expand the environment's name
Browse files Browse the repository at this point in the history
Running the `console` and `dbconsole` commands with a regular argument
as the environment's name automatically expand it to match an existing
environment (e.g. dev for development).

This feature wasn't available using the `--environment` (a.k.a `-e`)
option.
  • Loading branch information
robin850 committed Jul 16, 2017
1 parent 48b2499 commit 3777701
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,8 @@
* Properly expand shortcuts for environment's name running the `console`
and `dbconsole` commands.

*Robin Dupret*

* Passing the environment's name as a regular argument to the
`rails dbconsole` and `rails console` commands is deprecated.
The `-e` option should be used instead.
Expand Down
7 changes: 6 additions & 1 deletion railties/lib/rails/command/environment_argument.rb
Expand Up @@ -7,6 +7,9 @@ module EnvironmentArgument #:nodoc:

included do
argument :environment, optional: true, banner: "environment"

class_option :environment, aliases: "-e", type: :string,
desc: "Specifies the environment to run this console under (test/development/production)."
end

private
Expand All @@ -19,7 +22,9 @@ def extract_environment_option_from_argument
"will be removed in the next Rails " \
"version. Please, use the -e option " \
"instead."
elsif !options[:environment]
elsif options[:environment]
self.options = options.merge(environment: acceptable_environment(options[:environment]))
else
self.options = options.merge(environment: Rails::Command.environment)
end
end
Expand Down
3 changes: 0 additions & 3 deletions railties/lib/rails/commands/console/console_command.rb
Expand Up @@ -70,9 +70,6 @@ class ConsoleCommand < Base # :nodoc:
class_option :sandbox, aliases: "-s", type: :boolean, default: false,
desc: "Rollback database modifications on exit."

class_option :environment, aliases: "-e", type: :string,
desc: "Specifies the environment to run this console under (test/development/production)."

def initialize(args = [], local_options = {}, config = {})
console_options = []

Expand Down
3 changes: 0 additions & 3 deletions railties/lib/rails/commands/dbconsole/dbconsole_command.rb
Expand Up @@ -151,9 +151,6 @@ class DbconsoleCommand < Base # :nodoc:

class_option :header, type: :boolean

class_option :environment, aliases: "-e", type: :string,
desc: "Specifies the environment to run this console under (test/development/production)."

class_option :connection, aliases: "-c", type: :string,
desc: "Specifies the connection to use."

Expand Down
5 changes: 5 additions & 0 deletions railties/test/commands/console_test.rb
Expand Up @@ -82,6 +82,11 @@ def test_e_option
assert_match(/\sspecial-production\s/, output)
end

def test_e_option_is_properly_expanded
start ["-e", "prod"]
assert_match(/\sproduction\s/, output)
end

def test_environment_option
start ["--environment=special-production"]
assert_match(/\sspecial-production\s/, output)
Expand Down
6 changes: 6 additions & 0 deletions railties/test/commands/dbconsole_test.rb
Expand Up @@ -105,6 +105,12 @@ def test_rails_env_is_development_when_argument_is_dev
end
end

def test_rails_env_is_development_when_environment_option_is_dev
stub_available_environments([ "development", "test" ]) do
assert_match("development", parse_arguments([ "-e", "dev" ])[:environment])
end
end

def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
assert_deprecated do
stub_available_environments([ "dev" ]) do
Expand Down

0 comments on commit 3777701

Please sign in to comment.