Skip to content
This repository
Browse code

Starting to replace scattered path configuration settings with the pa…

…th object
  • Loading branch information...
commit 188a892c5a097ee6d62249d048a6be7d2dfe9649 1 parent 4153c6b
authored June 26, 2009
0  .keep b/railties/test/initializer/root/lib/.keep
No changes.
27  railties/lib/initializer.rb
@@ -5,6 +5,7 @@
5 5
 require 'rails/version'
6 6
 require 'rails/gem_dependency'
7 7
 require 'rails/rack'
  8
+require 'rails/paths'
8 9
 require 'rails/core'
9 10
 require 'rails/configuration'
10 11
 
@@ -112,24 +113,6 @@ def self.run(initializer = nil, config = nil)
112 113
     require 'ruby_version_check'
113 114
   end
114 115
 
115  
-  Initializer.default.add :set_root_path do
116  
-    raise 'RAILS_ROOT is not set' unless defined?(RAILS_ROOT)
117  
-    raise 'RAILS_ROOT is not a directory' unless File.directory?(RAILS_ROOT)
118  
-
119  
-    configuration.root_path =
120  
-      # Pathname is incompatible with Windows, but Windows doesn't have
121  
-      # real symlinks so File.expand_path is safe.
122  
-      if RUBY_PLATFORM =~ /(:?mswin|mingw)/
123  
-        File.expand_path(RAILS_ROOT)
124  
-
125  
-      # Otherwise use Pathname#realpath which respects symlinks.
126  
-      else
127  
-        Pathname.new(RAILS_ROOT).realpath.to_s
128  
-      end
129  
-
130  
-    RAILS_ROOT.replace configuration.root_path
131  
-  end
132  
-
133 116
   # If Rails is vendored and RubyGems is available, install stub GemSpecs
134 117
   # for Rails, Active Support, Active Record, Action Pack, Action Mailer, and
135 118
   # Active Resource. This allows Gem plugins to depend on Rails even when
@@ -158,8 +141,9 @@ def self.run(initializer = nil, config = nil)
158 141
   # Set the <tt>$LOAD_PATH</tt> based on the value of
159 142
   # Configuration#load_paths. Duplicates are removed.
160 143
   Initializer.default.add :set_load_path do
161  
-    load_paths = configuration.load_paths + configuration.framework_paths
162  
-    load_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
  144
+    # TODO: Think about unifying this with the general Rails paths
  145
+    configuration.framework_paths.reverse_each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
  146
+    configuration.paths.add_to_load_path
163 147
     $LOAD_PATH.uniq!
164 148
   end
165 149
 
@@ -221,6 +205,8 @@ def self.run(initializer = nil, config = nil)
221 205
   Initializer.default.add :load_environment do
222 206
     silence_warnings do
223 207
       next if @environment_loaded
  208
+      next unless File.file?(configuration.environment_path)
  209
+
224 210
       @environment_loaded = true
225 211
 
226 212
       config = configuration
@@ -564,6 +550,7 @@ def self.run(initializer = nil, config = nil)
564 550
   # Eager load application classes
565 551
   Initializer.default.add :load_application_classes do
566 552
     next if $rails_rake_task
  553
+
567 554
     if configuration.cache_classes
568 555
       configuration.eager_load_paths.each do |load_path|
569 556
         matcher = /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
33  railties/lib/rails/configuration.rb
@@ -10,7 +10,7 @@ class Configuration
10 10
                   :log_path, :log_level, :logger, :preload_frameworks,
11 11
                   :database_configuration_file, :cache_store, :time_zone,
12 12
                   :view_path, :metals, :controller_paths, :routes_configuration_file,
13  
-                  :eager_load_paths, :dependency_loading
  13
+                  :eager_load_paths, :dependency_loading, :paths
14 14
 
15 15
     def initialize
16 16
       set_root_path!
@@ -61,7 +61,36 @@ def set_root_path!
61 61
           Pathname.new(RAILS_ROOT).realpath.to_s
62 62
         end
63 63
 
64  
-      RAILS_ROOT.replace self.root_path
  64
+      @paths = Rails::Application::Root.new(root_path)
  65
