Skip to content
This repository
Browse code

Reapply Rails::Application::Path tweaks

  Reapplies: a4bdc00
             3c1dab7
  • Loading branch information...
commit 9a42e06dd8ac5d9abd50b0e47e8de0ac3ab00a9d 1 parent 783db25
Yehuda Katz + Carl Lerche authored
40 railties/lib/rails/configuration.rb
@@ -62,33 +62,19 @@ def set_root_path!
62 62 end
63 63
64 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.concat 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"
  65 + @paths.app "app", :load_path => true
  66 + @paths.app.metals "app/metal", :eager_load => true
  67 + @paths.app.models "app/models", :eager_load => true
  68 + @paths.app.controllers "app/controllers", builtin_directories, :eager_load => true
  69 + @paths.app.helpers "app/helpers", :eager_load => true
  70 + @paths.app.services "app/services", :load_path => true
  71 + @paths.lib "lib", :load_path => true
  72 + @paths.vendor "vendor", :load_path => true
  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", :glob => "#{RAILS_ENV}.rb"
92 78
93 79 RAILS_ROOT.replace root_path
94 80 end
40 railties/lib/rails/paths.rb
@@ -6,8 +6,8 @@ module PathParent
6 6 def method_missing(id, *args)
7 7 name = id.to_s
8 8
9   - if name =~ /^(.*)=$/
10   - @children[$1] = Path.new(args.first, @root)
  9 + if name =~ /^(.*)=$/ || args.any?
  10 + @children[$1 || name] = Path.new(@root, *args)
11 11 elsif path = @children[name]
12 12 path
13 13 else
@@ -28,17 +28,15 @@ 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, @all_paths = [], [], []
  31 + @all_paths = []
32 32 end
33 33
34 34 def load_once
35   - @load_once.uniq!
36   - @load_once
  35 + all_paths.map { |path| path.paths if path.load_once? }.compact.flatten.uniq
37 36 end
38 37
39 38 def eager_load
40   - @eager_load.uniq!
41   - @eager_load
  39 + all_paths.map { |path| path.paths if path.eager_load? }.compact.flatten.uniq
42 40 end
43 41
44 42 def all_paths
@@ -47,7 +45,7 @@ def all_paths
47 45 end
48 46
49 47 def load_paths
50   - all_paths.map { |path| path.paths }.flatten
  48 + all_paths.map { |path| path.paths if path.load_path? }.compact.flatten.uniq
51 49 end
52 50
53 51 def add_to_load_path
@@ -55,6 +53,14 @@ def add_to_load_path
55 53 $LOAD_PATH.unshift(path) if File.directory?(path)
56 54 end
57 55 end
  56 +
  57 + def push(*)
  58 + raise "Application root can only have one physical path"
  59 + end
  60 +
  61 + alias unshift push
  62 + alias << push
  63 + alias concat push
58 64 end
59 65
60 66 class Path
@@ -63,11 +69,18 @@ class Path
63 69 attr_reader :path
64 70 attr_accessor :glob
65 71
66   - def initialize(path, root)
  72 + def initialize(root, *paths)
  73 + @options = paths.last.is_a?(::Hash) ? paths.pop : {}
67 74 @children = {}
68 75 @root = root
69   - @paths = [path].flatten
70   - @glob = "**/*.rb"
  76 + @paths = paths.flatten
  77 + @glob = @options[:glob] || "**/*.rb"
  78 +
  79 + @load_once = @options[:load_once]
  80 + @eager_load = @options[:eager_load]
  81 + @load_path = @options[:load_path] || @eager_load
  82 +
  83 + @root.all_paths << self
71 84 end
72 85
73 86 def push(path)
@@ -86,7 +99,6 @@ def concat(paths)
86 99
87 100 def load_once!
88 101 @load_once = true
89   - @root.load_once.push *self.paths
90 102 end
91 103
92 104 def load_once?
@@ -95,8 +107,7 @@ def load_once?
95 107
96 108 def eager_load!
97 109 @eager_load = true
98   - @root.all_paths << self
99   - @root.eager_load.push *self.paths
  110 + @load_path = true
100 111 end
101 112
102 113 def eager_load?
@@ -105,7 +116,6 @@ def eager_load?
105 116
106 117 def load_path!
107 118 @load_path = true
108   - @root.all_paths << self
109 119 end
110 120
111 121 def load_path?
92 railties/test/paths_test.rb
@@ -17,17 +17,37 @@ def setup
17 17 assert_equal ["/foo/bar"], @root.app.to_a
18 18 end
19 19
  20 + test "creating a root level path without assignment" do
  21 + @root.app "/foo/bar"
  22 + assert_equal ["/foo/bar"], @root.app.to_a
  23 + end
  24 +
  25 + test "trying to access a path that does not exist raises NoMethodError" do
  26 + assert_raises(NoMethodError) { @root.app }
  27 + end
  28 +
20 29 test "relative paths are relative to the paths root" do
21 30 @root.app = "app"
22 31 assert_equal ["/foo/bar/app"], @root.app.to_a
23 32 end
24 33
  34 + test "relative paths are relative to the paths root without assignment" do
  35 + @root.app "app"
  36 + assert_equal ["/foo/bar/app"], @root.app.to_a
  37 + end
  38 +
25 39 test "creating a child level path" do
26 40 @root.app = "/foo/bar"
27 41 @root.app.models = "/foo/bar/baz"
28 42 assert_equal ["/foo/bar/baz"], @root.app.models.to_a
29 43 end
30 44
  45 + test "creating a child level path without assignment" do
  46 + @root.app = "/foo/bar"
  47 + @root.app.models "/foo/bar/baz"
  48 + assert_equal ["/foo/bar/baz"], @root.app.models.to_a
  49 + end
  50 +
