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
@@ -1,17 +1,17 @@
1
- The most common rails commands are:
2
- generate Generate new code (short-cut alias: "g")
3
- console Start the Rails console (short-cut alias: "c")
4
- server Start the Rails server (short-cut alias: "s")
5
- test Run tests except system tests (short-cut alias: "t")
6
- test:system Run system tests
7
- dbconsole Start a console for the database specified in config/database.yml
8
- (short-cut alias: "db")
1
+ Usage:
2
+ bin/rails COMMAND [options]
3
+
4
+ You must specify a command. The most common commands are:
5
+
6
+ generate Generate new code (short-cut alias: "g")
7
+ console Start the Rails console (short-cut alias: "c")
8
+ server Start the Rails server (short-cut alias: "s")
9
+ test Run tests except system tests (short-cut alias: "t")
10
+ test:system Run system tests
11
+ dbconsole Start a console for the database specified in config/database.yml
12
+ (short-cut alias: "db")
9
13
  <% unless engine? -%>
10
- new Create a new Rails application. "rails new my_app" creates a
11
- new application called MyApp in "./my_app"
12
- plugin new Create a new Rails railtie or engine
14
+ plugin new Create a new Rails railtie or engine
13
15
  <% end -%>
14
16
 
15
17
  All commands can be run with -h (or --help) for more information.
16
- In addition to those commands, there are:
17
-
@@ -6,10 +6,29 @@ module Rails
6
6
  hide_command!
7
7
 
8
8
  def help(*)
9
- say self.class.desc
9
+ say self.class.class_usage
10
+ end
11
+
12
+ def help_extended(*)
13
+ help
14
+
15
+ say ""
16
+ say "In addition to those commands, there are:"
17
+ say ""
10
18
 
11
- Rails::Command.print_commands
19
+ extended_commands = printing_commands_not_in_usage.sort_by(&:first)
20
+ print_table(extended_commands, truncate: true)
12
21
  end
22
+
23
+ private
24
+ COMMANDS_IN_USAGE = %w(generate console server test test:system dbconsole new)
25
+ private_constant :COMMANDS_IN_USAGE
26
+
27
+ def printing_commands_not_in_usage # :nodoc:
28
+ Rails::Command.printing_commands.reject do |command, _|
29
+ command.in?(COMMANDS_IN_USAGE)
30
+ end
31
+ end
13
32
  end
14
33
  end
15
34
  end
@@ -9,10 +9,7 @@ module Rails
9
9
 
10
10
  desc "initializers", "Print out all defined initializers in the order they are invoked by Rails."
11
11
  def perform
12
- extract_environment_option_from_argument
13
- ENV["RAILS_ENV"] = options[:environment]
14
-
15
- require_application_and_environment!
12
+ boot_application!
16
13
 
17
14
  Rails.application.initializers.tsort_each do |initializer|
18
15
  say "#{initializer.context_class}.#{initializer.name}"
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Command
5
+ class MiddlewareCommand < Base # :nodoc:
6
+ desc "middleware", "Print out your Rack middleware stack"
7
+ def perform
8
+ boot_application!
9
+
10
+ Rails.configuration.middleware.each do |middleware|
11
+ say "use #{middleware.inspect}"
12
+ end
13
+ say "run #{Rails.application.class.name}.routes"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -3,6 +3,8 @@
3
3
  module Rails
4
4
  module Command
5
5
  class NewCommand < Base # :nodoc:
6
+ self.bin = "rails"
7
+
6
8
  no_commands do
7
9
  def help
8
10
  Rails::Command.invoke :application, [ "--help" ]
@@ -7,8 +7,9 @@ module Rails
7
7
  class NotesCommand < Base # :nodoc:
8
8
  class_option :annotations, aliases: "-a", desc: "Filter by specific annotations, e.g. Foobar TODO", type: :array
9
9
 
10
+ desc "notes", "Show comments in your code annotated with FIXME, OPTIMIZE, and TODO"
10
11
  def perform(*)
11
- require_application_and_environment!
12
+ boot_application!
12
13
 
13
14
  display_annotations
14
15
  end
@@ -5,6 +5,8 @@ module Rails
5
5
  class PluginCommand < Base # :nodoc:
6
6
  hide_command!
7
7
 
8
+ self.bin = "rails"
9
+
8
10
  def help
9
11
  run_plugin_generator %w( --help )
10
12
  end
