railties 3.1.12 → 3.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. data/CHANGELOG.md +2292 -41
  2. data/README.rdoc +14 -5
  3. data/bin/rails +7 -0
  4. data/guides/code/getting_started/Gemfile +27 -0
  5. data/guides/code/getting_started/README.rdoc +261 -0
  6. data/guides/code/getting_started/Rakefile +7 -0
  7. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  8. data/guides/code/getting_started/app/assets/javascripts/application.js +9 -0
  9. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  10. data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +3 -0
  11. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  12. data/guides/code/getting_started/app/assets/stylesheets/application.css +7 -0
  13. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  14. data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +3 -0
  15. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  16. data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +56 -0
  17. data/guides/code/getting_started/app/controllers/application_controller.rb +3 -0
  18. data/guides/code/getting_started/app/controllers/comments_controller.rb +16 -0
  19. data/guides/code/getting_started/app/controllers/home_controller.rb +5 -0
  20. data/guides/code/getting_started/app/controllers/posts_controller.rb +84 -0
  21. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  22. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  23. data/guides/code/getting_started/app/helpers/home_helper.rb +2 -0
  24. data/guides/code/getting_started/app/helpers/posts_helper.rb +5 -0
  25. data/guides/code/getting_started/app/models/comment.rb +3 -0
  26. data/guides/code/getting_started/app/models/post.rb +11 -0
  27. data/guides/code/getting_started/app/models/tag.rb +3 -0
  28. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  29. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  30. data/guides/code/getting_started/app/views/home/index.html.erb +2 -0
  31. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  32. data/guides/code/getting_started/app/views/posts/_form.html.erb +32 -0
  33. data/guides/code/getting_started/app/views/posts/edit.html.erb +6 -0
  34. data/guides/code/getting_started/app/views/posts/index.html.erb +27 -0
  35. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  36. data/guides/code/getting_started/app/views/posts/show.html.erb +31 -0
  37. data/guides/code/getting_started/app/views/tags/_form.html.erb +12 -0
  38. data/guides/code/getting_started/config.ru +4 -0
  39. data/guides/code/getting_started/config/application.rb +53 -0
  40. data/guides/code/getting_started/config/boot.rb +6 -0
  41. data/guides/code/getting_started/config/database.yml +25 -0
  42. data/guides/code/getting_started/config/environment.rb +5 -0
  43. data/guides/code/getting_started/config/environments/development.rb +37 -0
  44. data/guides/code/getting_started/config/environments/production.rb +67 -0
  45. data/guides/code/getting_started/config/environments/test.rb +37 -0
  46. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  47. data/guides/code/getting_started/config/initializers/inflections.rb +10 -0
  48. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  49. data/guides/code/getting_started/config/initializers/secret_token.rb +7 -0
  50. data/guides/code/getting_started/config/initializers/session_store.rb +8 -0
  51. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  52. data/guides/code/getting_started/config/locales/en.yml +5 -0
  53. data/guides/code/getting_started/config/routes.rb +64 -0
  54. data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +11 -0
  55. data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +12 -0
  56. data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +11 -0
  57. data/guides/code/getting_started/db/schema.rb +43 -0
  58. data/guides/code/getting_started/db/seeds.rb +7 -0
  59. data/guides/code/getting_started/doc/README_FOR_APP +2 -0
  60. data/guides/code/getting_started/public/404.html +26 -0
  61. data/guides/code/getting_started/public/422.html +26 -0
  62. data/guides/code/getting_started/public/500.html +26 -0
  63. data/guides/code/getting_started/public/favicon.ico +0 -0
  64. data/guides/code/getting_started/public/robots.txt +5 -0
  65. data/guides/code/getting_started/script/rails +6 -0
  66. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  67. data/guides/code/getting_started/test/fixtures/posts.yml +11 -0
  68. data/guides/code/getting_started/test/fixtures/tags.yml +9 -0
  69. data/guides/code/getting_started/test/functional/comments_controller_test.rb +7 -0
  70. data/guides/code/getting_started/test/functional/home_controller_test.rb +9 -0
  71. data/guides/code/getting_started/test/functional/posts_controller_test.rb +49 -0
  72. data/guides/code/getting_started/test/performance/browsing_test.rb +12 -0
  73. data/guides/code/getting_started/test/test_helper.rb +13 -0
  74. data/guides/code/getting_started/test/unit/comment_test.rb +7 -0
  75. data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +4 -0
  76. data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +4 -0
  77. data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +4 -0
  78. data/guides/code/getting_started/test/unit/post_test.rb +7 -0
  79. data/guides/code/getting_started/test/unit/tag_test.rb +7 -0
  80. data/guides/rails_guides/generator.rb +2 -1
  81. data/guides/source/3_0_release_notes.textile +2 -2
  82. data/guides/source/3_1_release_notes.textile +3 -110
  83. data/guides/source/action_controller_overview.textile +11 -13
  84. data/guides/source/action_mailer_basics.textile +7 -18
  85. data/guides/source/action_view_overview.textile +78 -9
  86. data/guides/source/active_model_basics.textile +205 -0
  87. data/guides/source/active_record_basics.textile +31 -31
  88. data/guides/source/active_record_querying.textile +288 -67
  89. data/guides/source/active_record_validations_callbacks.textile +69 -75
  90. data/guides/source/active_resource_basics.textile +48 -2
  91. data/guides/source/active_support_core_extensions.textile +145 -24
  92. data/guides/source/ajax_on_rails.textile +65 -7
  93. data/guides/source/api_documentation_guidelines.textile +0 -6
  94. data/guides/source/asset_pipeline.textile +2 -2
  95. data/guides/source/association_basics.textile +25 -34
  96. data/guides/source/caching_with_rails.textile +12 -17
  97. data/guides/source/command_line.textile +29 -19
  98. data/guides/source/configuring.textile +40 -18
  99. data/guides/source/contributing_to_ruby_on_rails.textile +11 -18
  100. data/guides/source/debugging_rails_applications.textile +10 -21
  101. data/guides/source/engines.textile +618 -0
  102. data/guides/source/form_helpers.textile +1 -12
  103. data/guides/source/generators.textile +9 -11
  104. data/guides/source/getting_started.textile +152 -152
  105. data/guides/source/i18n.textile +4 -5
  106. data/guides/source/index.html.erb +0 -1
  107. data/guides/source/initialization.textile +26 -26
  108. data/guides/source/layouts_and_rendering.textile +97 -61
  109. data/guides/source/migrations.textile +380 -161
  110. data/guides/source/performance_testing.textile +4 -10
  111. data/guides/source/plugins.textile +11 -19
  112. data/guides/source/rails_application_templates.textile +12 -4
  113. data/guides/source/rails_on_rack.textile +25 -19
  114. data/guides/source/routing.textile +6 -13
  115. data/guides/source/ruby_on_rails_guides_guidelines.textile +0 -5
  116. data/guides/source/security.textile +11 -15
  117. data/guides/source/testing.textile +1 -9
  118. data/lib/rails/application.rb +107 -42
  119. data/lib/rails/application/bootstrap.rb +12 -11
  120. data/lib/rails/application/configuration.rb +27 -21
  121. data/lib/rails/application/finisher.rb +40 -17
  122. data/lib/rails/application/route_inspector.rb +75 -0
  123. data/lib/rails/application/routes_reloader.rb +15 -4
  124. data/lib/rails/code_statistics.rb +16 -5
  125. data/lib/rails/commands.rb +6 -5
  126. data/lib/rails/commands/application.rb +8 -1
  127. data/lib/rails/commands/console.rb +2 -0
  128. data/lib/rails/commands/dbconsole.rb +2 -2
  129. data/lib/rails/commands/destroy.rb +0 -2
  130. data/lib/rails/commands/generate.rb +3 -3
  131. data/lib/rails/commands/plugin.rb +161 -159
  132. data/lib/rails/commands/plugin_new.rb +3 -2
  133. data/lib/rails/commands/runner.rb +4 -0
  134. data/lib/rails/console/app.rb +26 -22
  135. data/lib/rails/console/helpers.rb +9 -5
  136. data/lib/rails/engine.rb +70 -34
  137. data/lib/rails/engine/commands.rb +39 -0
  138. data/lib/rails/engine/configuration.rb +1 -1
  139. data/lib/rails/generators.rb +3 -14
  140. data/lib/rails/generators/actions.rb +36 -9
  141. data/lib/rails/generators/app_base.rb +34 -38
  142. data/lib/rails/generators/base.rb +4 -4
  143. data/lib/rails/generators/generated_attribute.rb +1 -1
  144. data/lib/rails/generators/named_base.rb +1 -3
  145. data/lib/rails/generators/rails/app/USAGE +6 -0
  146. data/lib/rails/generators/rails/app/app_generator.rb +6 -2
  147. data/lib/rails/generators/rails/app/templates/Gemfile +4 -3
  148. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +9 -3
  149. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +11 -5
  150. data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
  151. data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
  152. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +1 -1
  153. data/lib/rails/generators/rails/app/templates/config/application.rb +11 -0
  154. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +1 -1
  155. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +1 -1
  156. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +10 -1
  157. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +10 -1
  158. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +6 -6
  159. data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +5 -0
  160. data/lib/rails/generators/rails/app/templates/config/routes.rb +1 -1
  161. data/lib/rails/generators/rails/app/templates/public/500.html +0 -1
  162. data/lib/rails/generators/rails/app/templates/public/index.html +1 -1
  163. data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  164. data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  165. data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  166. data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  167. data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  168. data/lib/rails/generators/rails/controller/templates/controller.rb +1 -1
  169. data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
  170. data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +17 -5
  171. data/lib/rails/generators/rails/plugin_new/templates/Rakefile +1 -0
  172. data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
  173. data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
  174. data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +1 -1
  175. data/lib/rails/generators/rails/plugin_new/templates/gitignore +4 -3
  176. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb +1 -1
  177. data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +1 -1
  178. data/lib/rails/generators/rails/plugin_new/templates/script/rails.tt +5 -3
  179. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +2 -2
  180. data/lib/rails/generators/rails/task/USAGE +9 -0
  181. data/lib/rails/generators/rails/task/task_generator.rb +12 -0
  182. data/lib/rails/generators/rails/task/templates/task.rb +8 -0
  183. data/lib/rails/generators/resource_helpers.rb +3 -3
  184. data/lib/rails/generators/test_unit/integration/templates/integration_test.rb +0 -2
  185. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +4 -4
  186. data/lib/rails/paths.rb +11 -38
  187. data/lib/rails/rack/debugger.rb +3 -4
  188. data/lib/rails/rack/logger.rb +26 -12
  189. data/lib/rails/railtie.rb +6 -1
  190. data/lib/rails/railtie/configuration.rb +12 -5
  191. data/lib/rails/source_annotation_extractor.rb +12 -10
  192. data/lib/rails/tasks/documentation.rake +3 -1
  193. data/lib/rails/tasks/engine.rake +1 -0
  194. data/lib/rails/tasks/misc.rake +1 -1
  195. data/lib/rails/tasks/routes.rake +3 -23
  196. data/lib/rails/test_help.rb +1 -2
  197. data/lib/rails/test_unit/testing.rake +8 -4
  198. data/lib/rails/version.rb +3 -3
  199. metadata +131 -61
  200. checksums.yaml +0 -7
  201. data/lib/rails/generators/rails/plugin/USAGE +0 -13
  202. data/lib/rails/generators/rails/plugin/plugin_generator.rb +0 -54
  203. data/lib/rails/generators/rails/plugin/templates/MIT-LICENSE.tt +0 -20
  204. data/lib/rails/generators/rails/plugin/templates/README.tt +0 -13
  205. data/lib/rails/generators/rails/plugin/templates/Rakefile.tt +0 -23
  206. data/lib/rails/generators/rails/plugin/templates/init.rb +0 -1
  207. data/lib/rails/generators/rails/plugin/templates/install.rb +0 -1
  208. data/lib/rails/generators/rails/plugin/templates/lib/%file_name%.rb.tt +0 -1
  209. data/lib/rails/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +0 -4
  210. data/lib/rails/generators/rails/plugin/templates/uninstall.rb +0 -1
