railties 6.0.0 → 7.1.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (286) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +699 -245
  3. data/MIT-LICENSE +1 -1
  4. data/RDOC_MAIN.md +99 -0
  5. data/README.rdoc +7 -8
  6. data/lib/minitest/rails_plugin.rb +80 -2
  7. data/lib/rails/all.rb +0 -1
  8. data/lib/rails/api/task.rb +37 -5
  9. data/lib/rails/app_updater.rb +9 -6
  10. data/lib/rails/application/bootstrap.rb +52 -14
  11. data/lib/rails/application/configuration.rb +327 -86
  12. data/lib/rails/application/default_middleware_stack.rb +33 -7
  13. data/lib/rails/application/dummy_config.rb +19 -0
  14. data/lib/rails/application/finisher.rb +94 -113
  15. data/lib/rails/application/routes_reloader.rb +17 -3
  16. data/lib/rails/application.rb +258 -201
  17. data/lib/rails/application_controller.rb +3 -4
  18. data/lib/rails/autoloaders/inflector.rb +21 -0
  19. data/lib/rails/autoloaders.rb +42 -42
  20. data/lib/rails/backtrace_cleaner.rb +13 -8
  21. data/lib/rails/cli.rb +5 -2
  22. data/lib/rails/code_statistics.rb +6 -4
  23. data/lib/rails/code_statistics_calculator.rb +16 -7
  24. data/lib/rails/command/actions.rb +10 -12
  25. data/lib/rails/command/base.rb +66 -46
  26. data/lib/rails/command/behavior.rb +3 -3
  27. data/lib/rails/command/environment_argument.rb +33 -17
  28. data/lib/rails/command/helpers/editor.rb +17 -12
  29. data/lib/rails/command.rb +90 -32
  30. data/lib/rails/commands/about/about_command.rb +14 -0
  31. data/lib/rails/commands/application/application_command.rb +2 -0
  32. data/lib/rails/commands/console/console_command.rb +17 -13
  33. data/lib/rails/commands/credentials/USAGE +54 -39
  34. data/lib/rails/commands/credentials/credentials_command/diffing.rb +53 -0
  35. data/lib/rails/commands/credentials/credentials_command.rb +83 -60
  36. data/lib/rails/commands/db/system/change/change_command.rb +8 -2
  37. data/lib/rails/commands/dbconsole/dbconsole_command.rb +39 -124
  38. data/lib/rails/commands/destroy/destroy_command.rb +3 -2
  39. data/lib/rails/commands/dev/dev_command.rb +1 -6
  40. data/lib/rails/commands/encrypted/USAGE +15 -20
  41. data/lib/rails/commands/encrypted/encrypted_command.rb +46 -35
  42. data/lib/rails/commands/gem_help/USAGE +16 -0
  43. data/lib/rails/commands/gem_help/gem_help_command.rb +13 -0
  44. data/lib/rails/commands/generate/generate_command.rb +3 -3
  45. data/lib/rails/commands/help/USAGE +15 -14
  46. data/lib/rails/commands/help/help_command.rb +21 -2
  47. data/lib/rails/commands/initializers/initializers_command.rb +1 -4
  48. data/lib/rails/commands/middleware/middleware_command.rb +17 -0
  49. data/lib/rails/commands/new/new_command.rb +2 -0
  50. data/lib/rails/commands/notes/notes_command.rb +5 -14
  51. data/lib/rails/commands/plugin/plugin_command.rb +2 -0
  52. data/lib/rails/commands/rake/rake_command.rb +27 -23
  53. data/lib/rails/commands/restart/restart_command.rb +14 -0
  54. data/lib/rails/commands/routes/routes_command.rb +13 -1
  55. data/lib/rails/commands/runner/USAGE +14 -12
  56. data/lib/rails/commands/runner/runner_command.rb +34 -21
  57. data/lib/rails/commands/secret/secret_command.rb +13 -0
  58. data/lib/rails/commands/secrets/USAGE +44 -43
  59. data/lib/rails/commands/secrets/secrets_command.rb +20 -38
  60. data/lib/rails/commands/server/server_command.rb +40 -64
  61. data/lib/rails/commands/test/USAGE +14 -0
  62. data/lib/rails/commands/test/test_command.rb +58 -16
  63. data/lib/rails/commands/unused_routes/unused_routes_command.rb +75 -0
  64. data/lib/rails/commands/version/version_command.rb +1 -0
  65. data/lib/rails/configuration.rb +53 -23
  66. data/lib/rails/console/app.rb +1 -4
  67. data/lib/rails/console/helpers.rb +2 -2
  68. data/lib/rails/deprecator.rb +7 -0
  69. data/lib/rails/engine/configuration.rb +54 -9
  70. data/lib/rails/engine/updater.rb +1 -1
  71. data/lib/rails/engine.rb +105 -81
  72. data/lib/rails/gem_version.rb +5 -5
  73. data/lib/rails/generators/actions/create_migration.rb +9 -5
  74. data/lib/rails/generators/actions.rb +275 -97
  75. data/lib/rails/generators/active_model.rb +28 -14
  76. data/lib/rails/generators/app_base.rb +487 -165
  77. data/lib/rails/generators/app_name.rb +3 -14
  78. data/lib/rails/generators/base.rb +42 -31
  79. data/lib/rails/generators/database.rb +41 -4
  80. data/lib/rails/generators/erb/mailer/mailer_generator.rb +0 -1
  81. data/lib/rails/generators/erb/mailer/templates/layout.html.erb.tt +1 -1
  82. data/lib/rails/generators/erb/scaffold/scaffold_generator.rb +2 -1
  83. data/lib/rails/generators/erb/scaffold/templates/_form.html.erb.tt +11 -11
  84. data/lib/rails/generators/erb/scaffold/templates/edit.html.erb.tt +8 -4
  85. data/lib/rails/generators/erb/scaffold/templates/index.html.erb.tt +11 -28
  86. data/lib/rails/generators/erb/scaffold/templates/new.html.erb.tt +7 -3
  87. data/lib/rails/generators/erb/scaffold/templates/partial.html.erb.tt +17 -0
  88. data/lib/rails/generators/erb/scaffold/templates/show.html.erb.tt +8 -17
  89. data/lib/rails/generators/erb.rb +1 -2
  90. data/lib/rails/generators/generated_attribute.rb +60 -19
  91. data/lib/rails/generators/migration.rb +4 -8
  92. data/lib/rails/generators/model_helpers.rb +29 -4
  93. data/lib/rails/generators/named_base.rb +15 -15
  94. data/lib/rails/generators/rails/app/USAGE +23 -6
  95. data/lib/rails/generators/rails/app/app_generator.rb +151 -80
  96. data/lib/rails/generators/rails/app/templates/Dockerfile.tt +103 -0
  97. data/lib/rails/generators/rails/app/templates/Gemfile.tt +44 -54
  98. data/lib/rails/generators/rails/app/templates/Rakefile.tt +1 -1
  99. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css.tt +2 -2
  100. data/lib/rails/generators/rails/app/templates/app/mailers/application_mailer.rb.tt +2 -2
  101. data/lib/rails/generators/rails/app/templates/app/models/application_record.rb.tt +1 -1
  102. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +4 -9
  103. data/lib/rails/generators/rails/app/templates/app/views/layouts/mailer.html.erb.tt +1 -1
  104. data/lib/rails/generators/rails/app/templates/bin/rails.tt +3 -3
  105. data/lib/rails/generators/rails/app/templates/bin/rake.tt +2 -2
  106. data/lib/rails/generators/rails/app/templates/bin/setup.tt +19 -15
  107. data/lib/rails/generators/rails/app/templates/config/application.rb.tt +18 -23
  108. data/lib/rails/generators/rails/app/templates/config/boot.rb.tt +3 -3
  109. data/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml.tt +5 -6
  110. data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml.tt +12 -11
  111. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml.tt +17 -16
  112. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml.tt +5 -5
  113. data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml.tt +12 -11
  114. data/lib/rails/generators/rails/app/templates/config/databases/oracle.yml.tt +12 -11
  115. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt +17 -18
  116. data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml.tt +4 -4
  117. data/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml.tt +14 -13
  118. data/lib/rails/generators/rails/app/templates/config/databases/trilogy.yml.tt +59 -0
  119. data/lib/rails/generators/rails/app/templates/config/environment.rb.tt +1 -1
  120. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +32 -14
  121. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +42 -55
  122. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +27 -15
  123. data/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt +3 -5
  124. data/lib/rails/generators/rails/app/templates/config/initializers/content_security_policy.rb.tt +21 -28
  125. data/lib/rails/generators/rails/app/templates/config/initializers/cors.rb.tt +3 -3
  126. data/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb.tt +6 -2
  127. data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb.tt +4 -4
  128. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_1.rb.tt +284 -0
  129. data/lib/rails/generators/rails/app/templates/config/initializers/permissions_policy.rb.tt +13 -0
  130. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +13 -15
  131. data/lib/rails/generators/rails/app/templates/config/puma.rb.tt +17 -20
  132. data/lib/rails/generators/rails/app/templates/config/routes.rb.tt +8 -1
  133. data/lib/rails/generators/rails/app/templates/config/storage.yml.tt +5 -5
  134. data/lib/rails/generators/rails/app/templates/config.ru.tt +2 -1
  135. data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +7 -5
  136. data/lib/rails/generators/rails/app/templates/docker-entrypoint.tt +10 -0
  137. data/lib/rails/generators/rails/app/templates/dockerignore.tt +43 -0
  138. data/lib/rails/generators/rails/app/templates/gitattributes.tt +9 -0
  139. data/lib/rails/generators/rails/app/templates/gitignore.tt +12 -9
  140. data/lib/rails/generators/rails/app/templates/node-version.tt +1 -0
  141. data/lib/rails/generators/rails/app/templates/test/channels/application_cable/connection_test.rb.tt +10 -8
  142. data/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt +13 -11
  143. data/lib/rails/generators/rails/application_record/application_record_generator.rb +4 -0
  144. data/lib/rails/generators/rails/benchmark/USAGE +19 -0
  145. data/lib/rails/generators/rails/benchmark/benchmark_generator.rb +30 -0
  146. data/lib/rails/generators/rails/benchmark/templates/benchmark.rb.tt +15 -0
  147. data/lib/rails/generators/rails/controller/USAGE +13 -5
  148. data/lib/rails/generators/rails/controller/controller_generator.rb +7 -42
  149. data/lib/rails/generators/rails/controller/templates/controller.rb.tt +1 -5
  150. data/lib/rails/generators/rails/credentials/credentials_generator.rb +30 -25
  151. data/lib/rails/generators/rails/credentials/templates/credentials.yml.tt +8 -0
  152. data/lib/rails/generators/rails/db/system/change/change_generator.rb +31 -1
  153. data/lib/rails/generators/rails/encrypted_file/encrypted_file_generator.rb +6 -2
  154. data/lib/rails/generators/rails/encryption_key_file/encryption_key_file_generator.rb +1 -2
  155. data/lib/rails/generators/rails/generator/USAGE +2 -2
  156. data/lib/rails/generators/rails/generator/generator_generator.rb +0 -1
  157. data/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt +1 -1
  158. data/lib/rails/generators/rails/generator/templates/USAGE.tt +1 -1
  159. data/lib/rails/generators/rails/helper/USAGE +2 -3
  160. data/lib/rails/generators/rails/integration_test/USAGE +2 -2
  161. data/lib/rails/generators/rails/migration/USAGE +22 -12
  162. data/lib/rails/generators/rails/model/model_generator.rb +4 -0
  163. data/lib/rails/generators/rails/plugin/USAGE +17 -6
  164. data/lib/rails/generators/rails/plugin/plugin_generator.rb +86 -46
  165. data/lib/rails/generators/rails/plugin/templates/%name%.gemspec.tt +15 -20
  166. data/lib/rails/generators/rails/plugin/templates/Gemfile.tt +10 -39
  167. data/lib/rails/generators/rails/plugin/templates/MIT-LICENSE.tt +1 -1
  168. data/lib/rails/generators/rails/plugin/templates/README.md.tt +1 -1
  169. data/lib/rails/generators/rails/plugin/templates/Rakefile.tt +4 -18
  170. data/lib/rails/generators/rails/plugin/templates/app/controllers/%namespaced_name%/application_controller.rb.tt +0 -1
  171. data/lib/rails/generators/rails/plugin/templates/app/mailers/%namespaced_name%/application_mailer.rb.tt +2 -2
  172. data/lib/rails/generators/rails/plugin/templates/app/views/layouts/%namespaced_name%/application.html.erb.tt +0 -3
  173. data/lib/rails/generators/rails/plugin/templates/bin/rails.tt +7 -22
  174. data/lib/rails/generators/rails/plugin/templates/gitignore.tt +10 -14
  175. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/engine.rb.tt +2 -2
  176. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/version.rb.tt +1 -1
  177. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%.rb.tt +1 -0
  178. data/lib/rails/generators/rails/plugin/templates/rails/boot.rb.tt +3 -3
  179. data/lib/rails/generators/rails/plugin/templates/test/%namespaced_name%_test.rb.tt +4 -4
  180. data/lib/rails/generators/rails/plugin/templates/test/integration/navigation_test.rb.tt +1 -1
  181. data/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt +6 -14
  182. data/lib/rails/generators/rails/resource/USAGE +4 -4
  183. data/lib/rails/generators/rails/resource/resource_generator.rb +6 -0
  184. data/lib/rails/generators/rails/resource_route/resource_route_generator.rb +2 -27
  185. data/lib/rails/generators/rails/scaffold/USAGE +5 -5
  186. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +2 -20
  187. data/lib/rails/generators/rails/scaffold_controller/USAGE +2 -2
  188. data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +7 -2
  189. data/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb.tt +2 -6
  190. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt +7 -11
  191. data/lib/rails/generators/rails/system_test/USAGE +2 -2
  192. data/lib/rails/generators/rails/task/USAGE +3 -3
  193. data/lib/rails/generators/resource_helpers.rb +2 -2
  194. data/lib/rails/generators/test_case.rb +3 -3
  195. data/lib/rails/generators/test_unit/controller/controller_generator.rb +2 -0
  196. data/lib/rails/generators/test_unit/controller/templates/functional_test.rb.tt +3 -3
  197. data/lib/rails/generators/test_unit/generator/generator_generator.rb +0 -1
  198. data/lib/rails/generators/test_unit/generator/templates/generator_test.rb.tt +3 -3
  199. data/lib/rails/generators/test_unit/integration/integration_generator.rb +0 -1
  200. data/lib/rails/generators/test_unit/integration/templates/integration_test.rb.tt +1 -1
  201. data/lib/rails/generators/test_unit/job/templates/unit_test.rb.tt +1 -1
  202. data/lib/rails/generators/test_unit/mailer/templates/functional_test.rb.tt +1 -1
  203. data/lib/rails/generators/test_unit/model/templates/fixtures.yml.tt +3 -3
  204. data/lib/rails/generators/test_unit/model/templates/unit_test.rb.tt +1 -1
  205. data/lib/rails/generators/test_unit/plugin/templates/%file_name%_test.rb.tt +1 -1
  206. data/lib/rails/generators/test_unit/plugin/templates/test_helper.rb +2 -2
  207. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +4 -5
  208. data/lib/rails/generators/test_unit/scaffold/templates/api_functional_test.rb.tt +6 -6
  209. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb.tt +8 -8
  210. data/lib/rails/generators/test_unit/scaffold/templates/system_test.rb.tt +9 -11
  211. data/lib/rails/generators/testing/assertions.rb +2 -2
  212. data/lib/rails/generators/testing/{behaviour.rb → behavior.rb} +8 -7
  213. data/lib/rails/generators.rb +43 -45
  214. data/lib/rails/health_controller.rb +55 -0
  215. data/lib/rails/info.rb +4 -4
  216. data/lib/rails/info_controller.rb +30 -13
  217. data/lib/rails/initializable.rb +1 -1
  218. data/lib/rails/mailers_controller.rb +18 -9
  219. data/lib/rails/paths.rb +28 -19
  220. data/lib/rails/rack/logger.rb +17 -17
  221. data/lib/rails/rackup/server.rb +15 -0
  222. data/lib/rails/railtie/configurable.rb +0 -1
  223. data/lib/rails/railtie/configuration.rb +15 -3
  224. data/lib/rails/railtie.rb +80 -37
  225. data/lib/rails/ruby_version_check.rb +5 -3
  226. data/lib/rails/secrets.rb +14 -9
  227. data/lib/rails/source_annotation_extractor.rb +69 -34
  228. data/lib/rails/tasks/engine.rake +8 -11
  229. data/lib/rails/tasks/framework.rake +4 -6
  230. data/lib/rails/tasks/log.rake +1 -1
  231. data/lib/rails/tasks/misc.rake +4 -15
  232. data/lib/rails/tasks/statistics.rake +9 -6
  233. data/lib/rails/tasks/tmp.rake +13 -6
  234. data/lib/rails/tasks/yarn.rake +17 -6
  235. data/lib/rails/tasks/zeitwerk.rake +16 -41
  236. data/lib/rails/tasks.rb +0 -6
  237. data/lib/rails/templates/layouts/application.html.erb +15 -0
  238. data/lib/rails/templates/rails/mailers/email.html.erb +46 -11
  239. data/lib/rails/templates/rails/mailers/index.html.erb +14 -7
  240. data/lib/rails/templates/rails/mailers/mailer.html.erb +11 -5
  241. data/lib/rails/templates/rails/welcome/index.html.erb +65 -48
  242. data/lib/rails/test_help.rb +13 -14
  243. data/lib/rails/test_unit/line_filtering.rb +1 -1
  244. data/lib/rails/test_unit/railtie.rb +0 -4
  245. data/lib/rails/test_unit/reporter.rb +8 -3
  246. data/lib/rails/test_unit/runner.rb +61 -19
  247. data/lib/rails/test_unit/test_parser.rb +88 -0
  248. data/lib/rails/test_unit/testing.rake +17 -36
  249. data/lib/rails/testing/maintain_test_schema.rb +16 -0
  250. data/lib/rails/version.rb +1 -1
  251. data/lib/rails/welcome_controller.rb +1 -0
  252. data/lib/rails/zeitwerk_checker.rb +15 -0
  253. data/lib/rails.rb +31 -23
  254. metadata +89 -58
  255. data/RDOC_MAIN.rdoc +0 -98
  256. data/lib/rails/application/dummy_erb_compiler.rb +0 -18
  257. data/lib/rails/command/spellchecker.rb +0 -58
  258. data/lib/rails/generators/css/assets/assets_generator.rb +0 -15
  259. data/lib/rails/generators/css/assets/templates/stylesheet.css +0 -4
  260. data/lib/rails/generators/css/scaffold/scaffold_generator.rb +0 -18
  261. data/lib/rails/generators/rails/app/templates/app/javascript/channels/consumer.js +0 -6
  262. data/lib/rails/generators/rails/app/templates/app/javascript/channels/index.js +0 -5
  263. data/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.tt +0 -23
  264. data/lib/rails/generators/rails/app/templates/bin/yarn.tt +0 -10
  265. data/lib/rails/generators/rails/app/templates/config/databases/frontbase.yml.tt +0 -50
  266. data/lib/rails/generators/rails/app/templates/config/databases/ibm_db.yml.tt +0 -86
  267. data/lib/rails/generators/rails/app/templates/config/initializers/application_controller_renderer.rb.tt +0 -8
  268. data/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb.tt +0 -7
  269. data/lib/rails/generators/rails/app/templates/config/initializers/cookies_serializer.rb.tt +0 -5
  270. data/lib/rails/generators/rails/app/templates/config/initializers/mime_types.rb.tt +0 -4
  271. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_0.rb.tt +0 -45
  272. data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +0 -16
  273. data/lib/rails/generators/rails/app/templates/config/spring.rb.tt +0 -6
  274. data/lib/rails/generators/rails/app/templates/package.json.tt +0 -11
  275. data/lib/rails/generators/rails/assets/USAGE +0 -17
  276. data/lib/rails/generators/rails/assets/assets_generator.rb +0 -26
  277. data/lib/rails/generators/rails/assets/templates/stylesheet.css +0 -4
  278. data/lib/rails/generators/rails/model/USAGE +0 -114
  279. data/lib/rails/generators/rails/plugin/templates/rails/application.rb.tt +0 -23
  280. data/lib/rails/generators/rails/scaffold/templates/scaffold.css +0 -80
  281. data/lib/rails/tasks/annotations.rake +0 -22
  282. data/lib/rails/tasks/dev.rake +0 -11
  283. data/lib/rails/tasks/initializers.rake +0 -9
  284. data/lib/rails/tasks/middleware.rake +0 -9
  285. data/lib/rails/tasks/restart.rake +0 -9
  286. data/lib/rails/tasks/routes.rake +0 -9
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "shellwords"
4
+ require "active_support/core_ext/kernel/reporting"
3
5
  require "active_support/core_ext/string/strip"