@@ -9,42 +9,45 @@ module Rails
9
9
 
10
10
  class << self
11
11
  def printing_commands
12
- formatted_rake_tasks.map(&:first)
12
+ rake_tasks.filter_map do |task|
13
+ if task.comment && task.locations.any?(non_app_file_pattern)
14
+ [task.name_with_args, task.comment]
15
+ end
16
+ end
13
17
  end
14
18
 
15
19
  def perform(task, args, config)
16
- require_rake
17
-
18
- Rake.with_application do |rake|
19
- rake.init("rails", [task, *args])
20
- rake.load_rakefile
21
- if Rails.respond_to?(:root)
22
- rake.options.suppress_backtrace_pattern = /\A(?!#{Regexp.quote(Rails.root.to_s)})/
20
+ with_rake(task, *args) do |rake|
21
+ if unrecognized_task = rake.top_level_tasks.find { |task| !rake.lookup(task[/[^\[]+/]) }
22
+ @rake_tasks = rake.tasks
23
+ raise UnrecognizedCommandError.new(unrecognized_task)
23
24
  end
25
+
26
+ rake.options.suppress_backtrace_pattern = non_app_file_pattern
24
27
  rake.standard_exception_handling { rake.top_level }
25
28
  end
26
29
  end
27
30
 
28
31
  private
29
- def rake_tasks
30
- require_rake
31
-
32
- return @rake_tasks if defined?(@rake_tasks)
33
-
34
- require_application!
32
+ def non_app_file_pattern
33
+ /\A(?!#{Regexp.quote Rails::Command.root.to_s})/
34
+ end
35
35
 
36
+ def with_rake(*args, &block)
37
+ require "rake"
36
38
  Rake::TaskManager.record_task_metadata = true
37
- Rake.application.instance_variable_set(:@name, "rails")
38
- load_tasks
39
- @rake_tasks = Rake.application.tasks.select(&:comment)
40
- end
41
39
 
42
- def formatted_rake_tasks
43
- rake_tasks.map { |t| [ t.name_with_args, t.comment ] }
40
+ result = nil
41
+ Rake.with_application do |rake|
42
+ rake.init(bin, args) unless args.empty?
43
+ rake.load_rakefile
44
+ result = block.call(rake)
45
+ end
46
+ result
44
47
  end
45
48
 
46
- def require_rake
47
- require "rake" # Defer booting Rake until we know it's needed.
49
+ def rake_tasks
50
+ @rake_tasks ||= with_rake(&:tasks)
48
51
  end
49
52
  end
50
53
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Command
5
+ class RestartCommand < Base # :nodoc:
6
+ desc "restart", "Restart app by touching tmp/restart.txt"
7
+ def perform
8
+ require "fileutils"
9
+ FileUtils.mkdir_p Rails::Command.application_root.join("tmp")
10
+ FileUtils.touch Rails::Command.application_root.join("tmp/restart.txt")
11
+ end
12
+ end
13
+ end
14
+ end
@@ -8,9 +8,21 @@ module Rails
8
8
  class_option :controller, aliases: "-c", desc: "Filter by a specific controller, e.g. PostsController or Admin::PostsController."
9
9
  class_option :grep, aliases: "-g", desc: "Grep routes by a specific pattern."
10
10
  class_option :expanded, type: :boolean, aliases: "-E", desc: "Print routes expanded vertically with parts explained."
11
+ class_option :unused, type: :boolean, aliases: "-u", desc: "Print unused routes."
11
12
 
13
+ no_commands do
14
+ def invoke_command(*)
15
+ if options.key?("unused")
16
+ Rails::Command.invoke "unused_routes", ARGV
17
+ else
18
+ super
19
+ end
20
+ end
21
+ end
22
+
23
+ desc "routes", "List all the defined routes"
12
24
  def perform(*)
13
- require_application_and_environment!
25
+ boot_application!
14
26
  require "action_dispatch/routing/inspector"
15
27
 
16
28
  say inspector.format(formatter, routes_filter)
@@ -1,20 +1,22 @@
1
- Examples:
1
+ Description:
2
+ The Rails `runner` allows running Ruby code in the context of your application.
2
3
 
3
- Run `puts Rails.env` after loading the app:
4
+ Examples:
5
+ Run `puts Rails.env` after loading the app:
4
6
 
5
- <%= executable %> 'puts Rails.env'
7
+ <%= executable %> 'puts Rails.env'
6
8
 
7
- Run the Ruby file located at `path/to/filename.rb` after loading the app:
9
+ Run the Ruby file located at `path/to/filename.rb` after loading the app:
8
10
 
9
- <%= executable %> path/to/filename.rb
11
+ <%= executable %> path/to/filename.rb
10
12
 
11
- Run the Ruby script read from stdin after loading the app:
12
- <%= executable %> -
13
+ Run the Ruby script read from stdin after loading the app:
13
14
 
14
- <% unless Gem.win_platform? %>
15
- You can also use the runner command as a shebang line for your executables:
15
+ <%= executable %> -
16
+ <% unless Gem.win_platform? -%>
16
17
 
17
- #!/usr/bin/env <%= File.expand_path(executable) %>
18
+ You can also use the runner command as a shebang line for your executables:
18
19
 
19
- Product.all.each { |p| p.price *= 2 ; p.save! }
20
- <% end %>
20
+ #!/usr/bin/env <%= File.expand_path(executable) %>
21
+ Product.all.each { |p| p.price *= 2 ; p.save! }
22
+ <% end -%>
@@ -7,52 +7,64 @@ module Rails
7
7
  class RunnerCommand < Base # :nodoc:
8
8
  include EnvironmentArgument
9
9
 
10
- self.environment_desc = "The environment for the runner to operate under (test/development/production)"
11
-
12
10
  no_commands do
13
- def help
11
+ def help(command_name = nil, *)
14
12
  super
15
- say self.class.desc
13
+ if command_name == "runner"
14
+ say ""
15
+ say self.class.class_usage
16
+ end
16
17
  end
17
18
  end
18
19
 
19
- def self.banner(*)
20
- "#{super} [<'Some.ruby(code)'> | <filename.rb> | -]"
21
- end
22
-
20
+ desc "runner [<'Some.ruby(code)'> | <filename.rb> | -]",
21
+ "Run Ruby code in the context of your application"
23
22
  def perform(code_or_file = nil, *command_argv)
24
- extract_environment_option_from_argument
25
-
26
23
  unless code_or_file
27
24
  help
28
25
  exit 1
29
26
  end
30
27
 
31
- ENV["RAILS_ENV"] = options[:environment]
32
-
33
- require_application_and_environment!
28
+ boot_application!
34
29
  Rails.application.load_runner
35
30
 
36
31
  ARGV.replace(command_argv)
37
32
 
38
33
  if code_or_file == "-"
39
- eval($stdin.read, TOPLEVEL_BINDING, "stdin")
34
+ Rails.application.executor.wrap(source: "application.runner.railties") do
35
+ eval($stdin.read, TOPLEVEL_BINDING, "stdin")
36
+ end
40
37
  elsif File.exist?(code_or_file)
41
38
  expanded_file_path = File.expand_path code_or_file
42
39
  $0 = expanded_file_path
43
- Kernel.load expanded_file_path
40
+ Rails.application.executor.wrap(source: "application.runner.railties") do
41
+ Kernel.load expanded_file_path
42
+ end
44
43
  else
45
44
  begin
46
- eval(code_or_file, TOPLEVEL_BINDING, __FILE__, __LINE__)
45
+ Rails.application.executor.wrap(source: "application.runner.railties") do
46
+ eval(code_or_file, TOPLEVEL_BINDING, __FILE__, __LINE__)
47
+ end
47
48
  rescue SyntaxError, NameError => e
48
- error "Please specify a valid ruby command or the path of a script to run."
49
- error "Run '#{self.class.executable} -h' for help."
50
- error ""
51
- error e
49
+ if looks_like_a_file_path?(code_or_file)
50
+ error "The file #{code_or_file} could not be found, please check and try again."
51
+ error "Run '#{self.class.executable} -h' for help."
52
+ else
53
+ error "Please specify a valid ruby command or the path of a script to run."
54
+ error "Run '#{self.class.executable} -h' for help."
55
+ error ""
56
+ error e
57
+ end
58
+
52
59
  exit 1
53
60
  end
54
61
  end
55
62
  end
63
+
64
+ private
65
+ def looks_like_a_file_path?(code_or_file)
66
+ code_or_file.ends_with?(".rb")
67
+ end
56
68
  end
57
69
  end
58
70
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Command
5
+ class SecretCommand < Base # :nodoc:
6
+ desc "secret", "Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions)."
7
+ def perform
8
+ require "securerandom"
9
+ puts SecureRandom.hex(64)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,66 +1,61 @@
1
- === ** DEPRECATED **
1
+ Description:
2
+ ** DEPRECATED **
3
+ Rails 5.2 has introduced a new `credentials` API that replaces Rails secrets.
4
+ Please use the Rails `credentials` commands instead.
5
+ Run `bin/rails credentials:help` for more information.
2
6
 
3
- Rails 5.2 has introduced a new `credentials` API that replaces Rails secrets.
4
- Please use the Rails `credentials` commands instead.
5
- Run `rails credentials:help` for more information.
7
+ The Rails `secrets` commands helps encrypting secrets to slim a production
8
+ environment's `ENV` hash. It's also useful for atomic deploys: no need to
9
+ coordinate key changes to get everything working as the keys are shipped
10
+ with the code.
6
11
 
7
- === Storing Encrypted Secrets in Source Control
12
+ Setup:
13
+ Run `<%= executable(:setup) %>` to opt in and generate the `config/secrets.yml.key`
14
+ and `config/secrets.yml.enc` files.
8
15
 
9
- The Rails `secrets` commands helps encrypting secrets to slim a production
10
- environment's `ENV` hash. It's also useful for atomic deploys: no need to
11
- coordinate key changes to get everything working as the keys are shipped
12
- with the code.
16
+ The latter contains all the keys to be encrypted while the former holds the
17
+ encryption key.
13
18
 
14
- === Setup
19
+ Don't lose the key! Put it in a password manager your team can access.
20
+ Should you lose it no one, including you, will be able to access any encrypted
21
+ secrets.
22
+ Don't commit the key! Add `config/secrets.yml.key` to your source control's
23
+ ignore file. If you use Git, Rails handles this for you.
15
24
 
16
- Run `bin/rails secrets:setup` to opt in and generate the `config/secrets.yml.key`
17
- and `config/secrets.yml.enc` files.
25
+ Rails also looks for the key in `ENV["RAILS_MASTER_KEY"]` if that's easier to
26
+ manage.
18
27
 
19
- The latter contains all the keys to be encrypted while the former holds the
20
- encryption key.
28
+ You could prepend that to your server's start command like this:
21
29
 
22
- Don't lose the key! Put it in a password manager your team can access.
23
- Should you lose it no one, including you, will be able to access any encrypted
24
- secrets.
25
- Don't commit the key! Add `config/secrets.yml.key` to your source control's
26
- ignore file. If you use Git, Rails handles this for you.
30
+ RAILS_MASTER_KEY="im-the-master-now-hahaha" bin/rails server
27
31
 
28
- Rails also looks for the key in `ENV["RAILS_MASTER_KEY"]` if that's easier to
29
- manage.
32
+ The `config/secrets.yml.enc` has much the same format as `config/secrets.yml`:
30
33
 
31
- You could prepend that to your server's start command like this:
34
+ production:
35
+ secret_key_base: so-secret-very-hidden-wow
36
+ payment_processing_gateway_key: much-safe-very-gaedwey-wow
32
37
 
33
- RAILS_MASTER_KEY="im-the-master-now-hahaha" server.start
38
+ But that's where the similarities between `secrets.yml` and `secrets.yml.enc`
39
+ end, e.g. no keys from `secrets.yml` will be moved to `secrets.yml.enc` and
40
+ be encrypted.
34
41
 
42
+ A `shared:` top level key is also supported such that any keys there is merged
43
+ into the other environments.
35
44
 
36
- The `config/secrets.yml.enc` has much the same format as `config/secrets.yml`:
45
+ Additionally, Rails won't read encrypted secrets out of the box even if you have
46
+ the key. Add this:
37
47
 
38
- production:
39
- secret_key_base: so-secret-very-hidden-wow
40
- payment_processing_gateway_key: much-safe-very-gaedwey-wow
48
+ config.read_encrypted_secrets = true
41
49
 
42
- But that's where the similarities between `secrets.yml` and `secrets.yml.enc`
43
- end, e.g. no keys from `secrets.yml` will be moved to `secrets.yml.enc` and
44
- be encrypted.
50
+ to the environment you'd like to read encrypted secrets. `<%= executable(:setup) %>`
51
+ inserts this into the production environment by default.
45
52
 
46
- A `shared:` top level key is also supported such that any keys there is merged
47
- into the other environments.
53
+ Editing Secrets:
54
+ After `<%= executable(:setup) %>`, run `<%= executable(:edit) %>`.
48
55
 
49
- Additionally, Rails won't read encrypted secrets out of the box even if you have
50
- the key. Add this:
56
+ That command opens a temporary file in `$VISUAL` or `$EDITOR` with the decrypted
57
+ contents of `config/secrets.yml.enc` to edit the encrypted secrets.
51
58
 
52
- config.read_encrypted_secrets = true
53
-
54
- to the environment you'd like to read encrypted secrets. `bin/rails secrets:setup`
55
- inserts this into the production environment by default.
56
-
57
- === Editing Secrets
58
-
59
- After `bin/rails secrets:setup`, run `bin/rails secrets:edit`.
60
-
61
- That command opens a temporary file in `$EDITOR` with the decrypted contents of
62
- `config/secrets.yml.enc` to edit the encrypted secrets.
63
-
64
- When the temporary file is next saved the contents are encrypted and written to
65
- `config/secrets.yml.enc` while the file itself is destroyed to prevent secrets
66
- from leaking.
59
+ When the temporary file is next saved the contents are encrypted and written to
60
+ `config/secrets.yml.enc` while the file itself is destroyed to prevent secrets
61
+ from leaking.
@@ -1,65 +1,47 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support"
4
+ require "active_support/core_ext/string/filters"
4
5
  require "rails/secrets"
6
+ require "rails/command/helpers/editor"
5
7
 
6
8
  module Rails
7
9
  module Command
8
10
  class SecretsCommand < Rails::Command::Base # :nodoc:
9
- no_commands do
10
- def help
11
- say "Usage:\n #{self.class.banner}"
12
- say ""
13
- say self.class.desc
14
- end
15
- end
16
-
17
- def setup
18
- deprecate_in_favor_of_credentials_and_exit
19
- end
11
+ include Helpers::Editor
20
12
 
13
+ desc "edit", "**deprecated** Open the secrets in `$VISUAL` or `$EDITOR` for editing"
21
14
  def edit
22
- if ENV["EDITOR"].to_s.empty?
23
- say "No $EDITOR to open decrypted secrets in. Assign one like this:"
24
- say ""
25
- say %(EDITOR="mate --wait" rails secrets:edit)
26
- say ""
27
- say "For editors that fork and exit immediately, it's important to pass a wait flag,"
28
- say "otherwise the secrets will be saved immediately with no chance to edit."
15
+ Rails.deprecator.warn(<<~MSG.squish)
16
+ `bin/rails secrets:edit` is deprecated in favor of credentials and will be removed in Rails 7.2.
17
+ Run `bin/rails credentials:help` for more information.
18
+ MSG
29
19
 
30
- return
31
- end
32
-
33
- require_application_and_environment!
20
+ boot_application!
34
21
 
35
- Rails::Secrets.read_for_editing do |tmp_path|
36
- system("#{ENV["EDITOR"]} #{tmp_path}")
22
+ using_system_editor do
23
+ Rails::Secrets.read_for_editing { |tmp_path| system_editor(tmp_path) }
24
+ say "File encrypted and saved."
37
25
  end
38
-
39
- say "New secrets encrypted and saved."
40
- rescue Interrupt
41
- say "Aborted changing encrypted secrets: nothing saved."
42
26
  rescue Rails::Secrets::MissingKeyError => error
43
27
  say error.message
44
28
  rescue Errno::ENOENT => error
45
- if /secrets\.yml\.enc/.match?(error.message)
46
- deprecate_in_favor_of_credentials_and_exit
29
+ if error.message.include?("secrets.yml.enc")
30
+ exit 1
47
31
  else
48
32
  raise
49
33
  end
50
34
  end
51
35
 
36
+ desc "show", "**deprecated** Show the decrypted secrets"
52
37
  def show
38
+ Rails.deprecator.warn(<<~MSG.squish)
39
+ `bin/rails secrets:show` is deprecated in favor of credentials and will be removed in Rails 7.2.
40
+ Run `bin/rails credentials:help` for more information.
41
+ MSG
42
+
53
43
  say Rails::Secrets.read
54
44
  end
55
-
56
- private
57
- def deprecate_in_favor_of_credentials_and_exit
58
- say "Encrypted secrets is deprecated in favor of credentials. Run:"
59
- say "rails credentials:help"
60
-
61
- exit 1
62
- end
63
45
  end
64
46
  end
65
47
  end