railties 4.2.11.3 → 5.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +262 -206
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +1 -1
  5. data/{bin → exe}/rails +0 -0
  6. data/lib/rails.rb +19 -1
  7. data/lib/rails/all.rb +10 -9
  8. data/lib/rails/api/task.rb +7 -14
  9. data/lib/rails/{app_rails_loader.rb → app_loader.rb} +4 -3
  10. data/lib/rails/application.rb +31 -41
  11. data/lib/rails/application/bootstrap.rb +1 -14
  12. data/lib/rails/application/configuration.rb +94 -95
  13. data/lib/rails/application/default_middleware_stack.rb +30 -16
  14. data/lib/rails/application/finisher.rb +11 -2
  15. data/lib/rails/application/routes_reloader.rb +1 -3
  16. data/lib/rails/application_controller.rb +1 -1
  17. data/lib/rails/cli.rb +2 -2
  18. data/lib/rails/code_statistics.rb +6 -8
  19. data/lib/rails/code_statistics_calculator.rb +8 -1
  20. data/lib/rails/command.rb +70 -0
  21. data/lib/rails/commands.rb +5 -3
  22. data/lib/rails/commands/commands_tasks.rb +25 -14
  23. data/lib/rails/commands/console.rb +7 -52
  24. data/lib/rails/commands/console_helper.rb +34 -0
  25. data/lib/rails/commands/dbconsole.rb +59 -78
  26. data/lib/rails/commands/dev_cache.rb +21 -0
  27. data/lib/rails/commands/plugin.rb +1 -1
  28. data/lib/rails/commands/rake_proxy.rb +34 -0
  29. data/lib/rails/commands/runner.rb +0 -1
  30. data/lib/rails/commands/server.rb +29 -36
  31. data/lib/rails/commands/test.rb +9 -0
  32. data/lib/rails/configuration.rb +21 -5
  33. data/lib/rails/console/app.rb +5 -0
  34. data/lib/rails/console/helpers.rb +1 -1
  35. data/lib/rails/engine.rb +68 -58
  36. data/lib/rails/engine/commands.rb +4 -2
  37. data/lib/rails/engine/configuration.rb +4 -10
  38. data/lib/rails/gem_version.rb +4 -4
  39. data/lib/rails/generators.rb +30 -7
  40. data/lib/rails/generators/.named_base.rb.swp +0 -0
  41. data/lib/rails/generators/actions.rb +19 -5
  42. data/lib/rails/generators/app_base.rb +65 -45
  43. data/lib/rails/generators/base.rb +6 -6
  44. data/lib/rails/generators/erb/mailer/mailer_generator.rb +5 -8
  45. data/lib/rails/generators/erb/scaffold/templates/_form.html.erb +9 -7
  46. data/lib/rails/generators/erb/scaffold/templates/edit.html.erb +1 -1
  47. data/lib/rails/generators/erb/scaffold/templates/index.html.erb +2 -2
  48. data/lib/rails/generators/erb/scaffold/templates/new.html.erb +1 -1
  49. data/lib/rails/generators/generated_attribute.rb +5 -1
  50. data/lib/rails/generators/migration.rb +7 -7
  51. data/lib/rails/generators/named_base.rb +21 -5
  52. data/lib/rails/generators/rails/app/USAGE +0 -1
  53. data/lib/rails/generators/rails/app/app_generator.rb +75 -11
  54. data/lib/rails/generators/rails/app/templates/Gemfile +17 -13
  55. data/lib/rails/generators/rails/app/templates/{README.rdoc → README.md} +1 -5
  56. data/lib/rails/generators/rails/app/templates/app/assets/config/manifest.js.tt +7 -0
  57. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +1 -1
  58. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/cable.coffee +11 -0
  59. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +3 -3
  60. data/lib/rails/generators/rails/app/templates/app/channels/application_cable/channel.rb +5 -0
  61. data/lib/rails/generators/rails/app/templates/app/channels/application_cable/connection.rb +5 -0
  62. data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt +3 -1
  63. data/lib/rails/generators/rails/app/templates/app/jobs/application_job.rb +2 -0
  64. data/lib/rails/generators/rails/app/templates/app/mailers/application_mailer.rb +4 -0
  65. data/lib/rails/generators/rails/app/templates/app/models/application_record.rb +3 -0
  66. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +20 -17
  67. data/lib/rails/generators/rails/app/templates/app/views/layouts/mailer.html.erb.tt +13 -0
  68. data/lib/rails/generators/{erb/mailer/templates/layout.text.erb → rails/app/templates/app/views/layouts/mailer.text.erb.tt} +0 -0
  69. data/lib/rails/generators/rails/app/templates/bin/setup +17 -12
  70. data/lib/rails/generators/rails/app/templates/bin/update +28 -0
  71. data/lib/rails/generators/rails/app/templates/config.ru.tt +11 -0
  72. data/lib/rails/generators/rails/app/templates/config/application.rb +9 -14
  73. data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml +1 -1
  74. data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +2 -2
  75. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +20 -3
  76. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +18 -11
  77. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +7 -3
  78. data/lib/rails/generators/rails/app/templates/config/initializers/active_record_belongs_to_required_by_default.rb +5 -0
  79. data/lib/rails/generators/rails/app/templates/config/initializers/application_controller_renderer.rb +6 -0
  80. data/lib/rails/generators/rails/app/templates/config/initializers/callback_terminator.rb +5 -0
  81. data/lib/rails/generators/rails/app/templates/config/initializers/cookies_serializer.rb +1 -0
  82. data/lib/rails/generators/rails/app/templates/config/initializers/cors.rb +16 -0
  83. data/lib/rails/generators/rails/app/templates/config/initializers/request_forgery_protection.rb +4 -0
  84. data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +2 -2
  85. data/lib/rails/generators/rails/app/templates/config/redis/cable.yml +9 -0
  86. data/lib/rails/generators/rails/app/templates/config/routes.rb +3 -53
  87. data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +3 -3
  88. data/lib/rails/generators/rails/app/templates/gitignore +7 -1
  89. data/lib/rails/generators/rails/controller/controller_generator.rb +6 -4
  90. data/lib/rails/generators/rails/migration/migration_generator.rb +1 -1
  91. data/lib/rails/generators/rails/model/USAGE +6 -1
  92. data/lib/rails/generators/rails/model/model_generator.rb +1 -1
  93. data/lib/rails/generators/rails/plugin/plugin_generator.rb +88 -35
  94. data/lib/rails/generators/rails/plugin/templates/%name%.gemspec +6 -9
  95. data/lib/rails/generators/rails/plugin/templates/Gemfile +3 -7
  96. data/lib/rails/generators/rails/plugin/templates/README.md +3 -0
  97. data/lib/rails/generators/rails/plugin/templates/Rakefile +2 -2
  98. data/lib/rails/generators/rails/plugin/templates/app/controllers/%namespaced_name%/application_controller.rb.tt +5 -0
  99. data/lib/rails/generators/rails/plugin/templates/app/helpers/%namespaced_name%/application_helper.rb.tt +5 -0
  100. data/lib/rails/generators/rails/plugin/templates/app/jobs/%namespaced_name%/application_job.rb.tt +5 -0
  101. data/lib/rails/generators/rails/plugin/templates/app/models/application_record.rb.tt +6 -0
  102. data/lib/rails/generators/rails/plugin/templates/app/views/layouts/%namespaced_name%/application.html.erb.tt +14 -0
  103. data/lib/rails/generators/rails/plugin/templates/bin/rails.tt +1 -1
  104. data/lib/rails/generators/rails/plugin/templates/bin/test.tt +8 -0
  105. data/lib/rails/generators/rails/plugin/templates/config/routes.rb +1 -1
  106. data/lib/rails/generators/rails/plugin/templates/gitignore +2 -3
  107. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%.rb +5 -0
  108. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/engine.rb +7 -0
  109. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/version.rb +1 -0
  110. data/lib/rails/generators/rails/plugin/templates/lib/tasks/{%name%_tasks.rake → %namespaced_name%_tasks.rake} +1 -1
  111. data/lib/rails/generators/rails/plugin/templates/rails/application.rb +3 -3
  112. data/lib/rails/generators/rails/plugin/templates/rails/dummy_manifest.js +11 -0
  113. data/lib/rails/generators/rails/plugin/templates/rails/engine_manifest.js +6 -0
  114. data/lib/rails/generators/rails/plugin/templates/rails/javascripts.js +1 -1
  115. data/lib/rails/generators/rails/plugin/templates/rails/routes.rb +1 -2
  116. data/lib/rails/generators/rails/plugin/templates/rails/stylesheets.css +3 -3
  117. data/lib/rails/generators/rails/plugin/templates/test/%namespaced_name%_test.rb +7 -0
  118. data/lib/rails/generators/rails/plugin/templates/test/test_helper.rb +4 -2
  119. data/lib/rails/generators/rails/resource/resource_generator.rb +0 -1
  120. data/lib/rails/generators/rails/resource_route/resource_route_generator.rb +0 -1
  121. data/lib/rails/generators/rails/scaffold/USAGE +1 -1
  122. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +2 -1
  123. data/lib/rails/generators/rails/scaffold/templates/scaffold.css +35 -7
  124. data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +4 -1
  125. data/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb +61 -0
  126. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +1 -1
  127. data/lib/rails/generators/resource_helpers.rb +3 -3
  128. data/lib/rails/generators/test_unit/controller/templates/.functional_test.rb.swp +0 -0
  129. data/lib/rails/generators/test_unit/controller/templates/functional_test.rb +3 -5
  130. data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +8 -3
  131. data/lib/rails/generators/test_unit/mailer/templates/functional_test.rb +2 -2
  132. data/lib/rails/generators/test_unit/mailer/templates/preview.rb +4 -4
  133. data/lib/rails/generators/test_unit/model/model_generator.rb +1 -1
  134. data/lib/rails/generators/test_unit/model/templates/fixtures.yml +2 -0
  135. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +5 -1
  136. data/lib/rails/generators/test_unit/scaffold/templates/.api_functional_test.rb.swp +0 -0
  137. data/lib/rails/generators/test_unit/scaffold/templates/.functional_test.rb.swp +0 -0
  138. data/lib/rails/generators/test_unit/scaffold/templates/api_functional_test.rb +44 -0
  139. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +14 -14
  140. data/lib/rails/generators/testing/assertions.rb +0 -2
  141. data/lib/rails/generators/testing/behaviour.rb +4 -17
  142. data/lib/rails/info.rb +2 -2
  143. data/lib/rails/info_controller.rb +23 -2
  144. data/lib/rails/mailers_controller.rb +2 -2
  145. data/lib/rails/paths.rb +14 -6
  146. data/lib/rails/rack.rb +1 -3
  147. data/lib/rails/rack/debugger.rb +2 -23
  148. data/lib/rails/rack/logger.rb +4 -0
  149. data/lib/rails/ruby_version_check.rb +3 -3
  150. data/lib/rails/source_annotation_extractor.rb +1 -1
  151. data/lib/rails/tasks.rb +4 -1
  152. data/lib/rails/tasks/engine.rake +1 -1
  153. data/lib/rails/tasks/framework.rake +18 -16
  154. data/lib/rails/tasks/initializers.rake +6 -0
  155. data/lib/rails/tasks/restart.rake +5 -0
  156. data/lib/rails/tasks/statistics.rake +5 -6
  157. data/lib/rails/tasks/tmp.rake +4 -12
  158. data/lib/rails/templates/rails/mailers/email.html.erb +17 -3
  159. data/lib/rails/templates/rails/welcome/index.html.erb +10 -3
  160. data/lib/rails/test_help.rb +7 -8
  161. data/lib/rails/test_unit/minitest_plugin.rb +91 -0
  162. data/lib/rails/test_unit/reporter.rb +74 -0
  163. data/lib/rails/test_unit/test_requirer.rb +28 -0
  164. data/lib/rails/test_unit/testing.rake +18 -39
  165. metadata +80 -33
  166. data/lib/rails/deprecation.rb +0 -19
  167. data/lib/rails/generators/erb/mailer/templates/layout.html.erb +0 -5
  168. data/lib/rails/generators/rails/app/templates/config.ru +0 -4
  169. data/lib/rails/generators/rails/app/templates/config/initializers/to_time_preserves_timezone.rb +0 -10
  170. data/lib/rails/generators/rails/plugin/templates/README.rdoc +0 -3
  171. data/lib/rails/generators/rails/plugin/templates/app/controllers/%name%/application_controller.rb.tt +0 -5
  172. data/lib/rails/generators/rails/plugin/templates/app/helpers/%name%/application_helper.rb.tt +0 -4
  173. data/lib/rails/generators/rails/plugin/templates/app/views/layouts/%name%/application.html.erb.tt +0 -14
  174. data/lib/rails/generators/rails/plugin/templates/lib/%name%.rb +0 -6
  175. data/lib/rails/generators/rails/plugin/templates/lib/%name%/engine.rb +0 -7
  176. data/lib/rails/generators/rails/plugin/templates/lib/%name%/version.rb +0 -3
  177. data/lib/rails/generators/rails/plugin/templates/test/%name%_test.rb +0 -7
  178. data/lib/rails/rack/log_tailer.rb +0 -38
  179. data/lib/rails/tasks/documentation.rake +0 -70
  180. data/lib/rails/test_unit/sub_test_task.rb +0 -126