4
6
 
5
7
  module Rails
@@ -10,12 +12,58 @@ module Rails
10
12
  @indentation = 0
11
13
  end
12
14
 
13
- # Adds an entry into +Gemfile+ for the supplied gem.
15
+ # Adds a +gem+ declaration to the +Gemfile+ for the specified gem.
14
16
  #
15
17
  # gem "rspec", group: :test
16
18
  # gem "technoweenie-restful-authentication", lib: "restful-authentication", source: "http://gems.github.com/"
17
19
  # gem "rails", "3.0", git: "https://github.com/rails/rails"
18
20
  # gem "RedCloth", ">= 4.1.0", "< 4.2.0"
21
+ # gem "rspec", comment: "Put this comment above the gem declaration"
22
+ #
23
+ # Note that this method only adds the gem to the +Gemfile+; it does not
24
+ # install the gem.
25
+ #
26
+ # ==== Options
27
+ #
28
+ # [+:version+]
29
+ # The version constraints for the gem, specified as a string or an
30
+ # array of strings:
31
+ #
32
+ # gem "my_gem", version: "~> 1.1"
33
+ # gem "my_gem", version: [">= 1.1", "< 2.0"]
34
+ #
35
+ # Alternatively, can be specified as one or more arguments following the
36
+ # gem name:
37
+ #
38
+ # gem "my_gem", ">= 1.1", "< 2.0"
39
+ #
40
+ # [+:comment+]
41
+ # Outputs a comment above the +gem+ declaration in the +Gemfile+.
42
+ #
43
+ # gem "my_gem", comment: "First line.\nSecond line."
44
+ #
45
+ # Outputs:
46
+ #
47
+ # # First line.
48
+ # # Second line.
49
+ # gem "my_gem"
50
+ #
51
+ # [+:group+]
52
+ # The gem group in the +Gemfile+ that the gem belongs to.
53
+ #
54
+ # [+:git+]
55
+ # The URL of the git repository for the gem.
56
+ #
57
+ # Any additional options passed to this method will be appended to the
58
+ # +gem+ declaration in the +Gemfile+. For example:
59
+ #
60
+ # gem "my_gem", comment: "Edge my_gem", git: "https://example.com/my_gem.git", branch: "master"
61
+ #
62
+ # Outputs:
63
+ #
64
+ # # Edge my_gem
65
+ # gem "my_gem", git: "https://example.com/my_gem.git", branch: "master"
66
+ #
19
67
  def gem(*args)
