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,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "pathname"
4
- require "shellwords"
5
4
  require "active_support"
6
5
  require "rails/command/helpers/editor"
7
6
  require "rails/command/environment_argument"
@@ -15,56 +14,42 @@ module Rails
15
14
  require_relative "credentials_command/diffing"
16
15
  include Diffing
17
16
 
18
- self.environment_desc = "Uses credentials from config/credentials/:environment.yml.enc encrypted by config/credentials/:environment.key key"
19
-
20
- no_commands do
21
- def help
22
- say "Usage:\n #{self.class.banner}"
23
- say ""
24
- say self.class.desc
25
- end
26
- end
27
-
17
+ desc "edit", "Open the decrypted credentials in `$VISUAL` or `$EDITOR` for editing"
28
18
  def edit
29
- extract_environment_option_from_argument(default_environment: nil)
30
- require_application!
19
+ load_environment_config!
20
+ load_generators
31
21
 
32
- ensure_editor_available(command: "bin/rails credentials:edit") || (return)
22
+ if environment_specified?
23
+ @content_path = "config/credentials/#{environment}.yml.enc" unless config.key?(:content_path)
24
+ @key_path = "config/credentials/#{environment}.key" unless config.key?(:key_path)
25
+ end
33
26
 
34
- ensure_encryption_key_has_been_added if credentials.key.nil?
27
+ ensure_encryption_key_has_been_added
35
28
  ensure_credentials_have_been_added
36
29
  ensure_diffing_driver_is_configured
37
30
 
38
- catch_editing_exceptions do
39
- change_credentials_in_system_editor
40
- end
41
-
42
- say "File encrypted and saved."
43
- rescue ActiveSupport::MessageEncryptor::InvalidMessage
44
- say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
31
+ change_credentials_in_system_editor
45
32
  end
46
33
 
34
+ desc "show", "Show the decrypted credentials"
47
35
  def show
48
- extract_environment_option_from_argument(default_environment: nil)
49
- require_application!
36
+ load_environment_config!
50
37
 
51
38
  say credentials.read.presence || missing_credentials_message
52
39
  end
53
40
 
41
+ desc "diff", "Enroll/disenroll in decrypted diffs of credentials using git"
54
42
  option :enroll, type: :boolean, default: false,
55
- desc: "Enrolls project in credentials file diffing with `git diff`"
56
-
43
+ desc: "Enroll project in credentials file diffing with `git diff`"
57
44
  option :disenroll, type: :boolean, default: false,
58
- desc: "Disenrolls project from credentials file diffing"
59
-
45
+ desc: "Disenroll project from credentials file diffing"
60
46
  def diff(content_path = nil)
61
47
  if @content_path = content_path
62
- extract_environment_option_from_argument(default_environment: extract_environment_from_path(content_path))
63
- require_application!
48
+ self.environment = extract_environment_from_path(content_path)
49
+ load_environment_config!
64
50
 
65
51
  say credentials.read.presence || credentials.content_path.read
66
52
  else
67
- require_application!
68
53
  disenroll_project_from_credentials_diffing if options[:disenroll]
69
54
  enroll_project_in_credentials_diffing if options[:enroll]
70
55
  end
@@ -73,68 +58,77 @@ module Rails
73
58
  end
74
59
 
75
60
  private
61
+ def config
62
+ Rails.application.config.credentials
63
+ end
64
+
65
+ def content_path
66
+ @content_path ||= relative_path(config.content_path)
67
+ end
68
+
69
+ def key_path
70
+ @key_path ||= relative_path(config.key_path)
71
+ end
72
+
76
73
  def credentials
77
- Rails.application.encrypted(content_path, key_path: key_path)
74
+ @credentials ||= Rails.application.encrypted(content_path, key_path: key_path)
78
75
  end
79
76
 
80
77
  def ensure_encryption_key_has_been_added
78
+ return if credentials.key?
79
+
80
+ require "rails/generators/rails/encryption_key_file/encryption_key_file_generator"
81
+
82
+ encryption_key_file_generator = Rails::Generators::EncryptionKeyFileGenerator.new
81
83
  encryption_key_file_generator.add_key_file(key_path)
82
84
  encryption_key_file_generator.ignore_key_file(key_path)