@@ -17,8 +17,11 @@ module Rails
17
17
 
18
18
  middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header
19
19
 
20
- if config.serve_static_files
21
- middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control
20
+ if config.public_file_server.enabled
21
+ headers = config.public_file_server.headers || {}
22
+ headers['Cache-Control'.freeze] = config.static_cache_control if config.static_cache_control
23
+
24
+ middleware.use ::ActionDispatch::Static, paths["public"].first, index: config.public_file_server.index_name, headers: headers
22
25
  end
23
26
 
24
27
  if rack_cache = load_rack_cache
@@ -26,15 +29,35 @@ module Rails
26
29
  middleware.use ::Rack::Cache, rack_cache
27
30
  end
28
31
 
29
- middleware.use ::Rack::Lock unless allow_concurrency?
32
+ if config.allow_concurrency == false
33
+ # User has explicitly opted out of concurrent request
34
+ # handling: presumably their code is not threadsafe
35
+
36
+ middleware.use ::Rack::Lock
37
+
38
+ elsif config.allow_concurrency == :unsafe
39
+ # Do nothing, even if we know this is dangerous. This is the
40
+ # historical behaviour for true.
41
+
42
+ else
43
+ # Default concurrency setting: enabled, but safe
44
+
45
+ unless config.cache_classes && config.eager_load
46
+ # Without cache_classes + eager_load, the load interlock
47
+ # is required for proper operation
48
+
49
+ middleware.use ::ActionDispatch::LoadInterlock
50
+ end
51
+ end
52
+
30
53
  middleware.use ::Rack::Runtime