20
68
  options = args.extract_options!
21
69
  name, *versions = args
@@ -24,6 +72,9 @@ module Rails
24
72
  # otherwise use name (version).
25
73
  parts, message = [ quote(name) ], name.dup
26
74
 
75
+ # Output a comment above the gem declaration.
76
+ comment = options.delete(:comment)
77
+
27
78
  if versions = versions.any? ? versions : options.delete(:version)
28
79
  _versions = Array(versions)
29
80
  _versions.each do |version|
@@ -38,10 +89,17 @@ module Rails
38
89
  parts << quote(options) unless options.empty?
39
90
 
40
91
  in_root do
41
- str = "gem #{parts.join(", ")}"
42
- str = indentation + str
43
- str = "\n" + str
44
- append_file "Gemfile", str, verbose: false
92
+ str = []
93
+ if comment
94
+ comment.each_line do |comment_line|
95
+ str << indentation
96
+ str << "# #{comment_line}"
97
+ end
98
+ str << "\n"
99
+ end
100
+ str << indentation
101
+ str << "gem #{parts.join(", ")}"
102
+ append_file_with_newline "Gemfile", str.join, verbose: false
45
103
  end
46
104
  end
47
105
 
@@ -58,9 +116,9 @@ module Rails
58
116
  log :gemfile, "group #{str}"