83
85
  end
84
86
 
85
87
  def ensure_credentials_have_been_added
86
- if options[:environment]
87
- encrypted_file_generator.add_encrypted_file_silently(content_path, key_path)
88
- else
89
- credentials_generator.add_credentials_file_silently
90
- end
88
+ require "rails/generators/rails/credentials/credentials_generator"
89
+
90
+ Rails::Generators::CredentialsGenerator.new(
91
+ [content_path, key_path],
92
+ skip_secret_key_base: environment_specified? && %w[development test].include?(environment),
93
+ quiet: true
94
+ ).invoke_all
91
95
  end
92
96
 
93
97
  def change_credentials_in_system_editor
94
- credentials.change do |tmp_path|
95
- system("#{ENV["EDITOR"]} #{Shellwords.escape(tmp_path)}")
98
+ using_system_editor do
99
+ say "Editing #{content_path}..."
100
+ credentials.change { |tmp_path| system_editor(tmp_path) }
101
+ say "File encrypted and saved."
102
+ warn_if_credentials_are_invalid
96
103
  end
104
+ rescue ActiveSupport::EncryptedFile::MissingKeyError => error
105
+ say error.message
106
+ rescue ActiveSupport::MessageEncryptor::InvalidMessage
107
+ say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
108
+ end
109
+
110
+ def warn_if_credentials_are_invalid
111
+ credentials.validate!
112
+ rescue ActiveSupport::EncryptedConfiguration::InvalidContentError => error
113
+ say "WARNING: #{error.message}", :red
114
+ say ""
115
+ say "Your application will not be able to load '#{content_path}' until the error has been fixed.", :red
97
116
  end
98
117
 
99
118
  def missing_credentials_message
100
- if credentials.key.nil?
101
- "Missing '#{key_path}' to decrypt credentials. See `bin/rails credentials:help`"
119
+ if !credentials.key?
120
+ "Missing '#{key_path}' to decrypt credentials. See `#{executable(:help)}`."
102
121
  else
103
- "File '#{content_path}' does not exist. Use `bin/rails credentials:edit` to change that."
122
+ "File '#{content_path}' does not exist. Use `#{executable(:edit)}` to change that."
104
123
  end
105
124
  end
106
125
 
107
- def content_path
108
- @content_path ||= options[:environment] ? "config/credentials/#{options[:environment]}.yml.enc" : "config/credentials.yml.enc"
109
- end
110
-
111
- def key_path
112
- options[:environment] ? "config/credentials/#{options[:environment]}.key" : "config/master.key"
126
+ def relative_path(path)
127
+ Rails.root.join(path).relative_path_from(Rails.root).to_s
113
128
  end
114
129
 
115
130
  def extract_environment_from_path(path)
116
- available_environments.find { |env| path.include? env } if path.end_with?(".yml.enc")
117
- end
118
-
119
- def encryption_key_file_generator
120
- require "rails/generators"
121
- require "rails/generators/rails/encryption_key_file/encryption_key_file_generator"
122
-
123
- Rails::Generators::EncryptionKeyFileGenerator.new
124
- end
125
-
126
- def encrypted_file_generator
127
- require "rails/generators"
128
- require "rails/generators/rails/encrypted_file/encrypted_file_generator"
129
-
130
- Rails::Generators::EncryptedFileGenerator.new
131
- end
132
-
133
- def credentials_generator
134
- require "rails/generators"
135
- require "rails/generators/rails/credentials/credentials_generator"
136
-
137
- Rails::Generators::CredentialsGenerator.new
131
+ available_environments.find { |env| path.end_with?("#{env}.yml.enc") }
138
132
  end
139
133
  end
140
134
  end
@@ -15,7 +15,8 @@ module Rails
15
15
  super
16
16
  end
17
17
 
18
- def perform
18
+ desc "change", "Change `config/database.yml` and your database gem to the target database"
19
+ def perform(*)
19
20
  Rails::Generators::Db::System::ChangeGenerator.start(@argv)
20
21
  end
21
22
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support/core_ext/string/filters"
4
- require "active_support/deprecation"
5
4
  require "rails/command/environment_argument"
6
5
 
7
6
  module Rails
@@ -12,86 +11,13 @@ module Rails
12
11
 
