Skip to content
This repository
Browse code

Fix a bug in ActionDispatch::Static where Rails cannot find assets if…

… started in another directory which is not the RAILS_ROOT.
  • Loading branch information...
commit 4e92134dfa15ae198c714b85376838ffec5a7773 1 parent 336cb3c
José Valim authored April 08, 2010
4  actionpack/lib/action_controller/caching/pages.rb
@@ -38,22 +38,22 @@ module Pages
38 38
       extend ActiveSupport::Concern
39 39
 
40 40
       included do
41  
-        @@page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : ""
42 41
         ##
43 42
         # :singleton-method:
44 43
         # The cache directory should be the document root for the web server and is set using <tt>Base.page_cache_directory = "/document/root"</tt>.
45 44
         # For Rails, this directory has already been set to Rails.public_path (which is usually set to <tt>RAILS_ROOT + "/public"</tt>). Changing
46 45
         # this setting can be useful to avoid naming conflicts with files in <tt>public/</tt>, but doing so will likely require configuring your
47 46
         # web server to look in the new location for cached files.
  47
+        @@page_cache_directory = ''
48 48
         cattr_accessor :page_cache_directory
49 49
 
50  
-        @@page_cache_extension = '.html'
51 50
         ##
52 51
         # :singleton-method:
53 52
         # Most Rails requests do not have an extension, such as <tt>/weblog/new</tt>. In these cases, the page caching mechanism will add one in
54 53
         # order to make it easy for the cached files to be picked up properly by the web server. By default, this cache extension is <tt>.html</tt>.
55 54
         # If you want something else, like <tt>.php</tt> or <tt>.shtml</tt>, just set Base.page_cache_extension. In cases where a request already has an
56 55
         # extension, such as <tt>.xml</tt> or <tt>.rss</tt>, page caching will not add an extension. This allows it to work well with RESTful apps.
  56
+        @@page_cache_extension = '.html'
57 57
         cattr_accessor :page_cache_extension
58 58
       end
59 59
 
2  actionpack/lib/action_controller/metal/compatibility.rb
@@ -32,8 +32,6 @@ class << self
32 32
       def rescue_action(env)
33 33
         raise env["action_dispatch.rescue.exception"]
34 34
       end
35  
-
36  
-      self.page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : ""
37 35
     end
38 36
 
39 37
     # For old tests
6  actionpack/lib/action_controller/railtie.rb
@@ -44,6 +44,12 @@ class Railtie < Rails::Railtie
44 44
       ActiveSupport.on_load(:action_controller) { self.logger ||= Rails.logger }
45 45
     end
46 46
 
  47
+    initializer "action_controller.page_cache_directory" do
  48
+      ActiveSupport.on_load(:action_controller) do
  49
+        self.page_cache_directory = Rails.public_path
  50
+      end
  51
+    end
  52
+
47 53
     initializer "action_controller.set_configs" do |app|
48 54
       paths = app.config.paths
49 55
       ac = app.config.action_controller
5  railties/lib/rails.rb
@@ -92,11 +92,12 @@ def version
92 92
     end
93 93
 
94 94
     def public_path
95  
-      @@public_path ||= self.root ? File.join(self.root, "public") : "public"
  95
+      application && application.paths.public.to_a.first
96 96
     end
97 97
 
98 98
     def public_path=(path)
99  
-      @@public_path = path
  99
+      ActiveSupport::Deprecation.warn "Setting Rails.public_path= is deprecated. " <<
  100
+        "Please set paths.public = in config/application.rb instead.", caller
100 101
     end
101 102
   end
102 103
 end
2  railties/lib/rails/application/configuration.rb
@@ -137,7 +137,7 @@ def session_options
137 137
 
138 138
       def default_middleware_stack
139 139
         ActionDispatch::MiddlewareStack.new.tap do |middleware|
140  
-          middleware.use('::ActionDispatch::Static', lambda { Rails.public_path }, :if => lambda { serve_static_assets })
  140
+          middleware.use('::ActionDispatch::Static', lambda { paths.public.to_a.first }, :if => lambda { serve_static_assets })
141 141
           middleware.use('::Rack::Lock', :if => lambda { !allow_concurrency })
142 142
           middleware.use('::Rack::Runtime')
143 143
           middleware.use('::Rails::Rack::Logger')
9  railties/test/application/configuration_test.rb
@@ -184,6 +184,15 @@ def teardown
184 184
       end
185 185
     end
186 186
 
  187
+    test "config.paths.public sets Rails.public_path" do
  188
+      add_to_config <<-RUBY
  189
+        config.paths.public = "somewhere"
  190
+      RUBY
  191
+
  192
+      require "#{app_path}/config/application"
  193
+      assert_equal File.join(app_path, "somewhere"), Rails.public_path
  194
+    end
  195
+
187 196
     def make_basic_app
188 197
       require "rails"
189 198
       require "action_controller/railtie"

0 notes on commit 4e92134

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