31
- middleware.use ::Rack::MethodOverride
54
+ middleware.use ::Rack::MethodOverride unless config.api_only
32
55
  middleware.use ::ActionDispatch::RequestId
33
56
 
34
57
  # Must come after Rack::MethodOverride to properly log overridden methods
35
58
  middleware.use ::Rails::Rack::Logger, config.log_tags
36
59
  middleware.use ::ActionDispatch::ShowExceptions, show_exceptions_app
37
- middleware.use ::ActionDispatch::DebugExceptions, app
60
+ middleware.use ::ActionDispatch::DebugExceptions, app, config.debug_exception_response_format
38
61
  middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies
39
62
 
40
63
  unless config.cache_classes
@@ -42,9 +65,9 @@ module Rails
42
65
  end
43
66
 
44
67
  middleware.use ::ActionDispatch::Callbacks
45
- middleware.use ::ActionDispatch::Cookies
68
+ middleware.use ::ActionDispatch::Cookies unless config.api_only
46
69
 
47
- if config.session_store
70
+ if !config.api_only && config.session_store
48
71
  if config.force_ssl && !config.session_options.key?(:secure)
49
72
  config.session_options[:secure] = true
50
73
  end
@@ -52,7 +75,6 @@ module Rails
52
75
  middleware.use ::ActionDispatch::Flash