13
12
  def initialize(options = {})
14
13
  @options = options
14
+ @options[:environment] ||= Rails::Command.environment
15
15
  end
16
16
 
17
17
  def start
18
- ENV["RAILS_ENV"] ||= @options[:environment] || environment
19
- config = db_config.configuration_hash
20
-
21
- case db_config.adapter
22
- when /^(jdbc)?mysql/
23
- args = {
24
- host: "--host",
25
- port: "--port",
26
- socket: "--socket",
27
- username: "--user",
28
- encoding: "--default-character-set",
29
- sslca: "--ssl-ca",
30
- sslcert: "--ssl-cert",
31
- sslcapath: "--ssl-capath",
32
- sslcipher: "--ssl-cipher",
33
- sslkey: "--ssl-key"
34
- }.filter_map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }
35
-
36
- if config[:password] && @options[:include_password]
37
- args << "--password=#{config[:password]}"
38
- elsif config[:password] && !config[:password].to_s.empty?
39
- args << "-p"
40
- end
41
-
42
- args << db_config.database
43
-
44
- find_cmd_and_exec(["mysql", "mysql5"], *args)
45
-
46
- when /^postgres|^postgis/
47
- ENV["PGUSER"] = config[:username] if config[:username]
48
- ENV["PGHOST"] = config[:host] if config[:host]
49
- ENV["PGPORT"] = config[:port].to_s if config[:port]
50
- ENV["PGPASSWORD"] = config[:password].to_s if config[:password] && @options[:include_password]
51
- ENV["PGSSLMODE"] = config[:sslmode].to_s if config[:sslmode]
52
- ENV["PGSSLCERT"] = config[:sslcert].to_s if config[:sslcert]
53
- ENV["PGSSLKEY"] = config[:sslkey].to_s if config[:sslkey]
54
- ENV["PGSSLROOTCERT"] = config[:sslrootcert].to_s if config[:sslrootcert]
55
- find_cmd_and_exec("psql", db_config.database)
56
-
57
- when "sqlite3"
58
- args = []
59
-
60
- args << "-#{@options[:mode]}" if @options[:mode]
61
- args << "-header" if @options[:header]
62
- args << File.expand_path(db_config.database, Rails.respond_to?(:root) ? Rails.root : nil)
63
-
64
- find_cmd_and_exec("sqlite3", *args)
65
-
66
- when "oracle", "oracle_enhanced"
67
- logon = ""
68
-
69
- if config[:username]
70
- logon = config[:username].dup
71
- logon << "/#{config[:password]}" if config[:password] && @options[:include_password]
72
- logon << "@#{db_config.database}" if db_config.database
73
- end
74
-
75
- find_cmd_and_exec("sqlplus", logon)
76
-
77
- when "sqlserver"
78
- args = []
79
-
80
- args += ["-d", "#{db_config.database}"] if db_config.database
81
- args += ["-U", "#{config[:username]}"] if config[:username]
82
- args += ["-P", "#{config[:password]}"] if config[:password]
83
-
84
- if config[:host]
85
- host_arg = +"tcp:#{config[:host]}"
86
- host_arg << ",#{config[:port]}" if config[:port]
87
- args += ["-S", host_arg]
88
- end
89
-
90
- find_cmd_and_exec("sqlcmd", *args)
91
-
92
- else
93
- abort "Unknown command-line client for #{db_config.database}."
94
- end
18
+ adapter_class.dbconsole(db_config, @options)
19
+ rescue NotImplementedError
20
+ abort "Unknown command-line client for #{db_config.database}."
95
21
  end
96
22
 
97
23
  def db_config
@@ -114,15 +40,23 @@ module Rails
114
40
  @db_config
115
41
  end
116
42
 
117
- def environment
118
- Rails.respond_to?(:env) ? Rails.env : Rails::Command.environment
119
- end
120
-
121
43
  def database
122
44
  @options[:database]
123
45
  end
124
46
 
47
+ def environment
48
+ @options[:environment]
49
+ end
50
+
125
51
  private