59
117
 
60
118
  in_root do
61
- append_file "Gemfile", "\ngroup #{str} do", force: true
119
+ append_file_with_newline "Gemfile", "\ngroup #{str} do", force: true
62
120
  with_indentation(&block)
63
- append_file "Gemfile", "\nend\n", force: true
121
+ append_file_with_newline "Gemfile", "end", force: true
64
122
  end
65
123
  end
66
124
 
@@ -71,9 +129,13 @@ module Rails
71
129
  log :github, "github #{str}"
72
130
 
73
131
  in_root do
74
- append_file "Gemfile", "\n#{indentation}github #{str} do", force: true
132
+ if @indentation.zero?
133
+ append_file_with_newline "Gemfile", "\ngithub #{str} do", force: true
134
+ else
135
+ append_file_with_newline "Gemfile", "#{indentation}github #{str} do", force: true
136
+ end
75
137
  with_indentation(&block)
76
- append_file "Gemfile", "\n#{indentation}end", force: true
138
+ append_file_with_newline "Gemfile", "#{indentation}end", force: true
77
139
  end
78
140
  end
79
141
 
@@ -91,27 +153,56 @@ module Rails
91
153
 
92
154
  in_root do
93
155
  if block
94
- append_file "Gemfile", "\nsource #{quote(source)} do", force: true
156
+ append_file_with_newline "Gemfile", "\nsource #{quote(source)} do", force: true
95
157
  with_indentation(&block)