+      @paths.app                 = "app"
  66
+      @paths.app.metals          = "app/metal"
  67
+      @paths.app.models          = "app/models"
  68
+      @paths.app.controllers     = "app/controllers"
  69
+      @paths.app.helpers         = "app/helpers"
  70
+      @paths.app.services        = "app/services"
  71
+      @paths.lib                 = "lib"
  72
+      @paths.vendor              = "vendor"
  73
+      @paths.vendor.plugins      = "vendor/plugins"
  74
+      @paths.cache               = "tmp/cache"
  75
+      @paths.config              = "config"
  76
+      @paths.config.locales      = "config/locales"
  77
+      @paths.config.environments = "config/environments"
  78
+
  79
+      @paths.app.controllers.push *builtin_directories
  80
+
  81
+      @paths.app.load_path!
  82
+      @paths.app.metals.load_path!
  83
+      @paths.app.models.eager_load!
  84
+      @paths.app.controllers.eager_load!
  85
+      @paths.app.helpers.eager_load!
  86
+      @paths.app.services.load_path!
  87
+      @paths.app.metals.eager_load!
  88
+      @paths.lib.load_path!
  89
+      @paths.vendor.load_path!
  90
+
  91
+      @paths.config.environments.glob = "#{RAILS_ENV}.rb"
  92
+
  93
+      RAILS_ROOT.replace root_path
65 94
     end
66 95
 
67 96
     # Enable threaded mode. Allows concurrent requests to controller actions and
43  railties/lib/rails/paths.rb
@@ -19,7 +19,7 @@ def method_missing(id, *args)
19 19
     class Root
20 20
       include PathParent
21 21
 
22  
-      attr_reader :path, :load_once, :eager_load
  22
+      attr_reader :path
23 23
       def initialize(path)
24 24
         raise unless path.is_a?(String)
25 25
 
@@ -28,7 +28,32 @@ def initialize(path)
28 28
         # TODO: Move logic from set_root_path initializer
29 29
         @path = File.expand_path(path)
30 30
         @root = self
31  
-        @load_once, @eager_load = Set.new, Set.new
  31
+        @load_once, @eager_load, @all_paths = [], [], []
  32
+      end
  33
+
  34
+      def load_once
  35
+        @load_once.uniq!
  36
+        @load_once
  37
+      end
  38
+
  39
+      def eager_load
  40
+        @eager_load.uniq!
  41
+        @eager_load
  42
+      end
  43
+
  44
+      def all_paths
  45
+        @all_paths.uniq!
  46
+        @all_paths
  47
+      end
  48
+
  49
+      def load_paths
  50
+        all_paths.map { |path| path.paths }.flatten
  51
+      end
  52
+
  53
+      def add_to_load_path
  54
+        load_paths.reverse_each do |path|
  55
+          $LOAD_PATH.unshift(path) if File.directory?(path)
  56
+        end
32 57
       end
33 58
     end
34 59
 
@@ -57,7 +82,7 @@ def unshift(path)
57 82
 
58 83
       def load_once!
59 84
         @load_once = true
60  
-        @root.load_once << self
  85
+        @root.load_once.push *self.paths
61 86
       end
62 87
 
63 88
       def load_once?
@@ -66,13 +91,23 @@ def load_once?
66 91
 
67 92
       def eager_load!
68 93
         @eager_load = true
69  
-        @root.eager_load << self
  94
+        @root.all_paths << self
  95
+        @root.eager_load.push *self.paths
70 96
       end
71 97
 
72 98
       def eager_load?
73 99
         @eager_load
74 100
       end
75 101
 
  102
+      def load_path!
  103
+        @load_path = true
  104
+        @root.all_paths << self
  105
+      end
  106
+
  107
+      def load_path?
  108
+        @load_path
  109
+      end
  110
+
76 111
       def paths
77 112
         @paths.map do |path|
78 113
           path.index('/') == 0 ? path : File.join(@root.path, path)
2  railties/lib/railties_path.rb
... ...
@@ -1 +1 @@
1  
-RAILTIES_PATH = File.join(File.dirname(__FILE__), '..')
  1