53
76
  end
54
77
 
55
- middleware.use ::ActionDispatch::ParamsParser
56
78
  middleware.use ::Rack::Head
57
79
  middleware.use ::Rack::ConditionalGet
58
80
  middleware.use ::Rack::ETag, "no-cache"
@@ -65,14 +87,6 @@ module Rails
65
87
  config.reload_classes_only_on_change != true || app.reloaders.map(&:updated?).any?
66
88
  end
67
89
 
68
- def allow_concurrency?
69
- if config.allow_concurrency.nil?
70
- config.cache_classes && config.eager_load
71
- else
72
- config.allow_concurrency
73
- end
74
- end
75
-
76
90
  def load_rack_cache
77
91
  rack_cache = config.action_dispatch.rack_cache
78
92
  return unless rack_cache
@@ -86,8 +86,10 @@ module Rails
86
86
  # added in the hook are taken into account.
87
87
  initializer :set_clear_dependencies_hook, group: :all do
88
88
  callback = lambda do
89
- ActiveSupport::DescendantsTracker.clear
90
- ActiveSupport::Dependencies.clear
89
+ ActiveSupport::Dependencies.interlock.attempt_unloading do
90
+ ActiveSupport::DescendantsTracker.clear
91
+ ActiveSupport::Dependencies.clear
92
+ end
91
93
  end