@@ -24,12 +24,21 @@ module Rails
24
24
  initializer :initialize_logger, :group => :all do
25
25
  Rails.logger ||= config.logger || begin
26
26
  path = config.paths["log"].first
27
- logger = ActiveSupport::BufferedLogger.new(path)
27
+ unless File.exist? File.dirname path
28
+ FileUtils.mkdir_p File.dirname path
29
+ end
30
+
31
+ f = File.open path, 'w'
32
+ f.binmode
33
+ f.sync = !Rails.env.production? # make sure every write flushes
34
+
35
+ logger = ActiveSupport::TaggedLogging.new(
36
+ ActiveSupport::BufferedLogger.new(f)
37
+ )
28
38
  logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
29
- logger.auto_flushing = false if Rails.env.production?
30
39
  logger
31
40
  rescue StandardError
32
- logger = ActiveSupport::BufferedLogger.new(STDERR)
41
+ logger = ActiveSupport::TaggedLogging.new(ActiveSupport::BufferedLogger.new(STDERR))
33
42
  logger.level = ActiveSupport::BufferedLogger::WARN
34
43
  logger.warn(
35
44
  "Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " +
@@ -37,7 +46,6 @@ module Rails
37
46
  )
38
47
  logger
39
48
  end
40
- at_exit { Rails.logger.flush if Rails.logger.respond_to?(:flush) }
41
49
  end
