railties 7.0.4 → 7.1.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +729 -174
  3. data/MIT-LICENSE +1 -1
  4. data/RDOC_MAIN.md +99 -0
  5. data/README.rdoc +5 -5
  6. data/lib/minitest/rails_plugin.rb +63 -0
  7. data/lib/rails/api/task.rb +35 -4
  8. data/lib/rails/app_updater.rb +1 -1
  9. data/lib/rails/application/bootstrap.rb +23 -4
  10. data/lib/rails/application/configuration.rb +190 -70
  11. data/lib/rails/application/default_middleware_stack.rb +8 -2
  12. data/lib/rails/application/dummy_config.rb +19 -0
  13. data/lib/rails/application/finisher.rb +43 -33
  14. data/lib/rails/application.rb +159 -59
  15. data/lib/rails/application_controller.rb +1 -1
  16. data/lib/rails/backtrace_cleaner.rb +1 -1
  17. data/lib/rails/cli.rb +5 -2
  18. data/lib/rails/command/actions.rb +10 -12
  19. data/lib/rails/command/base.rb +55 -53
  20. data/lib/rails/command/environment_argument.rb +32 -16
  21. data/lib/rails/command/helpers/editor.rb +17 -12
  22. data/lib/rails/command.rb +84 -33
  23. data/lib/rails/commands/about/about_command.rb +14 -0
  24. data/lib/rails/commands/application/application_command.rb +2 -0
  25. data/lib/rails/commands/console/console_command.rb +17 -13
  26. data/lib/rails/commands/credentials/USAGE +53 -55
  27. data/lib/rails/commands/credentials/credentials_command/diffing.rb +5 -3
  28. data/lib/rails/commands/credentials/credentials_command.rb +64 -70
  29. data/lib/rails/commands/db/system/change/change_command.rb +2 -1
  30. data/lib/rails/commands/dbconsole/dbconsole_command.rb +25 -115
  31. data/lib/rails/commands/destroy/destroy_command.rb +3 -2
  32. data/lib/rails/commands/dev/dev_command.rb +1 -6
  33. data/lib/rails/commands/encrypted/USAGE +15 -20
  34. data/lib/rails/commands/encrypted/encrypted_command.rb +46 -35
  35. data/lib/rails/commands/gem_help/USAGE +16 -0
  36. data/lib/rails/commands/gem_help/gem_help_command.rb +13 -0
  37. data/lib/rails/commands/generate/generate_command.rb +2 -2
  38. data/lib/rails/commands/help/USAGE +13 -13
  39. data/lib/rails/commands/help/help_command.rb +21 -2
  40. data/lib/rails/commands/initializers/initializers_command.rb +1 -4
  41. data/lib/rails/commands/middleware/middleware_command.rb +17 -0
  42. data/lib/rails/commands/new/new_command.rb +2 -0
  43. data/lib/rails/commands/notes/notes_command.rb +2 -1
  44. data/lib/rails/commands/plugin/plugin_command.rb +2 -0
  45. data/lib/rails/commands/rake/rake_command.rb +25 -22
  46. data/lib/rails/commands/restart/restart_command.rb +14 -0
  47. data/lib/rails/commands/routes/routes_command.rb +13 -1
  48. data/lib/rails/commands/runner/USAGE +14 -12
  49. data/lib/rails/commands/runner/runner_command.rb +32 -20
  50. data/lib/rails/commands/secret/secret_command.rb +13 -0
  51. data/lib/rails/commands/secrets/USAGE +44 -49
  52. data/lib/rails/commands/secrets/secrets_command.rb +20 -38
  53. data/lib/rails/commands/server/server_command.rb +33 -32
  54. data/lib/rails/commands/test/USAGE +14 -0
  55. data/lib/rails/commands/test/test_command.rb +56 -14
  56. data/lib/rails/commands/unused_routes/unused_routes_command.rb +75 -0
  57. data/lib/rails/commands/version/version_command.rb +1 -0
  58. data/lib/rails/configuration.rb +5 -5
  59. data/lib/rails/console/app.rb +1 -4
  60. data/lib/rails/deprecator.rb +7 -0
  61. data/lib/rails/engine/configuration.rb +50 -6
  62. data/lib/rails/engine.rb +49 -21
  63. data/lib/rails/gem_version.rb +4 -4
  64. data/lib/rails/generators/actions.rb +178 -59
  65. data/lib/rails/generators/active_model.rb +28 -14
  66. data/lib/rails/generators/app_base.rb +355 -82
  67. data/lib/rails/generators/app_name.rb +3 -14
  68. data/lib/rails/generators/base.rb +17 -9
  69. data/lib/rails/generators/database.rb +39 -1
  70. data/lib/rails/generators/erb/mailer/templates/layout.html.erb.tt +1 -1
  71. data/lib/rails/generators/generated_attribute.rb +12 -0
  72. data/lib/rails/generators/migration.rb +1 -2
  73. data/lib/rails/generators/model_helpers.rb +2 -1
  74. data/lib/rails/generators/rails/app/USAGE +22 -6
  75. data/lib/rails/generators/rails/app/app_generator.rb +83 -62
  76. data/lib/rails/generators/rails/app/templates/Dockerfile.tt +103 -0
  77. data/lib/rails/generators/rails/app/templates/Gemfile.tt +10 -10
  78. data/lib/rails/generators/rails/app/templates/app/views/layouts/mailer.html.erb.tt +1 -1
  79. data/lib/rails/generators/rails/app/templates/bin/setup.tt +10 -1
  80. data/lib/rails/generators/rails/app/templates/config/application.rb.tt +6 -17
  81. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml.tt +4 -4
  82. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml.tt +3 -3
  83. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt +4 -6
  84. data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml.tt +3 -3
  85. data/lib/rails/generators/rails/app/templates/config/databases/trilogy.yml.tt +59 -0
  86. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +11 -1
  87. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +31 -27
  88. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +12 -8
  89. data/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt +2 -0
  90. data/lib/rails/generators/rails/app/templates/config/initializers/content_security_policy.rb.tt +2 -2
  91. data/lib/rails/generators/rails/app/templates/config/initializers/cors.rb.tt +1 -1
  92. data/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb.tt +3 -3
  93. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_1.rb.tt +284 -0
  94. data/lib/rails/generators/rails/app/templates/config/initializers/permissions_policy.rb.tt +11 -9
  95. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +11 -13
  96. data/lib/rails/generators/rails/app/templates/config/puma.rb.tt +11 -19
  97. data/lib/rails/generators/rails/app/templates/config/routes.rb.tt +5 -1
  98. data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +6 -4
  99. data/lib/rails/generators/rails/app/templates/docker-entrypoint.tt +10 -0
  100. data/lib/rails/generators/rails/app/templates/dockerignore.tt +43 -0
  101. data/lib/rails/generators/rails/app/templates/gitignore.tt +4 -8
  102. data/lib/rails/generators/rails/app/templates/node-version.tt +1 -0
  103. data/lib/rails/generators/rails/app/templates/test/channels/application_cable/connection_test.rb.tt +10 -8
  104. data/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt +9 -7
  105. data/lib/rails/generators/rails/application_record/application_record_generator.rb +4 -0
  106. data/lib/rails/generators/rails/benchmark/benchmark_generator.rb +2 -1
  107. data/lib/rails/generators/rails/controller/USAGE +12 -4
  108. data/lib/rails/generators/rails/controller/controller_generator.rb +5 -0
  109. data/lib/rails/generators/rails/controller/templates/controller.rb.tt +1 -1
  110. data/lib/rails/generators/rails/credentials/credentials_generator.rb +29 -24
  111. data/lib/rails/generators/rails/credentials/templates/credentials.yml.tt +8 -0
  112. data/lib/rails/generators/rails/db/system/change/change_generator.rb +30 -0
  113. data/lib/rails/generators/rails/encryption_key_file/encryption_key_file_generator.rb +1 -2
  114. data/lib/rails/generators/rails/migration/USAGE +21 -11
  115. data/lib/rails/generators/rails/model/model_generator.rb +4 -0
  116. data/lib/rails/generators/rails/plugin/USAGE +17 -6
  117. data/lib/rails/generators/rails/plugin/plugin_generator.rb +4 -13
  118. data/lib/rails/generators/rails/plugin/templates/%name%.gemspec.tt +2 -2
  119. data/lib/rails/generators/rails/plugin/templates/Gemfile.tt +2 -2
  120. data/lib/rails/generators/rails/plugin/templates/MIT-LICENSE.tt +1 -1
  121. data/lib/rails/generators/rails/plugin/templates/bin/rails.tt +1 -15
  122. data/lib/rails/generators/rails/plugin/templates/gitignore.tt +0 -2
  123. data/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt +4 -4
  124. data/lib/rails/generators/rails/resource/resource_generator.rb +6 -0
  125. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +2 -1
  126. data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +1 -1
  127. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt +2 -2
  128. data/lib/rails/generators/test_case.rb +2 -2
  129. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +1 -1
  130. data/lib/rails/generators/testing/{behaviour.rb → behavior.rb} +5 -2
  131. data/lib/rails/generators.rb +6 -14
  132. data/lib/rails/health_controller.rb +55 -0
  133. data/lib/rails/info.rb +1 -1
  134. data/lib/rails/info_controller.rb +31 -11
  135. data/lib/rails/mailers_controller.rb +15 -5
  136. data/lib/rails/paths.rb +13 -10
  137. data/lib/rails/rack/logger.rb +15 -12
  138. data/lib/rails/rackup/server.rb +15 -0
  139. data/lib/rails/railtie/configuration.rb +14 -1
  140. data/lib/rails/railtie.rb +18 -18
  141. data/lib/rails/ruby_version_check.rb +2 -0
  142. data/lib/rails/secrets.rb +10 -8
  143. data/lib/rails/source_annotation_extractor.rb +67 -18
  144. data/lib/rails/tasks/engine.rake +8 -8
  145. data/lib/rails/tasks/framework.rake +4 -10
  146. data/lib/rails/tasks/log.rake +1 -1
  147. data/lib/rails/tasks/misc.rake +3 -14
  148. data/lib/rails/tasks/statistics.rake +5 -4
  149. data/lib/rails/tasks/tmp.rake +5 -5
  150. data/lib/rails/tasks/zeitwerk.rake +15 -35
  151. data/lib/rails/tasks.rb +0 -2
  152. data/lib/rails/templates/rails/mailers/email.html.erb +32 -0
  153. data/lib/rails/templates/rails/mailers/index.html.erb +14 -7
  154. data/lib/rails/templates/rails/mailers/mailer.html.erb +11 -5
  155. data/lib/rails/templates/rails/welcome/index.html.erb +1 -0
  156. data/lib/rails/test_help.rb +13 -14
  157. data/lib/rails/test_unit/line_filtering.rb +1 -1
  158. data/lib/rails/test_unit/reporter.rb +6 -2
  159. data/lib/rails/test_unit/runner.rb +36 -18
  160. data/lib/rails/test_unit/test_parser.rb +88 -0
  161. data/lib/rails/test_unit/testing.rake +13 -33
  162. data/lib/rails/testing/maintain_test_schema.rb +16 -0
  163. data/lib/rails/version.rb +1 -1
  164. data/lib/rails/zeitwerk_checker.rb +15 -0
  165. data/lib/rails.rb +15 -15
  166. metadata +66 -29
  167. data/RDOC_MAIN.rdoc +0 -97
  168. data/lib/rails/application/dummy_erb_compiler.rb +0 -18
  169. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_0.rb.tt +0 -135
  170. data/lib/rails/generators/rails/model/USAGE +0 -113
  171. data/lib/rails/tasks/middleware.rake +0 -9
  172. data/lib/rails/tasks/restart.rake +0 -9