92
94
 
93
95
  if config.reload_classes_only_on_change
@@ -108,6 +110,13 @@ module Rails
108
110
  ActionDispatch::Reloader.to_cleanup(&callback)
109
111
  end
110
112
  end
113
+
114
+ # Disable dependency loading during request cycle
115
+ initializer :disable_dependency_loading do
116
+ if config.eager_load && config.cache_classes
117
+ ActiveSupport::Dependencies.unhook!
118
+ end
119
+ end
111
120
  end
112
121
  end
113
122
  end
@@ -41,9 +41,7 @@ module Rails
41
41
  end
42
42
 
43
43
  def finalize!
44
- route_sets.each do |routes|
45
- routes.finalize!
46
- end
44
+ route_sets.each(&:finalize!)
47
45
  end
48
46
 
49
47
  def revert
@@ -6,7 +6,7 @@ class Rails::ApplicationController < ActionController::Base # :nodoc:
6
6
 
7
7
  def require_local!
8
8
  unless local_request?
9
- render text: '<p>For security purposes, this information is only available to local requests.</p>', status: :forbidden
9
+ render html: '<p>For security purposes, this information is only available to local requests.</p>'.html_safe, status: :forbidden
10
10
  end
11
11
  end
12
12
 
@@ -1,8 +1,8 @@
1
- require 'rails/app_rails_loader'
1
+ require 'rails/app_loader'
2
2
 
3
3
  # If we are inside a Rails application this method performs an exec and thus
4
4
  # the rest of this script is not run.
5
- Rails::AppRailsLoader.exec_app_rails
5
+ Rails::AppLoader.exec_app
6
6
 
7
7
  require 'rails/ruby_version_check'
8
8
  Signal.trap("INT") { puts; exit(1) }
@@ -7,9 +7,7 @@ class CodeStatistics #:nodoc:
7
7
  'Model tests',
8
8
  'Mailer tests',
9
9
  'Job tests',
10
- 'Integration tests',
11
- 'Functional tests (old)',
12
- 'Unit tests (old)']
10
+ 'Integration tests']
13
11
 
14
12
  def initialize(*pairs)
