1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/lib/rails/command/environment_argument.rb
yuuji.yaginuma c801b232bc Properly expand the environment's name in all commands
Since 3777701f13, the environment's name is
automatically expanded in console and dbconsole commands.
In order to match the behavior between the commands, fixes it to have the
same behavior of all the commands.

This behavior is defined in `EnvironmentArgument`. Since
`EnvironmentArgument` also defines the environment option, it is reused.

However, since desc was not content that can be used in all comments,
fixed desc to be defined for each command.
2019-03-15 07:43:10 +09:00

38 lines
1.2 KiB
Ruby

# frozen_string_literal: true
require "active_support"
require "active_support/core_ext/class/attribute"
module Rails
module Command
module EnvironmentArgument #:nodoc:
extend ActiveSupport::Concern
included do
class_attribute :environment_desc, default: "Specifies the environment to run this #{self.command_name} under (test/development/production)."
class_option :environment, aliases: "-e", type: :string, desc: environment_desc
end
private
def extract_environment_option_from_argument(default_environment: Rails::Command.environment)
if options[:environment]
self.options = options.merge(environment: acceptable_environment(options[:environment]))
else
self.options = options.merge(environment: default_environment)
end
end
def acceptable_environment(env = nil)
if available_environments.include? env
env
else
%w( production development test ).detect { |e| e =~ /^#{env}/ } || env
end
end
def available_environments
Dir["config/environments/*.rb"].map { |fname| File.basename(fname, ".*") }
end
end
end
end