@@ -9,31 +9,47 @@ module Rails
9
9
  extend ActiveSupport::Concern
10
10
 
11
11
  included do
12
- no_commands do
13
- class_attribute :environment_desc, default: "Specifies the environment to run this #{self.command_name} under (test/development/production)."
12
+ class_option :environment, aliases: "-e", type: :string,
13
+ desc: "The environment to run `#{self.command_name}` in (e.g. test / development / production)."
14
+ end
15
+
16
+ def initialize(...)
17
+ super
18
+
19
+ @environment_specified = options[:environment].present?
20
+
21
+ if !@environment_specified
22
+ self.options = options.merge(environment: Rails::Command.environment)
23
+ elsif !available_environments.include?(options[:environment])
24
+ self.options = options.merge(environment: expand_environment_name(options[:environment]))
14
25
  end
15
- class_option :environment, aliases: "-e", type: :string, desc: environment_desc
16
26
  end
17
27
 
18
28
  private
19
- def extract_environment_option_from_argument(default_environment: Rails::Command.environment)
20
- if options[:environment]
21
- self.options = options.merge(environment: acceptable_environment(options[:environment]))
22
- else
23
- self.options = options.merge(environment: default_environment)
24
- end
29
+ def require_application!
30
+ ENV["RAILS_ENV"] = environment
31
+ super
32
+ end
33
+
34
+ def environment
35
+ @environment ||= options[:environment]
36
+ end
37
+
38
+ def environment=(environment)
39
+ @environment = environment
25
40
  end