15
13
  @pairs = pairs
@@ -35,7 +33,7 @@ class CodeStatistics #:nodoc:
35
33
  Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}]
36
34
  end
37
35
 
38
- def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee)$/)
36
+ def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee|rake)$/)
39
37
  stats = CodeStatisticsCalculator.new
40
38
 
41
39
  Dir.foreach(directory) do |file_name|
@@ -71,12 +69,12 @@ class CodeStatistics #:nodoc:
71
69
 
72
70
  def print_header
73
71
  print_splitter
74
- puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
72
+ puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
75
73
  print_splitter
76
74
  end
77
75
 
78
76
  def print_splitter
79
- puts "+----------------------+-------+-------+---------+---------+-----+-------+"
77
+ puts "+----------------------+--------+--------+---------+---------+-----+-------+"
80
78
  end
81
79
 
82
80
  def print_line(name, statistics)
@@ -84,8 +82,8 @@ class CodeStatistics #:nodoc:
84
82
  loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue loc_over_m = 0
85
83
 
86
84
  puts "| #{name.ljust(20)} " \
87
- "| #{statistics.lines.to_s.rjust(5)} " \
88
- "| #{statistics.code_lines.to_s.rjust(5)} " \
85
+ "| #{statistics.lines.to_s.rjust(6)} " \
86
+ "| #{statistics.code_lines.to_s.rjust(6)} " \
89
87
  "| #{statistics.classes.to_s.rjust(7)} " \
90
88
  "| #{statistics.methods.to_s.rjust(7)} " \
91
89
  "| #{m_over_c.to_s.rjust(3)} " \
@@ -24,6 +24,9 @@ class CodeStatisticsCalculator #:nodoc:
24
24
  }
25
25
  }
26
26
 