42
50
 
43
51
  # Initialize cache early in the stack so railties can make use of it.
@@ -51,13 +59,6 @@ module Rails
51
59
  end
52
60
  end
53
61
 
54
- initializer :set_clear_dependencies_hook, :group => :all do
55
- ActionDispatch::Reloader.to_cleanup do
56
- ActiveSupport::DescendantsTracker.clear
57
- ActiveSupport::Dependencies.clear
58
- end
59
- end
60
-
61
62
  # Sets the dependency loading mechanism.
62
63
  # TODO: Remove files from the $" and always use require.
63
64
  initializer :initialize_dependency_mechanism, :group => :all do
@@ -1,5 +1,6 @@
1
1
  require 'active_support/core_ext/string/encoding'
2
2
  require 'active_support/core_ext/kernel/reporting'
3
+ require 'active_support/file_update_checker'
3
4
  require 'rails/engine/configuration'
4
5
 
5
6
  module Rails
@@ -7,11 +8,11 @@ module Rails
7
8
  class Configuration < ::Rails::Engine::Configuration
8
9
  attr_accessor :allow_concurrency, :asset_host, :asset_path, :assets,
9
10
  :cache_classes, :cache_store, :consider_all_requests_local,
10
- :dependency_loading, :filter_parameters,
11
- :force_ssl, :helpers_paths, :logger, :preload_frameworks,
12
- :reload_plugins, :secret_token, :serve_static_assets,
13
- :ssl_options, :static_cache_control, :session_options,
14
- :time_zone, :whiny_nils
11
+ :dependency_loading, :exceptions_app, :file_watcher, :filter_parameters,
12
+ :force_ssl, :helpers_paths, :logger, :log_tags, :preload_frameworks,
13
+ :railties_order, :relative_url_root, :reload_plugins, :secret_token,
14
+ :serve_static_assets, :ssl_options, :static_cache_control, :session_options,
15
+ :time_zone, :reload_classes_only_on_change, :whiny_nils
15
16
 