52
+ def adapter_class
53
+ if ActiveRecord::Base.respond_to?(db_config.adapter_class_method)
54
+ ActiveRecord::Base.public_send(db_config.adapter_class_method)
55
+ else
56
+ ActiveRecord::ConnectionAdapters::AbstractAdapter
57
+ end
58
+ end
59
+
126
60
  def configurations # :doc:
127
61
  require APP_PATH
128
62
  ActiveRecord::Base.configurations = Rails.application.config.database_configuration
@@ -130,31 +64,11 @@ module Rails
130
64
  end
131
65
 
132
66
  def find_cmd_and_exec(commands, *args) # :doc:
133
- commands = Array(commands)
134
-
135
- dirs_on_path = ENV["PATH"].to_s.split(File::PATH_SEPARATOR)
136
- unless (ext = RbConfig::CONFIG["EXEEXT"]).empty?
137
- commands = commands.map { |cmd| "#{cmd}#{ext}" }
138
- end
139
-
140
- full_path_command = nil
141
- found = commands.detect do |cmd|
142
- dirs_on_path.detect do |path|
143
- full_path_command = File.join(path, cmd)
144
- begin
145
- stat = File.stat(full_path_command)
146
- rescue SystemCallError
147
- else
148
- stat.file? && stat.executable?
149
- end
150
- end
151
- end
152
-
153
- if found
154
- exec full_path_command, *args
155
- else
156
- abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
157
- end
67
+ Rails.deprecator.warn(<<~MSG.squish)
68
+ Rails::DBConsole#find_cmd_and_exec is deprecated and will be removed in Rails 7.2.
69
+ Please use find_cmd_and_exec on the connection adapter class instead.
70
+ MSG
71
+ ActiveRecord::Base.connection.find_cmd_and_exec(commands, *args)
158
72
  end
159
73
  end
160
74
 
@@ -166,20 +80,16 @@ module Rails
166
80
  desc: "Automatically provide the password from database.yml"
167
81
 
168
82
  class_option :mode, enum: %w( html list line column ), type: :string,
169
- desc: "Automatically put the sqlite3 database in the specified mode (html, list, line, column)."
83
+ desc: "Automatically put the sqlite3 database in the specified mode"
170
84
 
171
85
  class_option :header, type: :boolean
172
86
 
173
87
  class_option :database, aliases: "--db", type: :string,
174
- desc: "Specifies the database to use."
88
+ desc: "Specify the database to use."
175
89
 
90
+ desc "dbconsole", "Start a console for the database specified in config/database.yml"
176
91
  def perform
177
- extract_environment_option_from_argument
178
-
179
- # RAILS_ENV needs to be set before config/application is required.
180
- ENV["RAILS_ENV"] = options[:environment]
181
-
182
- require_application_and_environment!
92
+ boot_application!
183
93
  Rails::DBConsole.start(options)
184
94
  end
185
95
  end
@@ -7,18 +7,19 @@ module Rails
7
7
  class DestroyCommand < Base # :nodoc:
8
8
  no_commands do
9
9
  def help
10
- require_application_and_environment!
10
+ boot_application!
11
11
  load_generators
12
12
 
13
13
  Rails::Generators.help self.class.command_name
14
14
  end
15
15
  end
16
16
 
17
+ desc "destroy GENERATOR", "Remove code generated by `bin/rails generate`"
17
18
  def perform(*)
18
19
  generator = args.shift
19
20
  return help unless generator
20
21
 
21
- require_application_and_environment!
22
+ boot_application!
22
23
  load_generators
23
24
 
24
25
  Rails::Generators.invoke generator, args, behavior: :revoke, destination_root: Rails::Command.root
@@ -5,12 +5,7 @@ require "rails/dev_caching"
5
5
  module Rails
6
6
  module Command
7
7
  class DevCommand < Base # :nodoc:
8
- no_commands do
9
- def help
10
- say "rails dev:cache # Toggle development mode caching on/off."
11
- end
12
- end
13
-
8
+ desc "cache", "Toggle development mode caching on/off"
14
9
  def cache
15
10
  Rails::DevCaching.enable_by_file
16
11
  end
@@ -1,28 +1,23 @@
1
- === Storing Encrypted Files in Source Control
1
+ Description:
2
+ The Rails `encrypted` commands provide access to encrypted files or configurations.
3
+ See the `Rails.application.encrypted` documentation for using them in your app.
2
4
 
