Skip to content
This repository
Browse code

renames load_(once_)paths to autoload_(once_)paths in dependencies an…

…d config
  • Loading branch information...
commit 6f83a5036d8a9c3f8ed74755ff6d42bc3f6e9982 1 parent 4a0a640
Xavier Noria authored
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *Rails 3.0.0 [Release Candidate] (unreleased)*
2 2
 
  3
+* Renamed ActiveSupport::Dependecies.load_(once_)paths to autoload_(once_)paths. [fxn]
  4
+
3 5
 * Added ActiveSupport::FileUpdateChecker to execute a block only if a set of files changed, used by Router and I18n locale files. [José Valim]
4 6
 
5 7
 * Added ActiveSupport::DescendantsTracker to track descendants with support to constants reloading. [José Valim]
24  activesupport/lib/active_support/dependencies.rb
@@ -33,14 +33,14 @@ module Dependencies #:nodoc:
33 33
 
34 34
     # The set of directories from which we may automatically load files. Files
35 35
     # under these directories will be reloaded on each request in development mode,
36  
-    # unless the directory also appears in load_once_paths.
37  
-    mattr_accessor :load_paths
38  
-    self.load_paths = []
  36
+    # unless the directory also appears in autoload_once_paths.
  37
+    mattr_accessor :autoload_paths
  38
+    self.autoload_paths = []
39 39
 
40 40
     # The set of directories from which automatically loaded constants are loaded
41  
-    # only once. All directories in this set must also be present in +load_paths+.
42  
-    mattr_accessor :load_once_paths
43  
-    self.load_once_paths = []
  41
+    # only once. All directories in this set must also be present in +autoload_paths+.
  42
+    mattr_accessor :autoload_once_paths
  43
+    self.autoload_once_paths = []
44 44
 
45 45
     # An array of qualified constant names that have been loaded. Adding a name to
46 46
     # this array will cause it to be unloaded the next time Dependencies are cleared.
@@ -352,7 +352,7 @@ def local_const_defined?(mod, const) #:nodoc:
352 352
 
353 353
     # Given +path+, a filesystem path to a ruby file, return an array of constant
354 354
     # paths which would cause Dependencies to attempt to load this file.
355  
-    def loadable_constants_for_path(path, bases = load_paths)
  355
+    def loadable_constants_for_path(path, bases = autoload_paths)
356 356
       expanded_path = Pathname.new(path[/\A(.*?)(\.rb)?\Z/, 1]).expand_path
357 357
 
358 358
       bases.inject([]) do |paths, root|
@@ -363,11 +363,11 @@ def loadable_constants_for_path(path, bases = load_paths)
363 363
       end.uniq
364 364
     end
365 365
 
366  
-    # Search for a file in load_paths matching the provided suffix.
  366
+    # Search for a file in autoload_paths matching the provided suffix.
367 367
     def search_for_file(path_suffix)
368 368
       path_suffix = path_suffix.sub(/(\.rb)?$/, ".rb")
369 369
 
370  
-      load_paths.each do |root|
  370
+      autoload_paths.each do |root|
371 371
         path = File.join(root, path_suffix)
372 372
         return path if File.file? path
373 373
       end
@@ -377,14 +377,14 @@ def search_for_file(path_suffix)
377 377
     # Does the provided path_suffix correspond to an autoloadable module?
378 378
     # Instead of returning a boolean, the autoload base for this module is returned.
379 379
     def autoloadable_module?(path_suffix)
380  
-      load_paths.each do |load_path|
  380
+      autoload_paths.each do |load_path|
381 381
         return load_path if File.directory? File.join(load_path, path_suffix)
382 382
       end
383 383
       nil
384 384
     end
385 385
 
386 386
     def load_once_path?(path)
387  
-      load_once_paths.any? { |base| path.starts_with? base }
  387
+      autoload_once_paths.any? { |base| path.starts_with? base }
388 388
     end
389 389
 
390 390
     # Attempt to autoload the provided module name by searching for a directory
@@ -396,7 +396,7 @@ def autoload_module!(into, const_name, qualified_name, path_suffix)
396 396
       return nil unless base_path = autoloadable_module?(path_suffix)
397 397
       mod = Module.new
398 398
       into.const_set const_name, mod
399  
-      autoloaded_constants << qualified_name unless load_once_paths.include?(base_path)
  399