16
17
  attr_writer :log_level
17
18
  attr_reader :encoding
@@ -19,22 +20,27 @@ module Rails
19
20
  def initialize(*)
20
21
  super
21
22
  self.encoding = "utf-8"
22
- @allow_concurrency = false
23
- @consider_all_requests_local = false
24
- @filter_parameters = []
25
- @helpers_paths = []
26
- @dependency_loading = true
27
- @serve_static_assets = true
28
- @static_cache_control = nil
29
- @force_ssl = false
30
- @ssl_options = {}
31
- @session_store = :cookie_store
32
- @session_options = {}
33
- @time_zone = "UTC"
34
- @log_level = nil
35
- @middleware = app_middleware
36
- @generators = app_generators
37
- @cache_store = [ :file_store, "#{root}/tmp/cache/" ]
23
+ @allow_concurrency = false
24
+ @consider_all_requests_local = false
25
+ @filter_parameters = []
26
+ @helpers_paths = []
27
+ @dependency_loading = true
28
+ @serve_static_assets = true
29
+ @static_cache_control = nil
30
+ @force_ssl = false
31
+ @ssl_options = {}
32
+ @session_store = :cookie_store
33
+ @session_options = {}
34
+ @time_zone = "UTC"
35
+ @log_level = nil
36
+ @middleware = app_middleware
37
+ @generators = app_generators
38
+ @cache_store = [ :file_store, "#{root}/tmp/cache/" ]
39
+ @railties_order = [:all]
40
+ @relative_url_root = ENV["RAILS_RELATIVE_URL_ROOT"]
41
+ @reload_classes_only_on_change = true
42
+ @file_watcher = ActiveSupport::FileUpdateChecker
43
+ @exceptions_app = nil
38
44
 