3
- The Rails `encrypted` commands provide access to encrypted files or configurations.
4
- See the `Rails.application.encrypted` documentation for using them in your app.
5
+ Encryption Keys:
6
+ By default, Rails looks for the encryption key in `ENV["RAILS_MASTER_KEY"]` or
7
+ `config/master.key`, but that lookup can be overridden with `--key`:
5
8
 
6
- === Encryption Keys
9
+ <%= executable(:edit) %> config/encrypted_file.yml.enc --key config/encrypted_file.key
7
10
 
8
- By default, Rails looks for the encryption key in `config/master.key` or
9
- `ENV["RAILS_MASTER_KEY"]`, but that lookup can be overridden with `--key`:
11
+ Don't commit the key! Add it to your source control's ignore file. If you use
12
+ Git, Rails handles this for you.
10
13
 
11
- rails encrypted:edit config/encrypted_file.yml.enc --key config/encrypted_file.key
14
+ Examples:
15
+ To edit or create an encrypted file use:
12
16
 
13
- Don't commit the key! Add it to your source control's ignore file. If you use
14
- Git, Rails handles this for you.
17
+ <%= executable(:edit) %> config/encrypted_file.yml.enc
15
18
 
16
- === Editing Files
19
+ This opens a temporary file in `$VISUAL` or `$EDITOR` with the decrypted contents for editing.
17
20
 
18
- To edit or create an encrypted file use:
21
+ To print the decrypted contents of an encrypted file use:
19
22
 
20
- rails encrypted:edit config/encrypted_file.yml.enc
21
-
22
- This opens a temporary file in `$EDITOR` with the decrypted contents for editing.
23
-
24
- === Viewing Files
25
-
26
- To print the decrypted contents of an encrypted file use:
27
-
28
- rails encrypted:show config/encrypted_file.yml.enc
23
+ <%= executable(:show) %> config/encrypted_file.yml.enc
@@ -12,54 +12,65 @@ module Rails
12
12
  class_option :key, aliases: "-k", type: :string,
13
13
  default: "config/master.key", desc: "The Rails.root relative path to the encryption key"
14
14
 
15
- no_commands do
16
- def help
17
- say "Usage:\n #{self.class.banner}"
18
- say ""
19
- say self.class.desc
20
- end
21
- end
22
-
23
- def edit(file_path)
24
- require_application!
25
- encrypted = Rails.application.encrypted(file_path, key_path: options[:key])
15
+ desc "edit", "Open the decrypted file in `$VISUAL` or `$EDITOR` for editing"
16
+ def edit(*)
17
+ load_environment_config!
26
18
 
27
- ensure_editor_available(command: "bin/rails encrypted:edit") || (return)
28
- ensure_encryption_key_has_been_added(options[:key]) if encrypted.key.nil?
29
- ensure_encrypted_file_has_been_added(file_path, options[:key])
19
+ ensure_encryption_key_has_been_added
20
+ ensure_encrypted_configuration_has_been_added
30
21
 
31
- catch_editing_exceptions do
32
- change_encrypted_file_in_system_editor(file_path, options[:key])
33
- end
34
-
35
- say "File encrypted and saved."
36
- rescue ActiveSupport::MessageEncryptor::InvalidMessage
37
- say "Couldn't decrypt #{file_path}. Perhaps you passed the wrong key?"
22
+ change_encrypted_configuration_in_system_editor
38
23
  end
39
24
 
40
- def show(file_path)
41
- require_application!
42
- encrypted = Rails.application.encrypted(file_path, key_path: options[:key])
25
+ desc "show", "Show the decrypted contents of the file"
26
+ def show(*)
27
+ load_environment_config!
43
28
 
44
- say encrypted.read.presence || missing_encrypted_message(key: encrypted.key, key_path: options[:key], file_path: file_path)
29
+ say encrypted_configuration.read.presence || missing_encrypted_configuration_message
45
30
  end
46
31
 
47
32
  private