+      autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
400 400
       return mod
401 401
     end
402 402
 
36  activesupport/test/dependencies_test.rb
@@ -25,11 +25,11 @@ def with_loading(*from)
25 25
     this_dir = File.dirname(__FILE__)
26 26
     parent_dir = File.dirname(this_dir)
27 27
     $LOAD_PATH.unshift(parent_dir) unless $LOAD_PATH.include?(parent_dir)
28  
-    prior_load_paths = ActiveSupport::Dependencies.load_paths
29  
-    ActiveSupport::Dependencies.load_paths = from.collect { |f| "#{this_dir}/#{f}" }
  28
+    prior_autoload_paths = ActiveSupport::Dependencies.autoload_paths
  29
+    ActiveSupport::Dependencies.autoload_paths = from.collect { |f| "#{this_dir}/#{f}" }
30 30
     yield
31 31
   ensure
32  
-    ActiveSupport::Dependencies.load_paths = prior_load_paths
  32
+    ActiveSupport::Dependencies.autoload_paths = prior_autoload_paths
33 33
     ActiveSupport::Dependencies.mechanism = old_mechanism
34 34
     ActiveSupport::Dependencies.explicitly_unloadable_constants = []
35 35
   end
@@ -264,7 +264,7 @@ def test_loadable_constants_for_path_should_handle_relative_paths
264 264
   def test_loadable_constants_for_path_should_provide_all_results
265 265
     fake_root = '/usr/apps/backpack'
266 266
     with_loading fake_root, fake_root + '/lib' do
267  
-      root = ActiveSupport::Dependencies.load_paths.first
  267
