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
@@ -18,6 +18,11 @@ module Rails
18
18
  app = Rails.application
19
19
  session = ActionDispatch::Integration::Session.new(app)
20
20
  yield session if block_given?
21
+
22
+ # This makes app.url_for and app.foo_path available in the console
23
+ session.extend(app.routes.url_helpers)
24
+ session.extend(app.routes.mounted_helpers)
25
+
21
26
  session
22
27
  end
23
28
 
@@ -4,7 +4,7 @@ module Rails
4
4
  #
5
5
  # This method assumes an +ApplicationController+ exists, and it extends +ActionController::Base+
6
6
  def helper
7
- @helper ||= ApplicationController.helpers
7
+ ApplicationController.helpers
8
8
  end
9
9
 
10
10
  # Gets a new instance of a controller object.
@@ -2,11 +2,12 @@ require 'rails/railtie'
2
2
  require 'rails/engine/railties'
3
3
  require 'active_support/core_ext/module/delegation'
4
4
  require 'pathname'
5
+ require 'thread'
5
6
 
6
7
  module Rails
7
8
  # <tt>Rails::Engine</tt> allows you to wrap a specific Rails application or subset of
8
9
  # functionality and share it with other applications or within a larger packaged application.
9
- # Since Rails 3.0, every <tt>Rails::Application</tt> is just an engine, which allows for simple
10
+ # Every <tt>Rails::Application</tt> is just an engine, which allows for simple
10
11
  # feature and application sharing.
11
12
  #
12
13
  # Any <tt>Rails::Engine</tt> is also a <tt>Rails::Railtie</tt>, so the same
@@ -15,10 +16,9 @@ module Rails
15
16
  #
16
17
  # == Creating an Engine
17
18
  #
18
- # In Rails versions prior to 3.0, your gems automatically behaved as engines, however,
19
- # this coupled Rails to Rubygems. Since Rails 3.0, if you want a gem to automatically
20
- # behave as an engine, you have to specify an +Engine+ for it somewhere inside
21
- # your plugin's +lib+ folder (similar to how we specify a +Railtie+):
19
+ # If you want a gem to behave as an engine, you have to specify an +Engine+
20
+ # for it somewhere inside your plugin's +lib+ folder (similar to how we
21
+ # specify a +Railtie+):
22
22
  #
23
23
  # # lib/my_engine.rb
24
24
  # module MyEngine
@@ -69,10 +69,9 @@ module Rails
69
69
  #
70
70
  # == Paths
71
71
  #
72
- # Since Rails 3.0, applications and engines have more flexible path configuration (as
73
- # opposed to the previous hardcoded path configuration). This means that you are not
74
- # required to place your controllers at <tt>app/controllers</tt>, but in any place
75
- # which you find convenient.
72
+ # Applications and engines have flexible path configuration, meaning that you
73
+ # are not required to place your controllers at <tt>app/controllers</tt>, but
74
+ # in any place which you find convenient.
76
75
  #
77
76
  # For example, let's suppose you want to place your controllers in <tt>lib/controllers</tt>.
78
77
  # You can set that as an option:
@@ -206,42 +205,51 @@ module Rails
206
205
  # With such an engine, everything that is inside the +MyEngine+ module will be isolated from
207
206
  # the application.
208
207
  #
209
- # Consider such controller:
208
+ # Consider this controller:
210
209
  #
211
210
  # module MyEngine
212
211
  # class FooController < ActionController::Base
213
212
  # end
214
213
  # end
215
214
  #
216
- # If an engine is marked as isolated, +FooController+ has access only to helpers from +Engine+ and
217
- # <tt>url_helpers</tt> from <tt>MyEngine::Engine.routes</tt>.
215
+ # If the +MyEngine+ engine is marked as isolated, +FooController+ only has
216
+ # access to helpers from +MyEngine+, and <tt>url_helpers</tt> from
217
+ # <tt>MyEngine::Engine.routes</tt>.
218
218
  #
219
- # The next thing that changes in isolated engines is the behavior of routes. Normally, when you namespace
220
- # your controllers, you also need to do namespace all your routes. With an isolated engine,
221
- # the namespace is applied by default, so you can ignore it in routes:
219
+ # The next thing that changes in isolated engines is the behavior of routes.
220
+ # Normally, when you namespace your controllers, you also need to namespace
221
+ # the related routes. With an isolated engine, the engine's namespace is
222
+ # automatically applied, so you don't need to specify it explicity in your
223
+ # routes:
222
224
  #