26
41
 
27
- def acceptable_environment(env = nil)
28
- if available_environments.include? env
29
- env
30
- else
31
- %w( production development test ).detect { |e| /^#{env}/.match?(e) } || env
32
- end
42
+ def environment_specified?
43
+ @environment_specified
33
44
  end
34
45
 
35
46
  def available_environments
36
- Dir["config/environments/*.rb"].map { |fname| File.basename(fname, ".*") }
47
+ @available_environments ||=
48
+ Dir["config/environments/*.rb"].map { |filename| File.basename(filename, ".*") }
49
+ end
50
+
51
+ def expand_environment_name(name)
52
+ %w[production development test].find { |full_name| full_name.start_with?(name) } || name
37
53
  end
38
54
  end
39
55
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "shellwords"
3
4
  require "active_support/encrypted_file"
4
5
 
5
6
  module Rails
@@ -7,27 +8,31 @@ module Rails
7
8
  module Helpers
8
9
  module Editor
9
10
  private
10
- def ensure_editor_available(command:)
11
- if ENV["EDITOR"].to_s.empty?
12
- say "No $EDITOR to open file in. Assign one like this:"
11
+ def editor
12
+ ENV["VISUAL"].to_s.empty? ? ENV["EDITOR"] : ENV["VISUAL"]
13
+ end
14
+
15
+ def display_hint_if_system_editor_not_specified
16
+ if editor.to_s.empty?
17
+ say "No $VISUAL or $EDITOR to open file in. Assign one like this:"
13
18
  say ""
14
- say %(EDITOR="mate --wait" #{command})
19
+ say %(VISUAL="mate --wait" #{executable(current_subcommand)})
15
20
  say ""
16
- say "For editors that fork and exit immediately, it's important to pass a wait flag,"
17
- say "otherwise the credentials will be saved immediately with no chance to edit."
21
+ say "For editors that fork and exit immediately, it's important to pass a wait flag;"
22
+ say "otherwise, the file will be saved immediately with no chance to edit."
18
23
 
19
- false
20
- else
21
24
  true
22
25
  end
23
26
  end
24
27
 
25
- def catch_editing_exceptions
26
- yield
28
+ def system_editor(file_path)
29
+ system(*Shellwords.split(editor), file_path.to_s)
30
+ end
31
+
32
+ def using_system_editor
33
+ display_hint_if_system_editor_not_specified || yield
27
34
  rescue Interrupt
28
35
  say "Aborted changing file: nothing saved."
29
- rescue ActiveSupport::EncryptedFile::MissingKeyError => error
30
- say error.message
31
36
  end
32
37
  end
33
38
  end
data/lib/rails/command.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "active_support"
4
4
  require "active_support/core_ext/enumerable"
5
5
  require "active_support/core_ext/object/blank"
6
+ require "rails/deprecator"
6
7
 
7
8
  require "thor"
8
9
 
@@ -13,9 +14,40 @@ module Rails
13
14
  autoload :Behavior
14
15
  autoload :Base
15
16
 
17
+ class CorrectableNameError < StandardError # :nodoc:
18
+ attr_reader :name
19
+
20
+ def initialize(message, name, alternatives)
21
+ @name = name
22
+ @alternatives = alternatives
23
+ super(message)
24
+ end
25
+
26
+ if !Exception.method_defined?(:detailed_message)
27
+ def detailed_message(...)
28
+ message
29
+ end
30
+ end
31
+
32
+ if defined?(DidYouMean::Correctable) && defined?(DidYouMean::SpellChecker)
33
+ include DidYouMean::Correctable
34
+
35
+ def corrections
36
+ @corrections ||= DidYouMean::SpellChecker.new(dictionary: @alternatives).correct(name)
37
+ end
38
+ end
39
+ end
40
+
41
+ class UnrecognizedCommandError < CorrectableNameError # :nodoc:
42
+ def initialize(name)
43
+ super("Unrecognized command #{name.inspect}", name, Command.printing_commands.map(&:first))
44
+ end
45
+ end
46
+
16
47
  include Behavior
17
48
 
18
- HELP_MAPPINGS = %w(-h -? --help)
49
+ HELP_MAPPINGS = %w(-h -? --help).to_set
50
+ VERSION_MAPPINGS = %w(-v --version).to_set
19
51
 
20
52
  class << self
21
53
  def hidden_commands # :nodoc:
@@ -28,30 +60,26 @@ module Rails
28
60
 
29
61
  # Receives a namespace, arguments, and the behavior to invoke the command.
30
62
  def invoke(full_namespace, args = [], **config)
31
- namespace = full_namespace = full_namespace.to_s
32
-
33
- if char = namespace =~ /:(\w+)$/
34
- command_name, namespace = $1, namespace.slice(0, char)
35
- else
36
- command_name = namespace
37
- end
38
-
39
- command_name, namespace = "help", "help" if command_name.blank? || HELP_MAPPINGS.include?(command_name)
40
- command_name, namespace, args = "application", "application", ["--help"] if rails_new_with_no_path?(args)
41
- command_name, namespace = "version", "version" if %w( -v --version ).include?(command_name)
42
-
43
- original_argv = ARGV.dup
44
- ARGV.replace(args)
63
+ args = ["--help"] if rails_new_with_no_path?(args)
45
64
 
65
+ full_namespace = full_namespace.to_s
66
+ namespace, command_name = split_namespace(full_namespace)
46
67
  command = find_by_namespace(namespace, command_name)
47
- if command && command.all_commands[command_name]
48
- command.perform(command_name, args, config)
68
+
69
+ with_argv(args) do
70
+ if command && command.all_commands[command_name]
71
+ command.perform(command_name, args, config)
72
+ else
73
+ invoke_rake(full_namespace, args, config)
74
+ end
75
+ end
76
+ rescue UnrecognizedCommandError => error
77
+ if error.name == full_namespace && command && command_name == full_namespace
78
+ command.perform("help", [], config)
49
79
  else
50
- args = ["--describe", full_namespace] if HELP_MAPPINGS.include?(args[0])
51
- find_by_namespace("rake").perform(full_namespace, args, config)
80
+ puts error.detailed_message
52
81
  end
53
- ensure
54
- ARGV.replace(original_argv)
82
+ exit(1)
55
83
  end
56
84
 
57
85
  # Rails finds namespaces similar to Thor, it only adds one rule:
@@ -76,33 +104,56 @@ module Rails
76
104
  namespaces[(lookups & namespaces.keys).first]
77
105
  end
78
106
 
79
- # Returns the root of the Rails engine or app running the command.
107
+ # Returns the root of the \Rails engine or app running the command.
80
108
  def root
81
109
  if defined?(ENGINE_ROOT)
82
110
  Pathname.new(ENGINE_ROOT)
83
- elsif defined?(APP_PATH)
84
- Pathname.new(File.expand_path("../..", APP_PATH))
111
+ else
112
+ application_root
85
113
  end
86
114
  end
87
115
 
88
- def print_commands # :nodoc:
89
- commands.each { |command| puts(" #{command}") }
116
+ def application_root # :nodoc:
117
+ Pathname.new(File.expand_path("../..", APP_PATH)) if defined?(APP_PATH)
90
118
  end
91
119
 
92
- private
93
- COMMANDS_IN_USAGE = %w(generate console server test test:system dbconsole new)
94
- private_constant :COMMANDS_IN_USAGE
120
+ def printing_commands # :nodoc:
121
+ lookup!
122
+
123
+ (subclasses - hidden_commands).flat_map(&:printing_commands)
124
+ end
95
125
 
126
+ private
96
127
  def rails_new_with_no_path?(args)
97
128
  args == ["new"]
98
129
  end
99
130
 
100
- def commands
101
- lookup!
131
+ def split_namespace(namespace)
132
+ case namespace
133
+ when /^(.+):(\w+)$/
134
+ [$1, $2]
135
+ when ""
136
+ ["help", "help"]
137
+ when HELP_MAPPINGS, "help"
138
+ ["help", "help_extended"]
139
+ when VERSION_MAPPINGS
140
+ ["version", "version"]
141
+ else
142
+ [namespace, namespace]
143
+ end
144
+ end
102
145
 
103
- visible_commands = (subclasses - hidden_commands).flat_map(&:printing_commands)
146
+ def with_argv(argv)
147
+ original_argv = ARGV.dup
148
+ ARGV.replace(argv)
149
+ yield
150
+ ensure
151
+ ARGV.replace(original_argv)
152
+ end
104
153
 
105
- (visible_commands - COMMANDS_IN_USAGE).sort
154
+ def invoke_rake(task, args, config)
155
+ args = ["--describe", task] if HELP_MAPPINGS.include?(args[0])
156
+ find_by_namespace("rake").perform(task, args, config)
106
157
  end
107
158
 
108
159
  def command_type # :doc:
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Command
5
+ class AboutCommand < Base # :nodoc:
6
+ desc "about", "List versions of all Rails frameworks and the environment"
7
+ def perform
8
+ boot_application!
9
+
10
+ say Rails::Info
11
+ end
12
+ end
13
+ end
14
+ end
@@ -18,6 +18,8 @@ module Rails
18
18
  class ApplicationCommand < Base # :nodoc:
19
19
  hide_command!
20
20
 
21
+ self.bin = "rails"
22
+
21
23
  def help
22
24
  perform # Punt help output to the generator.
23
25
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "irb"
4
- require "irb/completion"
5
-
6
3
  require "rails/command/environment_argument"
7
4
 
8
5
  module Rails
@@ -34,15 +31,26 @@ module Rails
34
31
 
35
32
  app.load_console
36
33
 
37
- @console = app.config.console || IRB
34
+ @console = app.config.console || begin
35
+ require "irb"
36
+ require "irb/completion"
38
37
 
39
- if @console == IRB
40
38
  IRB::WorkSpace.prepend(BacktraceCleaner)
39
+
40
+ if !Rails.env.local?
41
+ ENV["IRB_USE_AUTOCOMPLETE"] ||= "false"
42
+ end
43
+
44
+ IRB
41
45
  end
42
46
  end
43
47
 
44
48
  def sandbox?
45
- options[:sandbox]
49
+ return options[:sandbox] if !options[:sandbox].nil?
50
+
51
+ return false if Rails.env.local?
52
+
53
+ app.config.sandbox_by_default
46
54
  end
47
55
 
48
56
  def environment
@@ -75,7 +83,7 @@ module Rails
75
83
  class ConsoleCommand < Base # :nodoc:
76
84
  include EnvironmentArgument
77
85
 
78
- class_option :sandbox, aliases: "-s", type: :boolean, default: false,
86
+ class_option :sandbox, aliases: "-s", type: :boolean, default: nil,
79
87
  desc: "Rollback database modifications on exit."
80
88
 
81
89
  def initialize(args = [], local_options = {}, config = {})
@@ -92,13 +100,9 @@ module Rails
92
100
  super(args, local_options, config)
93
101
  end
94
102
 
103
+ desc "console", "Start the Rails console"
95
104
  def perform
96
- extract_environment_option_from_argument
97
-
98
- # RAILS_ENV needs to be set before config/application is required.
99
- ENV["RAILS_ENV"] = options[:environment]
100
-
101
- require_application_and_environment!
105
+ boot_application!
102
106
  Rails::Console.start(Rails.application, options)
103
107
  end
104
108
  end
@@ -1,75 +1,73 @@
1
- === Storing Encrypted Credentials in Source Control
1
+ Description:
2
+ The Rails `credentials` commands provide access to encrypted credentials,
3
+ so you can safely store access tokens, database passwords, and the like
4
+ safely inside the app without relying on a mess of ENVs.
2
5
 
3
- The Rails `credentials` commands provide access to encrypted credentials,
4
- so you can safely store access tokens, database passwords, and the like
5
- safely inside the app without relying on a mess of ENVs.
6
+ This also allows for atomic deploys: no need to coordinate key changes
7
+ to get everything working as the keys are shipped with the code.
6
8
 
7
- This also allows for atomic deploys: no need to coordinate key changes
8
- to get everything working as the keys are shipped with the code.
9
+ Setup:
10
+ Applications after Rails 5.2 automatically have a basic credentials file generated
11
+ that just contains the secret_key_base used by MessageVerifiers/MessageEncryptors, like the ones
12
+ signing and encrypting cookies.
9
13
 
10
- === Setup
14
+ For applications created prior to Rails 5.2, we'll automatically generate a new
15
+ credentials file in `config/credentials.yml.enc` the first time you run `<%= executable(:edit) %>`.
16
+ If you didn't have a master key saved in `config/master.key`, that'll be created too.
11
17
 
12
- Applications after Rails 5.2 automatically have a basic credentials file generated
13
- that just contains the secret_key_base used by MessageVerifiers/MessageEncryptors, like the ones
14
- signing and encrypting cookies.
18
+ Don't lose this master key! Put it in a password manager your team can access.
19
+ Should you lose it no one, including you, will be able to access any encrypted
20
+ credentials.
15
21
 
16
- For applications created prior to Rails 5.2, we'll automatically generate a new
17
- credentials file in `config/credentials.yml.enc` the first time you run `bin/rails credentials:edit`.
18
- If you didn't have a master key saved in `config/master.key`, that'll be created too.
22
+ Don't commit the key! Add `config/master.key` to your source control's
23
+ ignore file. If you use Git, Rails handles this for you.
19
24
 
20
- Don't lose this master key! Put it in a password manager your team can access.
21
- Should you lose it no one, including you, will be able to access any encrypted
22
- credentials.
25
+ Rails also looks for the master key in `ENV["RAILS_MASTER_KEY"]`, in case that
26
+ is easier to manage. You could set `RAILS_MASTER_KEY` in a deployment
27
+ configuration, or you could prepend it to your server's start command like so:
23
28
 
24
- Don't commit the key! Add `config/master.key` to your source control's
25
- ignore file. If you use Git, Rails handles this for you.
29
+ RAILS_MASTER_KEY="very-secret-and-secure" bin/rails server
26
30
 
27
- Rails also looks for the master key in `ENV["RAILS_MASTER_KEY"]`, if that's easier to manage.
31
+ If `ENV["RAILS_MASTER_KEY"]` is present, it takes precedence over
32
+ `config/master.key`.
28
33
 
29
- You could prepend that to your server's start command like this:
34
+ Set up Git to Diff Credentials:
35
+ Rails provides `<%= executable(:diff) %> --enroll` to instruct Git to call
36
+ `<%= executable(:diff) %>` when `git diff` is run on a credentials file.
30
37
 
31
- RAILS_MASTER_KEY="very-secret-and-secure" server.start
38
+ Running the command enrolls the project such that all credentials files use the
39
+ "rails_credentials" diff driver in .gitattributes.
32
40
 
33
- === Set up Git to Diff Credentials
41
+ Additionally since Git requires the driver itself to be set up in a config file
42
+ that isn't tracked Rails automatically ensures it's configured when running
43
+ `<%= executable(:edit) %>`.
34
44
 
35
- Rails provides `bin/rails credentials:diff --enroll` to instruct Git to call
36
- `bin/rails credentials:diff` when `git diff` is run on a credentials file.
45
+ Otherwise each co-worker would have to run enable manually, including on each new
46
+ repo clone.
37
47
 
38
- Running the command enrolls the project such that all credentials files use the
39
- "rails_credentials" diff driver in .gitattributes.
48
+ To disenroll from this feature, run `<%= executable(:diff) %> --disenroll`.
40
49
 
41
- Additionally since Git requires the driver itself to be set up in a config file
42
- that isn't tracked Rails automatically ensures it's configured when running
43
- `credentials:edit`.
50
+ Editing Credentials:
51
+ This will open a temporary file in `$VISUAL` or `$EDITOR` with the decrypted
52
+ contents to edit the encrypted credentials.
44
53
 
45
- Otherwise each co-worker would have to run enable manually, including on each new
46
- repo clone.
54
+ When the temporary file is next saved the contents are encrypted and written to
55
+ `config/credentials.yml.enc` while the file itself is destroyed to prevent credentials
56
+ from leaking.
47
57
 
48
- To disenroll from this feature, run `bin/rails credentials:diff --disenroll`.
58
+ Environment Specific Credentials:
59
+ The `credentials` command supports passing an `--environment` option to create an
60
+ environment specific override. That override will take precedence over the
61
+ global `config/credentials.yml.enc` file when running in that environment. So:
49
62
 
50
- === Editing Credentials
63
+ <%= executable(:edit) %> --environment development
51
64
 
52
- This will open a temporary file in `$EDITOR` with the decrypted contents to edit
53
- the encrypted credentials.
65
+ will create `config/credentials/development.yml.enc` with the corresponding
66
+ encryption key in `config/credentials/development.key` if the credentials file
67
+ doesn't exist.
54
68
 
55
- When the temporary file is next saved the contents are encrypted and written to
56
- `config/credentials.yml.enc` while the file itself is destroyed to prevent credentials
57
- from leaking.
69
+ In addition to that, the default credentials lookup paths can be overridden through
70
+ `config.credentials.content_path` and `config.credentials.key_path`.
58
71
 
59
- === Environment Specific Credentials
60
-
61
- The `credentials` command supports passing an `--environment` option to create an
62
- environment specific override. That override will take precedence over the
63
- global `config/credentials.yml.enc` file when running in that environment. So:
64
-
65
- bin/rails credentials:edit --environment development
66
-
67
- will create `config/credentials/development.yml.enc` with the corresponding
68
- encryption key in `config/credentials/development.key` if the credentials file
69
- doesn't exist.
70
-
71
- The encryption key can also be put in `ENV["RAILS_MASTER_KEY"]`, which takes
72
- precedence over the file encryption key.
73
-
74
- In addition to that, the default credentials lookup paths can be overridden through
75
- `config.credentials.content_path` and `config.credentials.key_path`.
72
+ Just as with `config/master.key`, `ENV["RAILS_MASTER_KEY"]` takes precedence
73
+ over any environment specific or specially configured key files.
@@ -13,7 +13,8 @@ module Rails::Command::CredentialsCommand::Diffing # :nodoc:
13
13
  gitattributes.write(GITATTRIBUTES_ENTRY, mode: "a")
14
14
 
15
15
  say "Enrolled project in credentials file diffing!"
16
- say "Rails ensures the rails_credentials diff driver is set when running `credentials:edit`. See `credentials:help` for more."
16
+ say ""
17
+ say "Rails will configure the Git diff driver for credentials when running `#{executable(:edit)}`. See `#{executable(:help)}` for more information."
17
18
  end
18
19
  end
19
20
 
@@ -42,10 +43,11 @@ module Rails::Command::CredentialsCommand::Diffing # :nodoc:
42
43
  end
43
44
 
44
45
  def configure_diffing_driver
45
- system "git config diff.rails_credentials.textconv 'bin/rails credentials:diff'"
46
+ system "git config diff.rails_credentials.textconv '#{executable(:diff)}'"
47
+ say "Configured Git diff driver for credentials."
46
48
  end
47
49
 
48
50
  def gitattributes
49
- Rails.root.join(".gitattributes")
51
+ @gitattributes ||= (Rails::Command.root || Pathname.pwd).join(".gitattributes")
50
52
  end
51
53
  end