+      root = ActiveSupport::Dependencies.autoload_paths.first
268 268
       assert_equal ["Lib::A::B", "A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/lib/a/b')
269 269
     end
270 270
   end
@@ -272,7 +272,7 @@ def test_loadable_constants_for_path_should_provide_all_results
272 272
   def test_loadable_constants_for_path_should_uniq_results
273 273
     fake_root = '/usr/apps/backpack/lib'
274 274
     with_loading fake_root, fake_root + '/' do
275  
-      root = ActiveSupport::Dependencies.load_paths.first
  275
+      root = ActiveSupport::Dependencies.autoload_paths.first
276 276
       assert_equal ["A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/a/b')
277 277
     end
278 278
   end
@@ -344,7 +344,7 @@ def test_qualified_name_for
344 344
 
345 345
   def test_file_search
346 346
     with_loading 'dependencies' do
347  
-      root = ActiveSupport::Dependencies.load_paths.first
  347
+      root = ActiveSupport::Dependencies.autoload_paths.first
348 348
       assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three')
349 349
       assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three.rb')
350 350
       assert_equal root + '/service_one.rb', ActiveSupport::Dependencies.search_for_file('service_one')
@@ -354,14 +354,14 @@ def test_file_search
354 354
 
355 355
   def test_file_search_uses_first_in_load_path
356 356
     with_loading 'dependencies', 'autoloading_fixtures' do
357  
-      deps, autoload = ActiveSupport::Dependencies.load_paths
  357
+      deps, autoload = ActiveSupport::Dependencies.autoload_paths
358 358
       assert_match %r/dependencies/, deps
359 359
       assert_match %r/autoloading_fixtures/, autoload
360 360
 
361 361
       assert_equal deps + '/conflict.rb', ActiveSupport::Dependencies.search_for_file('conflict')
362 362
     end
363 363
     with_loading 'autoloading_fixtures', 'dependencies' do
364  
-      autoload, deps = ActiveSupport::Dependencies.load_paths
  364
+      autoload, deps = ActiveSupport::Dependencies.autoload_paths
365 365
       assert_match %r/dependencies/, deps
366 366
       assert_match %r/autoloading_fixtures/, autoload
367 367
 
@@ -418,7 +418,7 @@ def test_const_missing_within_anonymous_module
418 418
 
419 419
   def test_removal_from_tree_should_be_detected
420 420
     with_loading 'dependencies' do
421  
-      root = ActiveSupport::Dependencies.load_paths.first
  421
+      root = ActiveSupport::Dependencies.autoload_paths.first
422 422
       c = ServiceOne
423 423
       ActiveSupport::Dependencies.clear
424 424
       assert ! defined?(ServiceOne)
@@ -433,7 +433,7 @@ def test_removal_from_tree_should_be_detected
433 433
 
434 434
   def test_references_should_work
435 435
     with_loading 'dependencies' do
436  
-      root = ActiveSupport::Dependencies.load_paths.first
  436
+      root = ActiveSupport::Dependencies.autoload_paths.first
437 437
       c = ActiveSupport::Dependencies.ref("ServiceOne")
438 438
       service_one_first = ServiceOne
439 439
       assert_equal service_one_first, c.get
@@ -460,9 +460,9 @@ def test_nested_load_error_isnt_rescued
460 460
     end
461 461
   end
462 462
 
463  
-  def test_load_once_paths_do_not_add_to_autoloaded_constants
  463
+  def test_autoload_once_paths_do_not_add_to_autoloaded_constants
464 464
     with_autoloading_fixtures do
465  
-      ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths.dup
  465
+      ActiveSupport::Dependencies.autoload_once_paths = ActiveSupport::Dependencies.autoload_paths.dup
466 466
 
467 467
       assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder")
468 468
       assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass")
@@ -473,7 +473,7 @@ def test_load_once_paths_do_not_add_to_autoloaded_constants
473 473
     end
474 474
   ensure
475 475
     Object.class_eval { remove_const :ModuleFolder }
476  
-    ActiveSupport::Dependencies.load_once_paths = []
  476
+    ActiveSupport::Dependencies.autoload_once_paths = []
477 477
   end
478 478
 
479 479
   def test_application_should_special_case_application_controller
@@ -760,20 +760,20 @@ def test_remove_constant_handles_double_colon_at_start
760 760
 
761 761
   def test_load_once_constants_should_not_be_unloaded
762 762
     with_autoloading_fixtures do
763  
-      ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths
  763
+      ActiveSupport::Dependencies.autoload_once_paths = ActiveSupport::Dependencies.autoload_paths
764 764
       ::A.to_s
765 765
       assert defined?(A)
766 766
       ActiveSupport::Dependencies.clear
767 767
       assert defined?(A)
768 768
     end
769 769
   ensure
770  
-    ActiveSupport::Dependencies.load_once_paths = []
  770
+    ActiveSupport::Dependencies.autoload_once_paths = []
771 771
     Object.class_eval { remove_const :A if const_defined?(:A) }
772 772
   end
773 773
 
774  
-  def test_load_once_paths_should_behave_when_recursively_loading
  774
+  def test_autoload_once_paths_should_behave_when_recursively_loading
775 775
     with_loading 'dependencies', 'autoloading_fixtures' do
776  
-      ActiveSupport::Dependencies.load_once_paths = [ActiveSupport::Dependencies.load_paths.last]
  776
+      ActiveSupport::Dependencies.autoload_once_paths = [ActiveSupport::Dependencies.autoload_paths.last]
777 777
       assert !defined?(CrossSiteDependency)
778 778
       assert_nothing_raised { CrossSiteDepender.nil? }
779 779
       assert defined?(CrossSiteDependency)
@@ -784,7 +784,7 @@ def test_load_once_paths_should_behave_when_recursively_loading
784 784
         "CrossSiteDependency shouldn't have been unloaded!"
785 785
     end
786 786
   ensure
787  
-    ActiveSupport::Dependencies.load_once_paths = []
  787
+    ActiveSupport::Dependencies.autoload_once_paths = []
788 788
   end
789 789
 
790 790
   def test_hook_called_multiple_times
3  railties/CHANGELOG
... ...
@@ -1,6 +1,9 @@
1 1
 *Rails 3.0.0 [Release Candidate] (unreleased)*
2 2
 
  3
+* config.load_(once_)paths in config/application.rb got renamed to config.autoload_(once_)paths. [fxn]
  4
+
3 5
 * Abort generation/booting on Ruby 1.9.1. [fxn]
  6
+
4 7
 * Made the rails command work even when you're in a subdirectory [Chad Fowler]
5 8
 
6 9
 
4  railties/lib/rails/application.rb
@@ -67,7 +67,7 @@ def inherited(base)
67 67
         raise "You cannot have more than one Rails::Application" if Rails.application
68 68
         super
69 69
         Rails.application = base.instance
70  
-        Rails.application.add_lib_to_load_paths!
  70
+        Rails.application.add_lib_to_load_path!
71 71
         ActiveSupport.run_load_hooks(:before_configuration, base.instance)
72 72
       end
73 73
 
@@ -97,7 +97,7 @@ def method_missing(*args, &block)
97 97
     # are changing config.root inside your application definition or having a custom
98 98
     # Rails application, you will need to add lib to $LOAD_PATH on your own in case
99 99
     # you need to load files in lib/ during the application configuration as well.
100  
-    def add_lib_to_load_paths! #:nodoc:
  100
+    def add_lib_to_load_path! #:nodoc:
101 101
       path = config.root.join('lib').to_s
102 102
       $LOAD_PATH.unshift(path) if File.exists?(path)
103 103
     end
10  railties/lib/rails/application/finisher.rb
@@ -7,14 +7,14 @@ module Finisher
7 7
         config.generators.templates.unshift(*paths.lib.templates.to_a)
8 8
       end
9 9
 
10  
-      initializer :ensure_load_once_paths_as_subset do
11  
-        extra = ActiveSupport::Dependencies.load_once_paths -
12  
-                ActiveSupport::Dependencies.load_paths
  10
+      initializer :ensure_autoload_once_paths_as_subset do
  11
+        extra = ActiveSupport::Dependencies.autoload_once_paths -
  12
+                ActiveSupport::Dependencies.autoload_paths
13 13
 
14 14
         unless extra.empty?
15 15
           abort <<-end_error
16  
-            load_once_paths must be a subset of the load_paths.
17  
-            Extra items in load_once_paths: #{extra * ','}
  16
+            autoload_once_paths must be a subset of the autoload_paths.
  17
+            Extra items in autoload_once_paths: #{extra * ','}
18 18
           end_error
19 19
         end
20 20
       end
16  railties/lib/rails/engine.rb
@@ -32,14 +32,14 @@ module Rails
32 32
   # == Configuration
33 33
   #
34 34
   # Besides the Railtie configuration which is shared across the application, in a
35  
-  # Rails::Engine you can access load_paths, eager_load_paths and load_once_paths,
  35
+  # Rails::Engine you can access autoload_paths, eager_load_paths and autoload_once_paths,
36 36
   # which differently from a Railtie, are scoped to the current Engine.
37 37
   #
38 38
   # Example:
39 39
   #
40 40
   #   class MyEngine < Rails::Engine
41 41
   #     # Add a load path for this specific Engine
42  
-  #     config.load_paths << File.expand_path("../lib/some/path", __FILE__)
  42
+  #     config.autoload_paths << File.expand_path("../lib/some/path", __FILE__)
43 43
   #
44 44
   #     initializer "my_engine.add_middleware" do |app|
45 45
   #       app.middleware.use MyEngine::Middleware
@@ -142,7 +142,7 @@ def eager_load!
142 142
 
143 143
     # Add configured load paths to ruby load paths and remove duplicates.
144 144
     initializer :set_load_path, :before => :bootstrap_hook do
145  
-      config.load_paths.reverse_each do |path|
  145
+      config.autoload_paths.reverse_each do |path|
146 146
         $LOAD_PATH.unshift(path) if File.directory?(path)
147 147
       end
148 148
       $LOAD_PATH.uniq!
@@ -154,17 +154,17 @@ def eager_load!
154 154
     # This needs to be an initializer, since it needs to run once
155 155
     # per engine and get the engine as a block parameter
156 156
     initializer :set_autoload_paths, :before => :bootstrap_hook do |app|
157  
-      ActiveSupport::Dependencies.load_paths.unshift(*config.load_paths)
  157
+      ActiveSupport::Dependencies.autoload_paths.unshift(*config.autoload_paths)
158 158
 
159 159
       if reloadable?(app)
160  
-        ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_once_paths)
  160
+        ActiveSupport::Dependencies.autoload_once_paths.unshift(*config.autoload_once_paths)
161 161
       else
162  
-        ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_paths)
  162