96
- append_file "Gemfile", "\nend\n", force: true
158
+ append_file_with_newline "Gemfile", "end", force: true
97
159
  else
98
160
  prepend_file "Gemfile", "source #{quote(source)}\n", verbose: false
99
161
  end
100
162
  end
101
163
  end
102
164
 
103
- # Adds a line inside the Application class for <tt>config/application.rb</tt>.
165
+ # Adds configuration code to a \Rails runtime environment.
104
166
  #
105
- # If options <tt>:env</tt> is specified, the line is appended to the corresponding
106
- # file in <tt>config/environments</tt>.
167
+ # By default, adds code inside the +Application+ class in
168
+ # +config/application.rb+ so that it applies to all environments.
169
+ #
170
+ # environment %(config.asset_host = "cdn.provider.com")
171
+ #
172
+ # Results in:
173
+ #
174
+ # # config/application.rb
175
+ # class Application < Rails::Application
176
+ # config.asset_host = "cdn.provider.com"
177
+ # # ...
178
+ # end
179
+ #
180
+ # If the +:env+ option is specified, the code will be added to the
181
+ # corresponding file in +config/environments+ instead.
182
+ #
183
+ # environment %(config.asset_host = "localhost:3000"), env: "development"
184
+ #
185
+ # Results in:
186
+ #
187
+ # # config/environments/development.rb
188
+ # Rails.application.configure do
189
+ # config.asset_host = "localhost:3000"
190
+ # # ...
191
+ # end
192
+ #
193
+ # +:env+ can also be an array. In which case, the code is added to each
194
+ # corresponding file in +config/environments+.
195
+ #
196
+ # The code can also be specified as the return value of the block:
107
197
  #
108
198
  # environment do
109
- # "config.action_controller.asset_host = 'cdn.provider.com'"
199
+ # %(config.asset_host = "cdn.provider.com")
110
200
  # end
111
201
  #
112
202
  # environment(nil, env: "development") do
113
- # "config.action_controller.asset_host = 'localhost:3000'"
203
+ # %(config.asset_host = "localhost:3000")
114
204
  # end
205
+ #
115
206
  def environment(data = nil, options = {})
116
207
  sentinel = "class Application < Rails::Application\n"
117
208
  env_file_sentinel = "Rails.application.configure do\n"
@@ -129,11 +220,20 @@ module Rails
129
220
  end
130
221
  alias :application :environment
131
222
 
132
- # Run a command in git.
223
+ # Runs one or more git commands.
133
224
  #
134
225
  # git :init