223
225
  # MyEngine::Engine.routes.draw do
224
226
  # resources :articles
225
227
  # end
226
228
  #
227
- # The routes above will automatically point to <tt>MyEngine::ArticlesController</tt>. Furthermore, you don't
228
- # need to use longer url helpers like <tt>my_engine_articles_path</tt>. Instead, you should simply use
229
- # <tt>articles_path</tt> as you would do with your application.
229
+ # If +MyEngine+ is isolated, The routes above will point to
230
+ # <tt>MyEngine::ArticlesController</tt>. You also don't need to use longer
231
+ # url helpers like +my_engine_articles_path+. Instead, you should simply use
232
+ # +articles_path+, like you would do with your main application.
230
233
  #
231
- # To make that behavior consistent with other parts of the framework, an isolated engine also has influence on
232
- # <tt>ActiveModel::Naming</tt>. When you use a namespaced model, like <tt>MyEngine::Article</tt>, it will normally
233
- # use the prefix "my_engine". In an isolated engine, the prefix will be omitted in url helpers and
234
- # form fields for convenience.
234
+ # To make this behavior consistent with other parts of the framework,
235
+ # isolated engines also have an effect on <tt>ActiveModel::Naming</tt>. In a
236
+ # normal Rails app, when you use a namespaced model such as
237
+ # <tt>Namespace::Article</tt>, <tt>ActiveModel::Naming</tt> will generate
238
+ # names with the prefix "namespace". In an isolated engine, the prefix will
239
+ # be omitted in url helpers and form fields, for convenience.
235
240
  #
236
- # polymorphic_url(MyEngine::Article.new) # => "articles_path"
241
+ # polymorphic_url(MyEngine::Article.new)
242
+ # # => "articles_path" # not "my_engine_articles_path"
237
243
  #
238
244
  # form_for(MyEngine::Article.new) do
239
245
  # text_field :title # => <input type="text" name="article[title]" id="article_title" />
240
246
  # end
241
247
  #
242
- # Additionally, an isolated engine will set its name according to namespace, so
243
- # MyEngine::Engine.engine_name will be "my_engine". It will also set MyEngine.table_name_prefix
244
- # to "my_engine_", changing the MyEngine::Article model to use the my_engine_articles table.
248
+ # Additionally, an isolated engine will set its own name according to its
249
+ # namespace, so <tt>MyEngine::Engine.engine_name</tt> will return
250
+ # "my_engine". It will also set +MyEngine.table_name_prefix+ to "my_engine_",
251
+ # meaning for example that <tt>MyEngine::Article</tt> will use the
252
+ # +my_engine_articles+ database table by default.
245
253
  #
246
254
  # == Using Engine's routes outside Engine
247
255
  #
@@ -296,7 +304,7 @@ module Rails
296
304
  # helper MyEngine::SharedEngineHelper
297
305
  # end
298
306
  #
299
- # If you want to include all of the engine's helpers, you can use #helper method on an engine's
307
+ # If you want to include all of the engine's helpers, you can use the #helper method on an engine's
300
308
  # instance:
301
309
  #
302
310
  # class ApplicationController < ActionController::Base
@@ -312,7 +320,7 @@ module Rails
312
320
  # Engines can have their own migrations. The default path for migrations is exactly the same
313
321
  # as in application: <tt>db/migrate</tt>
314
322
  #
315
- # To use engine's migrations in application you can use rake task, which copies them to
323
+ # To use engine's migrations in application you can use the rake task below, which copies them to
316
324
  # application's dir:
317
325
  #
318
326
  # rake ENGINE_NAME:install:migrations
@@ -328,7 +336,7 @@ module Rails
328
336
  #
329
337
  # == Loading priority
330
338
  #
331
- # In order to change engine's priority you can use +config.railties_order+ in main application.
339
+ # In order to change engine's priority you can use +config.railties_order+ in the main application.
332
340
  # It will affect the priority of loading views, helpers, assets and all the other files
333
341
  # related to engine or application.
334
342
  #
@@ -350,12 +358,7 @@ module Rails
350
358
  Rails::Railtie::Configuration.eager_load_namespaces << base
351
359
 
352
360
  base.called_from = begin