48
- def ensure_encryption_key_has_been_added(key_path)
33
+ def content_path
34
+ @content_path ||= args[0]
35
+ end
36
+
37
+ def key_path
38
+ options[:key]
39
+ end
40
+
41
+ def encrypted_configuration
42
+ @encrypted_configuration ||= Rails.application.encrypted(content_path, key_path: key_path)
43
+ end
44
+
45
+ def ensure_encryption_key_has_been_added
46
+ return if encrypted_configuration.key?
49
47
  encryption_key_file_generator.add_key_file(key_path)
50
48
  encryption_key_file_generator.ignore_key_file(key_path)
51
49
  end
52
50
 
53
- def ensure_encrypted_file_has_been_added(file_path, key_path)
54
- encrypted_file_generator.add_encrypted_file_silently(file_path, key_path)
51
+ def ensure_encrypted_configuration_has_been_added
52
+ encrypted_file_generator.add_encrypted_file_silently(content_path, key_path)
55
53
  end
56
54
 
57
- def change_encrypted_file_in_system_editor(file_path, key_path)
58
- Rails.application.encrypted(file_path, key_path: key_path).change do |tmp_path|
59
- system("#{ENV["EDITOR"]} #{tmp_path}")
55
+ def change_encrypted_configuration_in_system_editor
56
+ using_system_editor do
57
+ encrypted_configuration.change { |tmp_path| system_editor(tmp_path) }
58
+ say "File encrypted and saved."
59
+ warn_if_encrypted_configuration_is_invalid
60
60
  end
61
+ rescue ActiveSupport::EncryptedFile::MissingKeyError => error
62
+ say error.message
63
+ rescue ActiveSupport::MessageEncryptor::InvalidMessage
64
+ say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
61
65
  end
62
66
 
67
+ def warn_if_encrypted_configuration_is_invalid
68
+ encrypted_configuration.validate!
69
+ rescue ActiveSupport::EncryptedConfiguration::InvalidContentError => error
70
+ say "WARNING: #{error.message}", :red
71
+ say ""
72
+ say "Your application will not be able to load '#{content_path}' until the error has been fixed.", :red
73
+ end
63
74
 
64
75
  def encryption_key_file_generator
65
76
  require "rails/generators"
@@ -75,11 +86,11 @@ module Rails
75
86
  Rails::Generators::EncryptedFileGenerator.new
76
87
  end
77
88
 
78
- def missing_encrypted_message(key:, key_path:, file_path:)
79
- if key.nil?
80
- "Missing '#{key_path}' to decrypt data. See `bin/rails encrypted:help`"
89
+ def missing_encrypted_configuration_message
90
+ if !encrypted_configuration.key?
91
+ "Missing '#{key_path}' to decrypt data. See `#{executable(:help)}`"
81
92
  else
82
- "File '#{file_path}' does not exist. Use `bin/rails encrypted:edit #{file_path}` to change that."
93
+ "File '#{content_path}' does not exist. Use `#{executable(:edit)} #{content_path}` to change that."
83
94
  end
84
95
  end
85
96
  end
@@ -0,0 +1,16 @@
1
+ Usage:
2
+ rails COMMAND [options]
3
+
4
+ You must specify a command:
5
+
6
+ new Create a new Rails application. "rails new my_app" creates a
7
+ new application called MyApp in "./my_app"
8
+ plugin new Create a new Rails railtie or engine
9
+
10
+ All commands can be run with -h (or --help) for more information.
11
+
12
+ Inside a Rails application directory, some common commands are:
13
+
14
+ console Start the Rails console
15
+ server Start the Rails server
16
+ test Run tests except system tests
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Command
5
+ class GemHelpCommand < Base # :nodoc:
6
+ hide_command!
7
+
8
+ def perform
9
+ say self.class.class_usage
10
+ end
11
+ end
12
+ end
13
+ end
@@ -7,7 +7,7 @@ module Rails
7
7
  class GenerateCommand < Base # :nodoc:
8
8
  no_commands do
9
9
  def help
10
- require_application_and_environment!
10
+ boot_application!
11
11
  load_generators
12
12
 
13
13
  Rails::Generators.help self.class.command_name
@@ -18,7 +18,7 @@ module Rails
18
18
  generator = args.shift
19
19
  return help unless generator
20
20
 
21
- require_application_and_environment!
21
+ boot_application!
22
22
  load_generators
23
23
 
24
24
  ARGV.replace(args) # set up ARGV for third-party libraries