39
45
  @assets = ActiveSupport::OrderedOptions.new
40
46
  @assets.enabled = false
@@ -2,6 +2,7 @@ module Rails
2
2
  class Application
3
3
  module Finisher
4
4
  include Initializable
5
+ $rails_rake_task = nil
5
6
 
6
7
  initializer :add_generator_templates do
7
8
  config.generators.templates.unshift(*paths["lib/templates"].existent)
@@ -19,12 +20,6 @@ module Rails
19
20
  end
20
21
  end
21
22
 
22
- initializer :add_to_prepare_blocks do
23
- config.to_prepare_blocks.each do |block|
24
- ActionDispatch::Reloader.to_prepare(&block)
25
- end
26
- end
27
-
28
23
  initializer :add_builtin_route do |app|
29
24
  if Rails.env.development?
30
25
  app.routes.append do
@@ -37,14 +32,22 @@ module Rails
37
32
  build_middleware_stack
38
33
  end
39
34
 
40
- initializer :run_prepare_callbacks do
41
- ActionDispatch::Reloader.prepare!
42
- end
43
-
44
35
  initializer :define_main_app_helper do |app|
45
36
  app.routes.define_mounted_helper(:main_app)
46
37
  end
47
38
 
39
+ initializer :add_to_prepare_blocks do
40
+ config.to_prepare_blocks.each do |block|
41
+ ActionDispatch::Reloader.to_prepare(&block)
42
+ end
43
+ end
44
+
45
+ # This needs to happen before eager load so it happens
46
+ # in exactly the same point regardless of config.cache_classes
47
+ initializer :run_prepare_callbacks do
48
+ ActionDispatch::Reloader.prepare!
49
+ end
50
+
48
51
  initializer :eager_load! do
49
52
  if config.cache_classes && !$rails_rake_task
50
53
  ActiveSupport.run_load_hooks(:before_eager_load, self)
@@ -52,17 +55,37 @@ module Rails
52
55
  end
53
56
  end
54
57
 
58
+ # All initialization is done, including eager loading in production
55
59
  initializer :finisher_hook do
56
60
  ActiveSupport.run_load_hooks(:after_initialize, self)
57
61
  end
58
62
 
59
- # Force routes to be loaded just at the end and add it to to_prepare callbacks
60
- # This needs to be after the finisher hook to ensure routes added in the hook
61
- # are still loaded.
62
- initializer :set_routes_reloader do |app|
63
- reloader = lambda { app.routes_reloader.execute_if_updated }
64
- reloader.call
65
- ActionDispatch::Reloader.to_prepare(&reloader)
63
+ # Set app reload just after the finisher hook to ensure
64
+ # routes added in the hook are still loaded.
65
+ initializer :set_routes_reloader_hook do
66
+ reloader = routes_reloader
67
+ reloader.execute_if_updated
68
+ self.reloaders << reloader
69
+ ActionDispatch::Reloader.to_prepare { reloader.execute_if_updated }
70
+ end
71
+
72
+ # Set app reload just after the finisher hook to ensure
73
+ # paths added in the hook are still loaded.
74
+ initializer :set_clear_dependencies_hook, :group => :all do
75
+ callback = lambda do
76
+ ActiveSupport::DescendantsTracker.clear
77
+ ActiveSupport::Dependencies.clear
78
+ end
79
+
80
+ if config.reload_classes_only_on_change
81
+ reloader = config.file_watcher.new(*watchable_args, &callback)
82
+ self.reloaders << reloader
83
+ # We need to set a to_prepare callback regardless of the reloader result, i.e.
84
+ # models should be reloaded if any of the reloaders (i18n, routes) were updated.
85
+ ActionDispatch::Reloader.to_prepare(:prepend => true){ reloader.execute }
86
+ else
87
+ ActionDispatch::Reloader.to_cleanup(&callback)
88
+ end
66
89
  end
67
90
 
68
91
  # Disable dependency loading during request cycle