226
+ # # => runs `git init`
227
+ #
135
228
  # git add: "this.file that.rb"
136
- # git add: "onefile.rb", rm: "badfile.cxx"
229
+ # # => runs `git add this.file that.rb`
230
+ #
231
+ # git commit: "-m 'First commit'"
232
+ # # => runs `git commit -m 'First commit'`
233
+ #
234
+ # git add: "good.rb", rm: "bad.cxx"
235
+ # # => runs `git add good.rb; git rm bad.cxx`
236
+ #
137
237
  def git(commands = {})
138
238
  if commands.is_a?(Symbol)
139
239
  run "git #{commands}"
@@ -144,119 +244,195 @@ module Rails
144
244
  end
145
245
  end
146
246
 
147
- # Create a new file in the <tt>vendor/</tt> directory. Code can be specified
148
- # in a block or a data string can be given.
247
+ # Creates a file in +vendor/+. The contents can be specified as an
248
+ # argument or as the return value of the block.
149
249
  #
150
- # vendor("sekrit.rb") do
151
- # sekrit_salt = "#{Time.now}--#{3.years.ago}--#{rand}--"
152
- # "salt = '#{sekrit_salt}'"
250
+ # vendor "foreign.rb", <<~RUBY
251
+ # # Foreign code is fun
252
+ # RUBY
253
+ #
254
+ # vendor "foreign.rb" do
255
+ # "# Foreign code is fun"
153
256
  # end
154
257
  #
155
- # vendor("foreign.rb", "# Foreign code is fun")
156
258
  def vendor(filename, data = nil)
157
259
  log :vendor, filename
158
260
  data ||= yield if block_given?
159
261
  create_file("vendor/#{filename}", optimize_indentation(data), verbose: false)
160
262
  end
161
263
 
162
- # Create a new file in the <tt>lib/</tt> directory. Code can be specified
163
- # in a block or a data string can be given.
264
+ # Creates a file in +lib/+. The contents can be specified as an argument
265
+ # or as the return value of the block.
266
+ #
267
+ # lib "foreign.rb", <<~RUBY
268
+ # # Foreign code is fun
269
+ # RUBY
164
270
  #
165
- # lib("crypto.rb") do
166
- # "crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'"
271
+ # lib "foreign.rb" do
272
+ # "# Foreign code is fun"
167
273
  # end
168
274
  #
169
- # lib("foreign.rb", "# Foreign code is fun")
170
275
  def lib(filename, data = nil)
171
276
  log :lib, filename
172
277
  data ||= yield if block_given?
173
278
  create_file("lib/#{filename}", optimize_indentation(data), verbose: false)
174
279
  end
175
280
 
176
- # Create a new +Rakefile+ with the provided code (either in a block or a string).
281
+ # Creates a Rake tasks file in +lib/tasks/+. The code can be specified as
282
+ # an argument or as the return value of the block.
177
283
  #
178
- # rakefile("bootstrap.rake") do
284
+ # rakefile "bootstrap.rake", <<~RUBY
285
+ # task :bootstrap do
286
+ # puts "Boots! Boots! Boots!"
287
+ # end
288
+ # RUBY
289
+ #
290
+ # rakefile "bootstrap.rake" do
179
291
  # project = ask("What is the UNIX name of your project?")
180
292
  #
181
- # <<-TASK
293
+ # <<~RUBY
182
294
  # namespace :#{project} do
183
295
  # task :bootstrap do
184
- # puts "I like boots!"
296
+ # puts "Boots! Boots! Boots!"
185
297
  # end
186
298
  # end
187
- # TASK
299
+ # RUBY
188
300
  # end
189
301
  #
190
- # rakefile('seed.rake', 'puts "Planting seeds"')
191
302
  def rakefile(filename, data = nil)
192
303
  log :rakefile, filename
193
304
  data ||= yield if block_given?
194
305
  create_file("lib/tasks/#{filename}", optimize_indentation(data), verbose: false)
195
306
  end
196
307
 
197
- # Create a new initializer with the provided code (either in a block or a string).
198
- #
199
- # initializer("globals.rb") do
200
- # data = ""
308
+ # Creates an initializer file in +config/initializers/+. The code can be
309
+ # specified as an argument or as the return value of the block.
201
310
  #
202
- # ['MY_WORK', 'ADMINS', 'BEST_COMPANY_EVAR'].each do |const|
203
- # data << "#{const} = :entp\n"
204
- # end
311
+ # initializer "api.rb", <<~RUBY
312
+ # API_KEY = "123456"
313
+ # RUBY
205
314
  #
206
- # data
315
+ # initializer "api.rb" do
316
+ # %(API_KEY = "123456")
207
317
  # end
208
318
  #
209
- # initializer("api.rb", "API_KEY = '123456'")
210
319
  def initializer(filename, data = nil)
211
320
  log :initializer, filename
212
321
  data ||= yield if block_given?
213
322
  create_file("config/initializers/#{filename}", optimize_indentation(data), verbose: false)
214
323
  end
215
324
 
216
- # Generate something using a generator from Rails or a plugin.
217
- # The second parameter is the argument string that is passed to
218
- # the generator or an Array that is joined.
325
+ # Runs another generator.
326
+ #
327
+ # generate "scaffold", "Post title:string body:text"
328
+ # generate "scaffold", "Post", "title:string", "body:text"
219
329
  #
220
- # generate(:authenticated, "user session")
330
+ # The first argument is the generator name, and the remaining arguments
331
+ # are joined together and passed to the generator.
221
332
  def generate(what, *args)
222
333
  log :generate, what
223
334
 
224
335
  options = args.extract_options!
225
- options[:without_rails_env] = true
226
- argument = args.flat_map(&:to_s).join(" ")
336
+ options[:abort_on_failure] = !options[:inline]
227
337
 
228
- execute_command :rails, "generate #{what} #{argument}", options
338
+ rails_command "generate #{what} #{args.join(" ")}", options
229
339
  end