27
+ PATTERNS[:minitest] = PATTERNS[:rb].merge method: /^\s*(def|test)\s+['"_a-z]/
28
+ PATTERNS[:rake] = PATTERNS[:rb]
29
+
27
30
  def initialize(lines = 0, code_lines = 0, classes = 0, methods = 0)
28
31
  @lines = lines
29
32
  @code_lines = code_lines
@@ -74,6 +77,10 @@ class CodeStatisticsCalculator #:nodoc:
74
77
 
75
78
  private
76
79
  def file_type(file_path)
77
- File.extname(file_path).sub(/\A\./, '').downcase.to_sym
80
+ if file_path.end_with? '_test.rb'
81
+ :minitest
82
+ else
83
+ File.extname(file_path).sub(/\A\./, '').downcase.to_sym
84
+ end
78
85
  end
79
86
  end
@@ -0,0 +1,70 @@
1
+ require 'rails/commands/commands_tasks'
2
+
3
+ module Rails
4
+ class Command #:nodoc:
5
+ attr_reader :argv
6
+
7
+ def initialize(argv = [])
8
+ @argv = argv
9
+
10
+ @option_parser = build_option_parser
11
+ @options = {}
12
+ end
13
+
14
+ def self.run(task_name, argv)
15
+ command_name = command_name_for(task_name)
16
+
17
+ if command = command_for(command_name)
18
+ command.new(argv).run(command_name)
19
+ else
20
+ Rails::CommandsTasks.new(argv).run_command!(task_name)
21
+ end
22
+ end
23
+
24
+ def run(command_name)
25
+ parse_options_for(command_name)
26
+ @option_parser.parse! @argv
27
+
28
+ public_send(command_name)
29
+ end
30
+
31
+ def self.options_for(command_name, &options_to_parse)
32
+ @@command_options[command_name] = options_to_parse
33
+ end
34
+
35
+ def self.set_banner(command_name, banner)
36
+ options_for(command_name) { |opts, _| opts.banner = banner }
37
+ end
38
+
39
+ private
40
+ @@commands = []
41
+ @@command_options = {}
42
+
43
+ def parse_options_for(command_name)
44
+ @@command_options.fetch(command_name, proc {}).call(@option_parser, @options)
45
+ end
46
+
47
+ def build_option_parser
48
+ OptionParser.new do |opts|
49
+ opts.on('-h', '--help', 'Show this help.') do
50
+ puts opts
51
+ exit
52
+ end
53
+ end
54
+ end
55
+
56
+ def self.inherited(command)
57
+ @@commands << command
58
+ end
59
+
60
+ def self.command_name_for(task_name)
61
+ task_name.gsub(':', '_').to_sym
62
+ end
63
+
64
+ def self.command_for(command_name)
65
+ @@commands.find do |command|
66
+ command.public_instance_methods.include?(command_name)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -6,12 +6,14 @@ aliases = {
6
6
  "c" => "console",
7
7
  "s" => "server",
8
8
  "db" => "dbconsole",
9
- "r" => "runner"
9
+ "r" => "runner",
10
+ "t" => "test"
10
11
  }
11
12
 
12
13
  command = ARGV.shift
13
14
  command = aliases[command] || command
14
15
 
15
- require 'rails/commands/commands_tasks'
16
+ require 'rails/command'
17
+ require 'rails/commands/dev_cache'
16
18
 
17
- Rails::CommandsTasks.new(ARGV).run_command!(command)
19
+ Rails::Command.run(command, ARGV)
@@ -1,3 +1,5 @@
1
+ require 'rails/commands/rake_proxy'
2
+
1
3
  module Rails
2
4
  # This is a class which takes in a rails command and initiates the appropriate
3
5
  # initiation sequence.
@@ -5,6 +7,8 @@ module Rails
5
7
  # Warning: This class mutates ARGV because some commands require manipulating
6
8
  # it before they are run.
7
9
  class CommandsTasks # :nodoc:
10
+ include Rails::RakeProxy
11
+
8
12
  attr_reader :argv
9
13
 
10
14
  HELP_MESSAGE = <<-EOT
@@ -14,20 +18,25 @@ The most common rails commands are:
14
18
  generate Generate new code (short-cut alias: "g")
15
19
  console Start the Rails console (short-cut alias: "c")
16
20
  server Start the Rails server (short-cut alias: "s")
21
+ test Run tests (short-cut alias: "t")
17
22
  dbconsole Start a console for the database specified in config/database.yml
18
23
  (short-cut alias: "db")
19
24
  new Create a new Rails application. "rails new my_app" creates a
20
25
  new application called MyApp in "./my_app"
21
26
 
22
- In addition to those, there are:
23
- destroy Undo code generated with "generate" (short-cut alias: "d")
24
- plugin new Generates skeleton for developing a Rails plugin
25
- runner Run a piece of code in the application environment (short-cut alias: "r")
26
-
27
27
  All commands can be run with -h (or --help) for more information.
28
+
29
+ In addition to those commands, there are:
28
30
  EOT
29
31
 
30
- COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help)
32
+ ADDITIONAL_COMMANDS = [
33
+ [ 'destroy', 'Undo code generated with "generate" (short-cut alias: "d")' ],
34
+ [ 'plugin new', 'Generates skeleton for developing a Rails plugin' ],
35
+ [ 'runner',
36
+ 'Run a piece of code in the application environment (short-cut alias: "r")' ]
37
+ ]
38
+
39
+ COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help test)
31
40
 
32
41
  def initialize(argv)
33
42
  @argv = argv
@@ -35,10 +44,11 @@ EOT
35
44
 
36
45
  def run_command!(command)
37
46
  command = parse_command(command)
47
+
38
48
  if COMMAND_WHITELIST.include?(command)
39
49
  send(command)
40
50
  else
41
- write_error_message(command)
51
+ run_rake_task(command)
42
52
  end
43
53
  end
44
54
 
@@ -81,6 +91,10 @@ EOT
81
91
  end
82
92
  end
83
93
 
94
+ def test
95
+ require_command!("test")
96
+ end
97
+
84
98
  def dbconsole
85
99
  require_command!("dbconsole")
86
100
  Rails::DBConsole.start