+        ActiveSupport::Dependencies.autoload_once_paths.unshift(*config.autoload_paths)
163 163
       end
164 164
 
165 165
       # Freeze so future modifications will fail rather than do nothing mysteriously
166  
-      config.load_paths.freeze
167  
-      config.load_once_paths.freeze
  166
+      config.autoload_paths.freeze
  167
+      config.autoload_once_paths.freeze
168 168
     end
169 169
 
170 170
     initializer :add_routing_paths do |app|
10  railties/lib/rails/engine/configuration.rb
@@ -4,7 +4,7 @@ module Rails
4 4
   class Engine
5 5
     class Configuration < ::Rails::Railtie::Configuration
6 6
       attr_reader :root
7  
-      attr_writer :eager_load_paths, :load_once_paths, :load_paths
  7
+      attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths
8 8
 
9 9
       def initialize(root=nil)
10 10
         super()
@@ -41,12 +41,12 @@ def eager_load_paths
41 41
         @eager_load_paths ||= paths.eager_load
42 42
       end
43 43
 
44  
-      def load_once_paths
45  
-        @load_once_paths ||= paths.load_once
  44
+      def autoload_once_paths
  45
+        @autoload_once_paths ||= paths.load_once
46 46
       end
47 47
 
48  
-      def load_paths
49  
-        @load_paths ||= paths.load_paths
  48