230
340
 
231
- # Runs the supplied rake task (invoked with 'rake ...')
341
+ # Runs the specified Rake task.
342
+ #
343
+ # rake "db:migrate"
344
+ # rake "db:migrate", env: "production"
345
+ # rake "db:migrate", abort_on_failure: true
346
+ # rake "stats", capture: true
347
+ # rake "gems:install", sudo: true
348
+ #
349
+ # ==== Options
350
+ #
351
+ # [+:env+]
352
+ # The \Rails environment in which to run the task. Defaults to
353
+ # <tt>ENV["RAILS_ENV"] || "development"</tt>.
354
+ #
355
+ # [+:abort_on_failure+]
356
+ # Whether to halt the generator if the task exits with a non-success
357
+ # exit status.
232
358
  #
233
- # rake("db:migrate")
234
- # rake("db:migrate", env: "production")
235
- # rake("gems:install", sudo: true)
236
- # rake("gems:install", capture: true)
359
+ # [+:capture+]
360
+ # Whether to capture and return the output of the task.
361
+ #
362
+ # [+:sudo+]
363
+ # Whether to run the task using +sudo+.
237
364
  def rake(command, options = {})
238
365
  execute_command :rake, command, options
239
366
  end
240
367
 
241
- # Runs the supplied rake task (invoked with 'rails ...')
368
+ # Runs the specified \Rails command.
369
+ #
370
+ # rails_command "db:migrate"
371
+ # rails_command "db:migrate", env: "production"
372
+ # rails_command "db:migrate", abort_on_failure: true
373
+ # rails_command "stats", capture: true
374
+ # rails_command "gems:install", sudo: true
375
+ #
376
+ # ==== Options
377
+ #
378
+ # [+:env+]
379
+ # The \Rails environment in which to run the command. Defaults to
380
+ # <tt>ENV["RAILS_ENV"] || "development"</tt>.
381
+ #
382
+ # [+:abort_on_failure+]
383
+ # Whether to halt the generator if the command exits with a non-success
384
+ # exit status.
242
385
  #
243
- # rails_command("db:migrate")
244
- # rails_command("db:migrate", env: "production")
245
- # rails_command("gems:install", sudo: true)
246
- # rails_command("gems:install", capture: true)
386
+ # [+:capture+]
387
+ # Whether to capture and return the output of the command.
388
+ #
389
+ # [+:sudo+]
390
+ # Whether to run the command using +sudo+.
247
391
  def rails_command(command, options = {})
248
- execute_command :rails, command, options
392
+ if options[:inline]
393
+ log :rails, command
394
+ command, *args = Shellwords.split(command)
395
+ in_root do
396
+ silence_warnings do
397
+ ::Rails::Command.invoke(command, args, **options)
398
+ end
399
+ end
400
+ else
401
+ execute_command :rails, command, options
402
+ end
249
403
  end
250
404
 
251
- # Make an entry in Rails routing file <tt>config/routes.rb</tt>
405
+ # Make an entry in \Rails routing file <tt>config/routes.rb</tt>
252
406
  #
253
407
  # route "root 'welcome#index'"
254
- def route(routing_code)
408
+ # route "root 'admin#index'", namespace: :admin
409
+ def route(routing_code, namespace: nil)
410
+ namespace = Array(namespace)
411
+ namespace_pattern = route_namespace_pattern(namespace)
412
+ routing_code = namespace.reverse.reduce(routing_code) do |code, name|
413
+ "namespace :#{name} do\n#{rebase_indentation(code, 2)}end"
414
+ end
415
+
255
416
  log :route, routing_code
256
- sentinel = /\.routes\.draw do\s*\n/m
257
417
 
258
418
  in_root do