353
- call_stack = if Kernel.respond_to?(:caller_locations)
354
- caller_locations.map { |l| l.absolute_path || l.path }
355
- else
356
- # Remove the line number from backtraces making sure we don't leave anything behind
357
- caller.map { |p| p.sub(/:\d+.*/, '') }
358
- end
361
+ call_stack = caller_locations.map { |l| l.absolute_path || l.path }
359
362
 
360
363
  File.dirname(call_stack.detect { |p| p !~ %r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack] })
361
364
  end
@@ -405,7 +408,7 @@ module Rails
405
408
  end
406
409
  end
407
410
 
408
- # Finds engine with given path
411
+ # Finds engine with given path.
409
412
  def find(path)
410
413
  expanded_path = File.expand_path path
411
414
  Rails::Engine.subclasses.each do |klass|
@@ -427,6 +430,7 @@ module Rails
427
430
  @env_config = nil
428
431
  @helpers = nil
429
432
  @routes = nil
433
+ @app_build_lock = Mutex.new
430
434
  super
431
435
  end
432
436
 
@@ -484,7 +488,7 @@ module Rails
484
488
  helpers = Module.new
485
489
  all = ActionController::Base.all_helpers_from_path(helpers_paths)
486
490
  ActionController::Base.modules_for_helpers(all).each do |mod|
487
- helpers.send(:include, mod)
491
+ helpers.include(mod)
488
492
  end
489
493
  helpers
490
494
  end
@@ -497,10 +501,13 @@ module Rails
497
501
 
498
502
  # Returns the underlying rack application for this engine.
499
503
  def app
500
- @app ||= begin
501
- config.middleware = config.middleware.merge_into(default_middleware_stack)
502
- config.middleware.build(endpoint)
503
- end
504
+ @app || @app_build_lock.synchronize {
505
+ @app ||= begin
506
+ stack = default_middleware_stack
507
+ config.middleware = build_middleware.merge_into(stack)
508
+ config.middleware.build(endpoint)
509
+ end
510
+ }
504
511
  end
505
512
 
506
513
  # Returns the endpoint for this engine. If none is registered,
@@ -511,24 +518,19 @@ module Rails
511
518
 
512
519
  # Define the Rack API for this engine.
513
520
  def call(env)
514
- env.merge!(env_config)
515
- if env['SCRIPT_NAME']
516
- env["ROUTES_#{routes.object_id}_SCRIPT_NAME"] = env['SCRIPT_NAME'].dup
517
- end
518
- app.call(env)
521
+ req = build_request env
522
+ app.call req.env
519
523
  end
520
524
 
521
525
  # Defines additional Rack env configuration that is added on each call.
522
526
  def env_config
523
- @env_config ||= {
524
- 'action_dispatch.routes' => routes
525
- }
527
+ @env_config ||= {}
526
528
  end
527
529
 
528
530
  # Defines the routes for this engine. If a block is given to
529
531
  # routes, it is appended to the engine.
530
532
  def routes
531
- @routes ||= ActionDispatch::Routing::RouteSet.new
533
+ @routes ||= ActionDispatch::Routing::RouteSet.new_with_config(config)
532
534
  @routes.append(&Proc.new) if block_given?
533
535
  @routes
534
536
  end
@@ -559,7 +561,7 @@ module Rails
559
561
  # and the load_once paths.
560
562
  #
561
563
  # This needs to be an initializer, since it needs to run once
562
- # per engine and get the engine as a block parameter
564
+ # per engine and get the engine as a block parameter.
563
565
  initializer :set_autoload_paths, before: :bootstrap_hook do
564
566
  ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths)
565
567
  ActiveSupport::Dependencies.autoload_once_paths.unshift(*_all_autoload_once_paths)
@@ -582,7 +584,7 @@ module Rails
582
584
  # I18n load paths are a special case since the ones added
583
585
  # later have higher priority.
584
586
  initializer :add_locales do
585
- config.i18n.railties_load_path.concat(paths["config/locales"].existent)
587
+ config.i18n.railties_load_path << paths["config/locales"]
586
588
  end
587
589
 
588
590
  initializer :add_view_paths do
@@ -599,12 +601,6 @@ module Rails
599
601
  end
600
602
  end
601
603
 
602
- initializer :append_assets_path, group: :all do |app|
603
- app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories)
604
- app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories)
605
- app.config.assets.paths.unshift(*paths["app/assets"].existent_directories)
606
- end
607
-
608
604
  initializer :prepend_helpers_path do |app|