@@ -0,0 +1,75 @@
1
+ module Rails
2
+ class Application
3
+ ##
4
+ # This class is just used for displaying route information when someone
5
+ # executes `rake routes`. People should not use this class.
6
+ class RouteInspector # :nodoc:
7
+ def initialize
8
+ @engines = ActiveSupport::OrderedHash.new
9
+ end
10
+
11
+ def format all_routes, filter = nil
12
+ if filter
13
+ all_routes = all_routes.select{ |route| route.defaults[:controller] == filter }
14
+ end
15
+
16
+ routes = collect_routes(all_routes)
17
+
18
+ formatted_routes(routes) +
19
+ formatted_routes_for_engines
20
+ end
21
+
22
+ def collect_routes(routes)
23
+ routes = routes.collect do |route|
24
+ route_reqs = route.requirements
25
+
26
+ rack_app = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
27
+
28
+ controller = route_reqs[:controller] || ':controller'
29
+ action = route_reqs[:action] || ':action'
30
+
31
+ endpoint = rack_app ? rack_app.inspect : "#{controller}##{action}"
32
+ constraints = route_reqs.except(:controller, :action)
33
+
34
+ reqs = endpoint
35
+ reqs += " #{constraints.inspect}" unless constraints.empty?
36
+
37
+ verb = route.verb.source.gsub(/[$^]/, '')
38
+
39
+ collect_engine_routes(reqs, rack_app)
40
+
41
+ {:name => route.name.to_s, :verb => verb, :path => route.path.spec.to_s, :reqs => reqs }
42
+ end
43
+
44
+ # Skip the route if it's internal info route
45
+ routes.reject { |r| r[:path] =~ %r{/rails/info/properties|^/assets} }
46
+ end
47
+
48
+ def collect_engine_routes(name, rack_app)
49
+ return unless rack_app && rack_app.respond_to?(:routes)
50
+ return if @engines[name]
51
+
52
+ routes = rack_app.routes
53
+ if routes.is_a?(ActionDispatch::Routing::RouteSet)
54
+ @engines[name] = collect_routes(routes.routes)
55
+ end
56
+ end
57
+
58
+ def formatted_routes_for_engines
59
+ @engines.map do |name, routes|
60
+ ["\nRoutes for #{name}:"] + formatted_routes(routes)
61
+ end.flatten
62
+ end
63
+
64
+ def formatted_routes(routes)
65
+ name_width = routes.map{ |r| r[:name].length }.max
66
+ verb_width = routes.map{ |r| r[:verb].length }.max
67
+ path_width = routes.map{ |r| r[:path].length }.max
68
+
69
+ routes.map do |r|
70
+ "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,10 +1,13 @@
1
+ require "active_support/core_ext/module/delegation"
2
+
1
3
  module Rails
2
4
  class Application
3
- class RoutesReloader < ::ActiveSupport::FileUpdateChecker
4
- attr_reader :route_sets
5
+ class RoutesReloader
6
+ attr_reader :route_sets, :paths
7
+ delegate :execute_if_updated, :execute, :updated?, :to => :updater
5
8
 
6
9
  def initialize
7
- super([]) { reload! }
10
+ @paths = []
8
11
  @route_sets = []
9
12
  end
10
13
 
@@ -16,7 +19,15 @@ module Rails
16
19
  revert
17
20
  end
18
21
 
19
- protected
22
+ private
23
+
24
+ def updater
25
+ @updater ||= begin
26
+ updater = ActiveSupport::FileUpdateChecker.new(paths) { reload! }
27
+ updater.execute
28
+ updater
29
+ end
30
+ end
20
31
 
21
32
  def clear!
22
33
  route_sets.each do |routes|
@@ -30,7 +30,7 @@ class CodeStatistics #:nodoc:
30
30
  stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
31
31
 
32
32
  Dir.foreach(directory) do |file_name|
33
- if File.stat(directory + "/" + file_name).directory? and (/^\./ !~ file_name)
33
+ if File.directory?(directory + "/" + file_name) and (/^\./ !~ file_name)
34
34
  newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
35
35
  stats.each { |k, v| stats[k] += newstats[k] }
36
36
  end
@@ -38,11 +38,22 @@ class CodeStatistics #:nodoc:
38
38
  next unless file_name =~ pattern
39
39
 
40
40
  f = File.open(directory + "/" + file_name)
41
-
41
+ comment_started = false
42
42
  while line = f.gets
43
43
  stats["lines"] += 1
44
- stats["classes"] += 1 if line =~ /class [A-Z]/
45
- stats["methods"] += 1 if line =~ /def [a-z]/
44
+ if(comment_started)
45
+ if line =~ /^=end/
46
+ comment_started = false
47
+ end
48
+ next
49
+ else
50
+ if line =~ /^=begin/
51
+ comment_started = true
52
+ next
53
+ end
54
+ end
55
+ stats["classes"] += 1 if line =~ /^\s*class\s+[_A-Z]/
56
+ stats["methods"] += 1 if line =~ /^\s*def\s+[_a-z]/
46
57
  stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