+RAILTIES_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..'))
1  railties/test/abstract_unit.rb
@@ -2,6 +2,7 @@
2 2
 $:.unshift File.dirname(__FILE__) + "/../../activerecord/lib"
3 3
 $:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
4 4
 $:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"
  5
+$:.unshift File.dirname(__FILE__) + "/../../activeresource/lib"
5 6
 $:.unshift File.dirname(__FILE__) + "/../lib"
6 7
 $:.unshift File.dirname(__FILE__) + "/../builtin/rails_info"
7 8
 
86  railties/test/initializer/path_test.rb
... ...
@@ -0,0 +1,86 @@
  1
+require 'abstract_unit'
  2
+require 'active_support/ruby/shim'
  3
+require 'initializer'
  4
+
  5
+RAILS_ROOT.replace File.join(File.dirname(__FILE__), "root")
  6
+
  7
+module Rails
  8
+  def self.vendor_rails? ; false ; end
  9
+end
  10
+
  11
+# TODO: Can this be reset?
  12
+Rails::Initializer.run do |config|
  13
+  config.frameworks = [:action_controller, :action_view, :action_mailer, :active_record]
  14
+end
  15
+
  16
+class PathsTest < ActiveSupport::TestCase
  17
+  def setup
  18
+    @paths = Rails::Initializer.default.config.paths
  19
+  end
  20
+
  21
+  def root(*path)
  22
+    File.expand_path(File.join(File.dirname(__FILE__), "root", *path))
  23
+  end
  24
+
  25
+  def assert_path(paths, *dir)
  26
+    assert_equal [root(*dir)], paths.paths
  27
+  end
  28
+
  29
+  test "booting up Rails yields a valid paths object" do
  30
+    assert_path @paths.app, "app"
  31
+    assert_path @paths.app.metals, "app", "metal"
  32
+    assert_path @paths.app.models, "app", "models"
  33
+    assert_path @paths.app.helpers, "app", "helpers"
  34
+    assert_path @paths.app.services, "app", "services"
  35
+    assert_path @paths.lib, "lib"
  36
+    assert_path @paths.vendor, "vendor"
  37
+    assert_path @paths.vendor.plugins, "vendor", "plugins"
  38
+    assert_path @paths.cache, "tmp", "cache"
  39
+    assert_path @paths.config, "config"
  40
+    assert_path @paths.config.locales, "config", "locales"
  41
+    assert_path @paths.config.environments, "config", "environments"
  42
+
  43
+    assert_equal Pathname.new(File.dirname(__FILE__)).join("root", "app", "controllers").expand_path,
  44
+      Pathname.new(@paths.app.controllers.to_a.first).expand_path
  45
+    assert_equal Pathname.new(File.dirname(__FILE__)).join("..", "..", "builtin", "rails_info").expand_path,
  46
+      Pathname.new(@paths.app.controllers.to_a[1]).expand_path
  47
+  end
  48
+
  49
+  test "booting up Rails yields a list of paths that are eager" do
  50
+    assert @paths.app.models.eager_load?
  51
+    assert @paths.app.controllers.eager_load?
  52
+    assert @paths.app.helpers.eager_load?
  53
+    assert @paths.app.metals.eager_load?
  54
+  end
  55
+
  56
+  test "environments has a glob equal to the current environment" do
  57
+    assert_equal "#{RAILS_ENV}.rb", @paths.config.environments.glob
  58
+  end
  59
+
  60
+  def assert_in_load_path(*path)
  61
+    assert $:.any? { |p| File.expand_path(p) == root(*path) }, "Load path does not include '#{root(*path)}'. They are:\n-----\n #{$:.join("\n")}\n-----"
  62
+  end
  63
+
  64
+  def assert_not_in_load_path(*path)
  65
+    assert !$:.any? { |p| File.expand_path(p) == root(*path) }, "Load path includes '#{root(*path)}'. They are:\n-----\n #{$:.join("\n")}\n-----"
  66
+  end
  67
+
  68
+  test "load path includes each of the paths in config.paths as long as the directories exist" do
  69
+    assert_in_load_path "app"
  70