609
605
  if !isolated? || (app == self)
610
606
  app.config.helpers_paths.unshift(*paths["app/helpers"].existent)
@@ -690,5 +686,19 @@ module Rails
690
686
  def _all_load_paths #:nodoc:
691
687
  @_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq
692
688
  end
689
+
690
+ private
691
+
692
+ def build_request(env)
693
+ env.merge!(env_config)
694
+ req = ActionDispatch::Request.new env
695
+ req.routes = routes
696
+ req.engine_script_name = req.script_name
697
+ req
698
+ end
699
+
700
+ def build_middleware
701
+ config.middleware
702
+ end
693
703
  end
694
704
  end
@@ -2,7 +2,8 @@ ARGV << '--help' if ARGV.empty?
2
2
 
3
3
  aliases = {
4
4
  "g" => "generate",
5
- "d" => "destroy"
5
+ "d" => "destroy",
6
+ "t" => "test"
6
7
  }
7
8
 
8
9
  command = ARGV.shift
@@ -12,7 +13,7 @@ require ENGINE_PATH
12
13
  engine = ::Rails::Engine.find(ENGINE_ROOT)
13
14
 
14
15
  case command
15
- when 'generate', 'destroy'
16
+ when 'generate', 'destroy', 'test'
16
17
  require 'rails/generators'
17
18
  Rails::Generators.namespace = engine.railtie_namespace
18
19
  engine.load_generators
@@ -30,6 +31,7 @@ Usage: rails COMMAND [ARGS]
30
31
  The common Rails commands available for engines are:
31
32
  generate Generate new code (short-cut alias: "g")
32
33
  destroy Undo code generated with "generate" (short-cut alias: "d")
34
+ test Run tests (short-cut alias: "t")
33
35
 
34
36
  All commands can be run with -h for more information.
35
37
 
@@ -4,17 +4,14 @@ module Rails
4
4
  class Engine
5
5
  class Configuration < ::Rails::Railtie::Configuration
6
6
  attr_reader :root
7
- attr_writer :middleware, :eager_load_paths, :autoload_once_paths, :autoload_paths
7
+ attr_accessor :middleware
8
+ attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths
8
9
 
9
10
  def initialize(root=nil)
10
11
  super()
11
12
  @root = root
12
13
  @generators = app_generators.dup
13
- end
14
-
15
- # Returns the middleware stack for the engine.
16
- def middleware
17
- @middleware ||= Rails::Configuration::MiddlewareStackProxy.new
14
+ @middleware = Rails::Configuration::MiddlewareStackProxy.new
18
15
  end
19
16
 
20
17
  # Holds generators configuration:
@@ -39,7 +36,7 @@ module Rails
39
36
  @paths ||= begin
40
37
  paths = Rails::Paths::Root.new(@root)
41
38
 
42
- paths.add "app", eager_load: true, glob: "*"
39
+ paths.add "app", eager_load: true, glob: "{*,*/concerns}"
43
40
  paths.add "app/assets", glob: "*"
44
41
  paths.add "app/controllers", eager_load: true
45
42
  paths.add "app/helpers", eager_load: true
@@ -47,9 +44,6 @@ module Rails
47
44
  paths.add "app/mailers", eager_load: true
48
45
  paths.add "app/views"
49
46
 
50
- paths.add "app/controllers/concerns", eager_load: true
51
- paths.add "app/models/concerns", eager_load: true
52
-
53
47
  paths.add "lib", load_path: true
54
48
  paths.add "lib/assets", glob: "*"
55
49
  paths.add "lib/tasks", glob: "**/*.rake"
@@ -5,10 +5,10 @@ module Rails
5
5
  end
6
6
 
7
7
  module VERSION
8
- MAJOR = 4
9
- MINOR = 2
10
- TINY = 11
11
- PRE = "3"
8
+ MAJOR = 5
9
+ MINOR = 0
10
+ TINY = 0
11
+ PRE = "beta1"
12
12
 
13
13
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
14
14
  end
@@ -33,6 +33,7 @@ module Rails
33
33
  scaffold_controller: '-c',
34
34
  stylesheets: '-y',
35
35
  stylesheet_engine: '-se',
36
+ scaffold_stylesheet: '-ss',
36
37
  template_engine: '-e',
37
38
  test_framework: '-t'
38
39
  },