47
58
  end
48
59
  end
@@ -104,4 +115,4 @@ class CodeStatistics #:nodoc:
104
115
  puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
105
116
  puts ""
106
117
  end
107
- end
118
+ end
@@ -4,6 +4,7 @@ ARGV << '--help' if ARGV.empty?
4
4
 
5
5
  aliases = {
6
6
  "g" => "generate",
7
+ "d" => "destroy",
7
8
  "c" => "console",
8
9
  "s" => "server",
9
10
  "db" => "dbconsole",
@@ -15,15 +16,15 @@ command = aliases[command] || command
15
16
 
16
17
  case command
17
18
  when 'generate', 'destroy', 'plugin'
19
+ require 'rails/generators'
20
+
18
21
  if command == 'plugin' && ARGV.first == 'new'
19
22
  require "rails/commands/plugin_new"
20
23
  else
21
24
  require APP_PATH
22
25
  Rails.application.require_environment!
23
26
 
24
- if defined?(ENGINE_PATH) && engine = Rails::Engine.find(ENGINE_PATH)
25
- Rails.application = engine
26
- end
27
+ Rails.application.load_generators
27
28
 
28
29
  require "rails/commands/#{command}"
29
30
  end
@@ -87,13 +88,13 @@ The most common rails commands are:
87
88
 
88
89
  In addition to those, there are:
89
90
  application Generate the Rails application code
90
- destroy Undo code generated with "generate"
91
+ destroy Undo code generated with "generate" (short-cut alias: "d")
91
92
  benchmarker See how fast a piece of code runs
92
93
  profiler Get profile information from a piece of code
93
94
  plugin Install a plugin
94
95
  runner Run a piece of code in the application environment (short-cut alias: "r")
95
96
 
96
- All commands can be run with -h for more information.
97
+ All commands can be run with -h (or --help) for more information.
97
98
  EOT
98
99
  exit(1)
99
100
  end
@@ -9,10 +9,17 @@ if ARGV.first != "new"
9
9
  ARGV[0] = "--help"
10
10
  else
11
11
  ARGV.shift
12
+ railsrc = File.join(File.expand_path("~"), ".railsrc")
13
+ if File.exist?(railsrc)
14
+ extra_args_string = File.open(railsrc).read
15
+ extra_args = extra_args_string.split(/\n+/).map {|l| l.split}.flatten
16
+ puts "Using #{extra_args.join(" ")} from #{railsrc}"
17
+ ARGV << extra_args
18
+ ARGV.flatten!
19
+ end
12
20
  end
13
21
 
14
22
  require 'rubygems' if ARGV.include?("--dev")
15
-
16
23
  require 'rails/generators'
17
24
  require 'rails/generators/rails/app/app_generator'
18
25
 
@@ -42,6 +42,8 @@ module Rails
42
42
  else
43
43
  puts "Loading #{Rails.env} environment (Rails #{Rails.version})"
44
44
  end
45
+
46
+ IRB::ExtendCommandBundle.send :include, Rails::ConsoleMethods
45
47
  IRB.start
46
48
  end
47
49
  end
@@ -33,7 +33,7 @@ module Rails
33
33
  options['mode'] = mode
34
34
  end
35
35
 
36
- opt.on("-h", "--header") do |h|
36
+ opt.on("--header") do |h|
37
37
  options['header'] = h
38
38
  end
39
39
 
@@ -41,7 +41,7 @@ module Rails
41
41
  abort opt.to_s unless (0..1).include?(ARGV.size)
42
42
  end
43
43
 
44
- unless config = YAML::load(ERB.new(IO.read("#{@app.root}/config/database.yml")).result)[Rails.env]
44
+ unless config = @app.config.database_configuration[Rails.env]
45
45
  abort "No database is configured for the environment '#{Rails.env}'"
46
46
  end
47
47
 
@@ -1,8 +1,6 @@
1
1
  require 'rails/generators'
2
2
  require 'active_support/core_ext/object/inclusion'
3
3
 
4
- Rails::Generators.configure!
5
-
6
4
  if ARGV.first.in?([nil, "-h", "--help"])
7
5
  Rails::Generators.help 'destroy'
8
6
  exit