259
- inject_into_file "config/routes.rb", optimize_indentation(routing_code, 2), after: sentinel, verbose: false, force: false
419
+ if namespace_match = match_file("config/routes.rb", namespace_pattern)
420
+ base_indent, *, existing_block_indent = namespace_match.captures.compact.map(&:length)
421
+ existing_line_pattern = /^[ ]{,#{existing_block_indent}}\S.+\n?/
422
+ routing_code = rebase_indentation(routing_code, base_indent + 2).gsub(existing_line_pattern, "")
423
+ namespace_pattern = /#{Regexp.escape namespace_match.to_s}/
424
+ end
425
+
426
+ inject_into_file "config/routes.rb", routing_code, after: namespace_pattern, verbose: false, force: false
427
+
428
+ if behavior == :revoke && namespace.any? && namespace_match
429
+ empty_block_pattern = /(#{namespace_pattern})((?:\s*end\n){1,#{namespace.size}})/
430
+ gsub_file "config/routes.rb", empty_block_pattern, verbose: false, force: true do |matched|
431
+ beginning, ending = empty_block_pattern.match(matched).captures
432
+ ending.sub!(/\A\s*end\n/, "") while !ending.empty? && beginning.sub!(/^[ ]*namespace .+ do\n\s*\z/, "")
433
+ beginning + ending
434
+ end
435
+ end
260
436
  end
261
437
  end
262
438
 
@@ -268,7 +444,6 @@ module Rails
268
444
  end
269
445
 
270
446
  private
271
-
272
447
  # Define log for backwards compatibility. If just one argument is sent,
273
448
  # invoke say, otherwise invoke say_status. Differently from say and
274
449
  # similarly to say_status, this method respects the quiet? option given.
@@ -285,28 +460,18 @@ module Rails
285
460
  # based on the executor parameter provided.
286
461
  def execute_command(executor, command, options = {}) # :doc:
287
462
  log executor, command
288
- env = options[:env] || ENV["RAILS_ENV"] || "development"
289
- rails_env = " RAILS_ENV=#{env}" unless options[:without_rails_env]
290
463
  sudo = options[:sudo] && !Gem.win_platform? ? "sudo " : ""
291
- config = { verbose: false }
292
-
293
- config[:capture] = options[:capture] if options[:capture]
294
- config[:abort_on_failure] = options[:abort_on_failure] if options[:abort_on_failure]
295
-
296
- in_root { run("#{sudo}#{extify(executor)} #{command}#{rails_env}", config) }
464
+ config = {
465
+ env: { "RAILS_ENV" => (options[:env] || ENV["RAILS_ENV"] || "development") },
466
+ verbose: false,
467
+ capture: options[:capture],
468
+ abort_on_failure: options[:abort_on_failure],
469
+ }
470
+
471
+ in_root { run("#{sudo}#{Shellwords.escape Gem.ruby} bin/#{executor} #{command}", config) }
297
472
  end
298
473
 
299
- # Add an extension to the given name based on the platform.
300
- def extify(name) # :doc:
301
- if Gem.win_platform?
302
- "#{name}.bat"
303
- else
304
- name
305
- end
306
- end
307
-
308
- # Surround string with single quotes if there is no quotes.
309
- # Otherwise fall back to double quotes
474
+ # Always returns value in double quotes.
310
475
  def quote(value) # :doc:
311
476
  if value.respond_to? :each_pair
312
477
  return value.map do |k, v|
@@ -315,23 +480,15 @@ module Rails
315
480
  end
316
481
  return value.inspect unless value.is_a? String
317
482
 
318
- if value.include?("'")
319
- value.inspect
320
- else
321
- "'#{value}'"
322
- end
483
+ "\"#{value.tr("'", '"')}\""
323
484
  end
324
485
 
325
486
  # Returns optimized string with indentation
326
487
  def optimize_indentation(value, amount = 0) # :doc:
327
488
  return "#{value}\n" unless value.is_a?(String)
328
-
329
- if value.lines.size > 1
330
- value.strip_heredoc.indent(amount)
331
- else
332
- "#{value.strip.indent(amount)}\n"
333
- end
489
+ "#{value.strip_heredoc.indent(amount).chomp}\n"
334
490
  end
491
+ alias rebase_indentation optimize_indentation
335
492
 
336
493
  # Indent the +Gemfile+ to the depth of @indentation
337
494
  def indentation # :doc:
@@ -345,6 +502,27 @@ module Rails
345
502
  ensure
346
503
  @indentation -= 1
347
504
  end
505
+
506
+ # Append string to a file with a newline if necessary
507
+ def append_file_with_newline(path, str, options = {})
508
+ gsub_file path, /\n?\z/, options do |match|
509
+ match.end_with?("\n") ? "" : "\n#{str}\n"
510
+ end
511
+ end
512
+
513
+ def match_file(path, pattern)
514
+ File.read(path).match(pattern) if File.exist?(path)
515
+ end
516
+
517
+ def route_namespace_pattern(namespace)
518
+ namespace.each_with_index.reverse_each.reduce(nil) do |pattern, (name, i)|
519
+ cummulative_margin = "\\#{i + 1}[ ]{2}"
520
+ blank_or_indented_line = "^[ ]*\n|^#{cummulative_margin}.*\n"
521
+ "(?:(?:#{blank_or_indented_line})*?^(#{cummulative_margin})namespace :#{name} do\n#{pattern})?"
522
+ end.then do |pattern|
523
+ /^([ ]*).+\.routes\.draw do[ ]*\n#{pattern}/
524
+ end
525
+ end
348
526
  end
349
527
  end
350
528
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Rails
4
4
  module Generators
5
- # ActiveModel is a class to be implemented by each ORM to allow Rails to
5
+ # ActiveModel is a class to be implemented by each ORM to allow \Rails to
6
6
  # generate customized controller code.
7
7
  #
8
8
  # The API has the same methods as ActiveRecord, but each method returns a
@@ -32,21 +32,27 @@ module Rails
32
32
  @name = name
33
33
  end
34
34
 
35
- # GET index
35
+ # Used for:
36
+ #
37
+ # * GET +index+
36
38
  def self.all(klass)
37
39
  "#{klass}.all"
38
40
  end
39
41
 
40
- # GET show
41
- # GET edit
42
- # PATCH/PUT update
43
- # DELETE destroy
42
+ # Used for:
43
+ #
44
+ # * GET +show+
45
+ # * GET +edit+
46
+ # * PATCH / PUT +update+
47
+ # * DELETE +destroy+
44
48
  def self.find(klass, params = nil)
45
49
  "#{klass}.find(#{params})"
46
50
  end
47
51
 
48
- # GET new
49
- # POST create
52
+ # Used for:
53
+ #
54
+ # * GET +new+
55
+ # * POST +create+
50
56
  def self.build(klass, params = nil)
51
57
  if params
52
58
  "#{klass}.new(#{params})"
@@ -55,25 +61,33 @@ module Rails
55
61
  end
56
62
  end
57
63
 
58
- # POST create
64
+ # Used for:
65
+ #
66
+ # * POST +create+
59
67
  def save
60
68
  "#{name}.save"
61
69
  end
62
70
 
63
- # PATCH/PUT update
71
+ # Used for:
72
+ #
73
+ # * PATCH / PUT +update+
64
74
  def update(params = nil)
65
75
  "#{name}.update(#{params})"
66
76
  end
67
77
 
68
- # POST create
69
- # PATCH/PUT update
78
+ # Used for:
79
+ #
80
+ # * POST +create+
81
+ # * PATCH / PUT +update+
70
82
  def errors
71
83
  "#{name}.errors"
72
84
  end
73
85
 
74
- # DELETE destroy
86
+ # Used for:
87
+ #
88
+ # * DELETE +destroy+
75
89
  def destroy
76
- "#{name}.destroy"
90
+ "#{name}.destroy!"
77
91
  end
78
92
  end
79
93
  end