@@ -44,6 +45,7 @@ module Rails
44
45
 
45
46
  DEFAULT_OPTIONS = {
46
47
  rails: {
48
+ api: false,
47
49
  assets: true,
48
50
  force_plural: false,
49
51
  helper: true,
@@ -56,12 +58,14 @@ module Rails
56
58
  scaffold_controller: :scaffold_controller,
57
59
  stylesheets: true,
58
60
  stylesheet_engine: :css,
61
+ scaffold_stylesheet: true,
59
62
  test_framework: false,
60
63
  template_engine: :erb
61
64
  }
62
65
  }
63
66
 
64
67
  def self.configure!(config) #:nodoc:
68
+ api_only! if config.api_only
65
69
  no_color! unless config.colorize_logging
66
70
  aliases.deep_merge! config.aliases
67
71
  options.deep_merge! config.options
@@ -99,6 +103,21 @@ module Rails
99
103
  @fallbacks ||= {}
100
104
  end
101
105
 
106
+ # Configure generators for API only applications. It basically hides
107
+ # everything that is usually browser related, such as assets and session
108
+ # migration generators, and completely disable views, helpers and assets
109
+ # so generators such as scaffold won't create them.
110
+ def self.api_only!
111
+ hide_namespaces "assets", "helper", "css", "js"
112
+
113
+ options[:rails].merge!(
114
+ api: true,
115
+ assets: false,
116
+ helper: false,
117
+ template_engine: nil
118
+ )
119
+ end
120
+
102
121
  # Remove the color from output.
103
122
  def self.no_color!
104
123
  Thor::Base.shell = Thor::Shell::Basic
@@ -153,13 +172,13 @@ module Rails
153
172
  def self.invoke(namespace, args=ARGV, config={})
154
173
  names = namespace.to_s.split(':')
155
174
  if klass = find_by_namespace(names.pop, names.any? && names.join(':'))
156
- args << "--help" if args.empty? && klass.arguments.any? { |a| a.required? }
175
+ args << "--help" if args.empty? && klass.arguments.any?(&:required?)
157
176
  klass.start(args, config)
158
177
  else
159
- options = sorted_groups.map(&:last).flatten
178
+ options = sorted_groups.flat_map(&:last)
160
179
  suggestions = options.sort_by {|suggested| levenshtein_distance(namespace.to_s, suggested) }.first(3)
161
180
  msg = "Could not find generator '#{namespace}'. "
162
- msg << "Maybe you meant #{ suggestions.map {|s| "'#{s}'"}.to_sentence(last_word_connector: " or ") }\n"
181
+ msg << "Maybe you meant #{ suggestions.map {|s| "'#{s}'"}.to_sentence(last_word_connector: " or ", locale: :en) }\n"
163
182
  msg << "Run `rails generate --help` for more options."
164
183
  puts msg
165
184
  end
@@ -189,6 +208,7 @@ module Rails
189
208
  "#{test}:model",
190
209
  "#{test}:scaffold",
191
210
  "#{test}:view",
211
+ "#{test}:job",
192
212
  "#{template}:controller",
193
213
  "#{template}:scaffold",
194
214
  "#{template}:mailer",
@@ -226,7 +246,7 @@ module Rails
226
246
 
227
247
  def self.public_namespaces
228
248
  lookup!
229
- subclasses.map { |k| k.namespace }
249
+ subclasses.map(&:namespace)
230
250
  end
231
251
 
232
252
  def self.print_generators
@@ -267,10 +287,13 @@ module Rails
267
287
  d = (0..m).to_a
268
288
  x = nil
269
289
 
270
- str1.each_char.each_with_index do |char1,i|
290
+ # avoid duplicating an enumerable object in the loop
291
+ str2_codepoint_enumerable = str2.each_codepoint
292
+
293
+ str1.each_codepoint.with_index do |char1, i|
271
294
  e = i+1
272
295
 
273
- str2.each_char.each_with_index do |char2,j|
296
+ str2_codepoint_enumerable.with_index do |char2, j|
274
297
  cost = (char1 == char2) ? 0 : 1
275
298
  x = [
276
299
  d[j+1] + 1, # insertion
@@ -284,7 +307,7 @@ module Rails
284
307
  d[m] = x
285
308
  end
286
309
 
287
- return x
310
+ x
288
311
  end
289
312
 
290
313
  # Prints a list of generators.