+      def autoload_paths
  49
+        @autoload_paths ||= paths.load_paths
50 50
       end
51 51
     end
52 52
   end
2  railties/lib/rails/generators/rails/app/templates/config/application.rb
@@ -22,7 +22,7 @@ class Application < Rails::Application
22 22
     # -- all .rb files in that directory are automatically loaded.
23 23
 
24 24
     # Add additional load paths for your own custom dirs
25  
-    # config.load_paths += %W( #{config.root}/extras )
  25
+    # config.autoload_paths += %W( #{config.root}/extras )
26 26
 
27 27
     # Only load the plugins named here, in the order given (default is alphabetical).
28 28
     # :all can be used as a placeholder for all plugins not explicitly named
6  railties/test/generators/actions_test.rb
@@ -130,9 +130,9 @@ def test_gem_should_insert_on_separate_lines
130 130
 
131 131
   def test_environment_should_include_data_in_environment_initializer_block
132 132
     run_generator
133  
-    load_paths = 'config.load_paths += %w["#{Rails.root}/app/extras"]'
134  
-    action :environment, load_paths
135  
-    assert_file 'config/application.rb', /#{Regexp.escape(load_paths)}/
  133
+    autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]'
  134
+    action :environment, autoload_paths
  135
+    assert_file 'config/application.rb', /#{Regexp.escape(autoload_paths)}/
136 136
   end
137 137
 
138 138
   def test_environment_with_block_should_include_block_contents_in_environment_initializer_block

10 notes on commit 6f83a50

Jeremy Walker
iHiD commented on 6f83a50

Was load_paths depreciated?

This just broke all my apps with no previous warning.

Thanks,
iHiD

José Valim
Owner

Yes, it was. The deprecation was placed on Rails 2.3 apps, so it was a bit harder on people already using Rails 3 apps.

Jeremy Walker
iHiD commented on 6f83a50

OK, that's fair enough. Thank you for answering.

Joshua Partogi

I have a library under lib/a/b/c.rb and this change broke my app now.

With this change does it mean we have to load the libraries under the lib/ folder it as such:
Lib::A::B::C

Xavier Noria
Owner
fxn commented on 6f83a50

@scrum8, this particular commit only renames. Perhaps you mean 9b19a6f ?

If that is the case, you need to require c.rb where A::B::C is used:

require "a/b/c"

Note that you can use that relative path because lib is still in Ruby's load path.

Joshua Partogi

Hi @fxn,

Thanks for the response. So now we have to explicitly call require? Did I get this right?

Xavier Noria
Owner
fxn commented on 6f83a50

@scrum8, by default yes.

You can still get the old behavior adding lib to config.autoload_paths in config/application.rb.

Joshua Partogi

Thanks fxn,

Any reason why we have to explicitly configure it in config/application.rb now?

Xavier Noria
Owner
fxn commented on 6f83a50

Applications are no longer configured in config/environment.rb in Rails 3. If you generate a new Rails 3 application you'll see there's a config/application.rb with contents that resemble what config/environment.rb had before.

Ryan Carmelo Briones

(Sorry to be late to the party) Is there any explanation available on the interwebs on why the decision to not autoload lib by default?

Please sign in to comment.
Something went wrong with that request. Please try again.