31 51 test "child level paths are relative from the root" do
32 52 @root.app = "/app"
33 53 @root.app.models = "baz"
@@ -40,6 +60,11 @@ def setup
40 60 assert_equal ["/app", "/app2"], @root.app.to_a
41 61 end
42 62
  63 + test "adding multiple physical paths as an array without assignment" do
  64 + @root.app "/app", "/app2"
  65 + assert_equal ["/app", "/app2"], @root.app.to_a
  66 + end
  67 +
43 68 test "adding multiple physical paths using #push" do
44 69 @root.app = "/app"
45 70 @root.app.push "/app2"
@@ -66,10 +91,10 @@ def setup
66 91
67 92 test "the root can only have one physical path" do
68 93 assert_raise(RuntimeError) { Rails::Application::Root.new(["/fiz", "/biz"]) }
69   - assert_raise(NoMethodError) { @root.push "/biz" }
70   - assert_raise(NoMethodError) { @root.unshift "/biz" }
71   - assert_raise(NoMethodError) { @root.concat ["/biz"]}
72   - assert_raise(NoMethodError) { @root << "/biz" }
  94 + assert_raise(RuntimeError) { @root.push "/biz" }
  95 + assert_raise(RuntimeError) { @root.unshift "/biz" }
  96 + assert_raise(RuntimeError) { @root.concat ["/biz"]}
  97 + assert_raise(RuntimeError) { @root << "/biz" }
73 98 end
74 99
75 100 test "it is possible to add a path that should be loaded only once" do
@@ -79,6 +104,19 @@ def setup
79 104 assert @root.load_once.include?(@root.app.paths.first)
80 105 end
81 106
  107 + test "it is possible to add a path without assignment and specify it should be loaded only once" do
  108 + @root.app "/app", :load_once => true
  109 + assert @root.app.load_once?
  110 + assert @root.load_once.include?("/app")
  111 + end
  112 +
  113 + test "it is possible to add multiple paths without assignment and specify it should be loaded only once" do
  114 + @root.app "/app", "/app2", :load_once => true
  115 + assert @root.app.load_once?
  116 + assert @root.load_once.include?("/app")
  117 + assert @root.load_once.include?("/app2")
  118 + end
  119 +
82 120 test "making a path load_once more than once only includes it once in @root.load_once" do
83 121 @root.app = "/app"
84 122 @root.app.load_once!
@@ -86,6 +124,13 @@ def setup
86 124 assert_equal 1, @root.load_once.select {|p| p == @root.app.paths.first }.size
87 125 end
88 126
  127 + test "paths added to a load_once path should be added to the load_once collection" do
  128 + @root.app = "/app"
  129 + @root.app.load_once!
  130 + @root.app << "/app2"
  131 + assert_equal 2, @root.load_once.size
  132 + end
  133 +
89 134 test "it is possible to mark a path as eager" do
90 135 @root.app = "/app"
91 136 @root.app.eager_load!
@@ -93,6 +138,27 @@ def setup
93 138 assert @root.eager_load.include?(@root.app.paths.first)
94 139 end
95 140
  141 + test "it is possible to add a path without assignment and mark it as eager" do
  142 + @root.app "/app", :eager_load => true
  143 + assert @root.app.eager_load?
  144 + assert @root.eager_load.include?("/app")
  145 + end
  146 +
  147 + test "it is possible to add multiple paths without assignment and mark them as eager" do
  148 + @root.app "/app", "/app2", :eager_load => true
  149 + assert @root.app.eager_load?
  150 + assert @root.eager_load.include?("/app")
  151 + assert @root.eager_load.include?("/app2")
  152 + end
  153 +
  154 + test "it is possible to create a path without assignment and mark it both as eager and load once" do
  155 + @root.app "/app", :eager_load => true, :load_once => true
  156 + assert @root.app.eager_load?
  157 + assert @root.app.load_once?
  158 + assert @root.eager_load.include?("/app")
  159 + assert @root.load_once.include?("/app")
  160 + end
  161 +
96 162 test "making a path eager more than once only includes it once in @root.eager_paths" do
97 163 @root.app = "/app"
98 164 @root.app.eager_load!
@@ -100,6 +166,13 @@ def setup
100 166 assert_equal 1, @root.eager_load.select {|p| p == @root.app.paths.first }.size
101 167 end
102 168
  169 + test "paths added to a eager_load path should be added to the eager_load collection" do
  170 + @root.app = "/app"
  171 + @root.app.eager_load!
  172 + @root.app << "/app2"
  173 + assert_equal 2, @root.eager_load.size
  174 + end
  175 +
103 176 test "a path should have a glob that defaults to **/*.rb" do
104 177 @root.app = "/app"
105 178 assert_equal "**/*.rb", @root.app.glob
@@ -111,6 +184,11 @@ def setup
111 184 assert_equal "*.rb", @root.app.glob
112 185 end
113 186
  187 + test "it should be possible to override a path's default glob without assignment" do
  188 + @root.app "/app", :glob => "*.rb"
  189 + assert_equal "*.rb", @root.app.glob
  190 + end
  191 +
114 192 test "a path can be added to the load path" do
115 193 @root.app = "app"
116 194 @root.app.load_path!
@@ -118,6 +196,12 @@ def setup
118 196 assert_equal ["/foo/bar/app"], @root.load_paths
119 197 end
120 198
  199 + test "a path can be added to the load path on creation" do
  200 + @root.app "/app", :load_path => true
  201 + assert @root.app.load_path?
  202 + assert_equal ["/app"], @root.load_paths
  203 + end
  204 +
121 205 test "adding a path to the eager paths also adds it to the load path" do
122 206 @root.app = "app"
123 207 @root.app.eager_load!

0 comments on commit 9a42e06

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