+    assert_in_load_path "app", "controllers"
  71
+    assert_in_load_path "app", "metal"
  72
+    assert_in_load_path "app", "models"
  73
+    assert_in_load_path "app", "helpers"
  74
+    assert_in_load_path "lib"
  75
+    assert_in_load_path "vendor"
  76
+
  77
+    assert_not_in_load_path "app", "views"
  78
+    assert_not_in_load_path "app", "services"
  79
+    assert_not_in_load_path "config"
  80
+    assert_not_in_load_path "config", "locales"
  81
+    assert_not_in_load_path "config", "environments"
  82
+    assert_not_in_load_path "tmp"
  83
+    assert_not_in_load_path "tmp", "cache"
  84
+  end
  85
+
  86
+end
0  railties/test/initializer/root/app/controllers/.keep
No changes.
0  railties/test/initializer/root/app/helpers/.keep
No changes.
0  railties/test/initializer/root/app/metal/.keep
No changes.
0  railties/test/initializer/root/app/models/.keep
No changes.
0  railties/test/initializer/root/app/views/.keep
No changes.
4  railties/test/initializer/root/config/database.yml
... ...
@@ -0,0 +1,4 @@
  1
+development:
  2
+  adapter: sqlite3
  3
+  database: db/railties.db
  4
+  timeout: 5000
0  railties/test/initializer/root/config/environments/.keep
No changes.
0  railties/test/initializer/root/config/locales/.keep
No changes.
0  railties/test/initializer/root/config/routes.rb
No changes.
0  railties/test/initializer/root/tmp/.keep
No changes.
0  railties/test/initializer/root/tmp/cache/.keep
No changes.
0  railties/test/initializer/root/vendor/.keep
No changes.
21  railties/test/paths_test.rb
@@ -69,28 +69,28 @@ def setup
69 69
     @root.app = "/app"
70 70
     @root.app.load_once!
71 71
     assert @root.app.load_once?
72  
-    assert @root.load_once.include?(@root.app)
  72
+    assert @root.load_once.include?(@root.app.paths.first)
73 73
   end
74 74
 
75 75
   test "making a path load_once more than once only includes it once in @root.load_once" do
76 76
     @root.app = "/app"
77 77
     @root.app.load_once!
78 78
     @root.app.load_once!
79  
-    assert_equal 1, @root.load_once.select {|p| p == @root.app }.size
  79
+    assert_equal 1, @root.load_once.select {|p| p == @root.app.paths.first }.size
80 80
   end
81 81
 
82 82
   test "it is possible to mark a path as eager" do
83 83
     @root.app = "/app"
84 84
     @root.app.eager_load!
85 85
     assert @root.app.eager_load?
86  
-    assert @root.eager_load.include?(@root.app)
  86
+    assert @root.eager_load.include?(@root.app.paths.first)
87 87
   end
88 88
 
89 89
   test "making a path eager more than once only includes it once in @root.eager_paths" do
90 90
     @root.app = "/app"
91 91
     @root.app.eager_load!
92 92
     @root.app.eager_load!
93  
-    assert_equal 1, @root.eager_load.select {|p| p == @root.app }.size
  93
+    assert_equal 1, @root.eager_load.select {|p| p == @root.app.paths.first }.size
94 94
   end
95 95
 
96 96
   test "a path should have a glob that defaults to **/*.rb" do
@@ -103,4 +103,17 @@ def setup
103 103
     @root.app.glob = "*.rb"
104 104
     assert_equal "*.rb", @root.app.glob
105 105
   end
  106
+
  107
+  test "a path can be added to the load path" do
  108
+    @root.app = "app"
  109
+    @root.app.load_path!
  110
+    @root.app.models = "app/models"
  111
+    assert_equal ["/foo/bar/app"], @root.load_paths
  112
+  end
  113
+
  114
+  test "adding a path to the eager paths also adds it to the load path" do
  115
+    @root.app = "app"
  116
+    @root.app.eager_load!
  117
+    assert_equal ["/foo/bar/app"], @root.load_paths
  118
+  end
106 119
 end
Something went wrong with that request. Please try again.