@@ -105,6 +119,7 @@ EOT
105
119
 
106
120
  def help
107
121
  write_help_message
122
+ write_commands ADDITIONAL_COMMANDS + formatted_rake_tasks
108
123
  end
109
124
 
110
125
  private
@@ -146,13 +161,9 @@ EOT
146
161
  puts HELP_MESSAGE
147
162
  end
148
163
 
149
- def write_error_message(command)
150
- puts "Error: Command '#{command}' not recognized"
151
- if %x{rake #{command} --dry-run 2>&1 } && $?.success?
152
- puts "Did you mean: `$ rake #{command}` ?\n\n"
153
- end
154
- write_help_message
155
- exit(1)
164
+ def write_commands(commands)
165
+ width = commands.map { |name, _| name.size }.max || 10
166
+ commands.each { |command| printf(" %-#{width}s %s\n", *command) }
156
167
  end
157
168
 
158
169
  def parse_command(command)
@@ -1,14 +1,13 @@
1
1
  require 'optparse'
2
2
  require 'irb'
3
3
  require 'irb/completion'
4
+ require 'rails/commands/console_helper'
4
5
 
5
6
  module Rails
6
7
  class Console
7
- class << self
8
- def start(*args)
9
- new(*args).start
10
- end
8
+ include ConsoleHelper
11
9
 
10
+ class << self
12
11
  def parse_arguments(arguments)
13
12
  options = {}
14
13
 
@@ -18,34 +17,11 @@ module Rails
18
17
  opt.on("-e", "--environment=name", String,
19
18
  "Specifies the environment to run this console under (test/development/production).",
20
19
  "Default: development") { |v| options[:environment] = v.strip }
21
- opt.on("--debugger", 'Enables the debugger.') do |v|
22
- if RUBY_VERSION < '2.0.0'
23
- options[:debugger] = v
24
- else
25
- puts "=> Notice: debugger option is ignored since Ruby 2.0 and " \
26
- "it will be removed in future versions."
27
- end
28
- end
29
20
  opt.parse!(arguments)
30
21
  end
31
22
 
32
- if arguments.first && arguments.first[0] != '-'
33
- env = arguments.first
34
- if available_environments.include? env
35
- options[:environment] = env
36
- else
37
- options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
38
- end
39
- end
40
-
41
- options
23
+ set_options_env(arguments, options)
42
24
  end
43
-
44
- private
45
-
46
- def available_environments
47
- Dir['config/environments/*.rb'].map { |fname| File.basename(fname, '.*') }
48
- end
49
25
  end
50
26
 
51
27
  attr_reader :options, :app, :console
@@ -65,36 +41,15 @@ module Rails
65
41
  end
66
42
 
67
43
  def environment
68
- options[:environment] ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
69
- end
70
-
71
- def environment?
72
- environment
44
+ options[:environment] ||= super
73
45
  end
46
+ alias_method :environment?, :environment
74
47
 
75
48
  def set_environment!
76
49
  Rails.env = environment
77
50
  end
78
51
 
79
- if RUBY_VERSION < '2.0.0'
80
- def debugger?
81
- options[:debugger]
82
- end
83
-
84
- def require_debugger
85
- require 'debugger'
86
- puts "=> Debugger enabled"
87
- rescue LoadError
88
- puts "You're missing the 'debugger' gem. Add it to your Gemfile, bundle it and try again."
89
- exit(1)
90
- end
91
- end
92
-
93
52
  def start
94
- if RUBY_VERSION < '2.0.0'
95
- require_debugger if debugger?
96
- end
97
-
98
53
  set_environment! if environment?
99
54
 
100
55
  if sandbox?
@@ -105,7 +60,7 @@ module Rails
105
60
  end
106
61
 
107
62
  if defined?(console::ExtendCommandBundle)
108
- console::ExtendCommandBundle.send :include, Rails::ConsoleMethods
63
+ console::ExtendCommandBundle.include(Rails::ConsoleMethods)
109
64
  end
110
65
  console.start
111
66
  end