Skip to content

Commit

Permalink
Added Rails.public_path to control where HTML and assets are expected…
Browse files Browse the repository at this point in the history
… to be loaded from (defaults to Rails.root + "/public") #11581 [nicksieger]
  • Loading branch information
dhh committed Apr 13, 2008
1 parent be7fd81 commit 420c4b3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_controller/caching/pages.rb
Expand Up @@ -36,7 +36,7 @@ module Caching
# == Setting the cache directory
#
# The cache directory should be the document root for the web server and is set using Base.page_cache_directory = "/document/root".
# For Rails, this directory has already been set to RAILS_ROOT + "/public".
# For Rails, this directory has already been set to Rails.public_path (which is usually set to RAILS_ROOT + "/public").
#
# == Setting the cache extension
#
Expand All @@ -46,7 +46,7 @@ module Pages
def self.included(base) #:nodoc:
base.extend(ClassMethods)
base.class_eval do
@@page_cache_directory = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/public" : ""
@@page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : ""
cattr_accessor :page_cache_directory

@@page_cache_extension = '.html'
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/dispatcher.rb
Expand Up @@ -67,7 +67,7 @@ def failsafe_logger
end

cattr_accessor :error_file_path
self.error_file_path = "#{::RAILS_ROOT}/public" if defined? ::RAILS_ROOT
self.error_file_path = Rails.public_path if defined?(Rails.public_path)

cattr_accessor :unprepared
self.unprepared = true
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/rescue.rb
Expand Up @@ -153,7 +153,7 @@ def rescue_action_in_public(exception) #:doc:
# If the file doesn't exist, the body of the response will be left empty.
def render_optional_error_file(status_code)
status = interpret_status(status_code)
path = "#{RAILS_ROOT}/public/#{status[0,3]}.html"
path = "#{Rails.public_path}/#{status[0,3]}.html"
if File.exist?(path)
render :file => path, :status => status
else
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -101,7 +101,7 @@ module Helpers #:nodoc:
# something like Live HTTP Headers for Firefox to verify that the cache is indeed working (and that the assets are not being
# requested over and over).
module AssetTagHelper
ASSETS_DIR = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/public" : "public"
ASSETS_DIR = defined?(Rails.public_path) ? Rails.public_path : "public"
JAVASCRIPTS_DIR = "#{ASSETS_DIR}/javascripts"
STYLESHEETS_DIR = "#{ASSETS_DIR}/stylesheets"

Expand Down
24 changes: 20 additions & 4 deletions actionpack/test/controller/rescue_test.rb
Expand Up @@ -305,15 +305,19 @@ def test_clean_backtrace

def test_not_implemented
with_all_requests_local false do
head :not_implemented
with_rails_public_path(".") do
head :not_implemented
end
end
assert_response :not_implemented
assert_equal "GET, PUT", @response.headers['Allow']
end

def test_method_not_allowed
with_all_requests_local false do
get :method_not_allowed
with_rails_public_path(".") do
get :method_not_allowed
end
end
assert_response :method_not_allowed
assert_equal "GET, HEAD, PUT", @response.headers['Allow']
Expand Down Expand Up @@ -391,15 +395,27 @@ def with_remote_addr(addr)
@request.remote_addr = old_remote_addr
end

def with_rails_root(path = nil)
def with_rails_public_path(rails_root)
old_rails = Object.const_get(:Rails) rescue nil
mod = Object.const_set(:Rails, Module.new)
(class << mod; self; end).instance_eval do
define_method(:public_path) { "#{rails_root}/public" }
end
yield
ensure
Object.module_eval { remove_const(:Rails) } if defined?(Rails)
Object.const_set(:Rails, old_rails) if old_rails
end

def with_rails_root(path = nil,&block)
old_rails_root = RAILS_ROOT if defined?(RAILS_ROOT)
if path
silence_warnings { Object.const_set(:RAILS_ROOT, path) }
else
Object.remove_const(:RAILS_ROOT) rescue nil
end

yield
with_rails_public_path(path, &block)

ensure
if old_rails_root
Expand Down
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Added Rails.public_path to control where HTML and assets are expected to be loaded from (defaults to Rails.root + "/public") #11581 [nicksieger]

* rake time:zones:local finds correct base utc offset for zones in the Southern Hemisphere [Geoff Buesing]

* Don't require rails/gem_builder during rails initialization, it's only needed for the gems:build task. [rick]
Expand Down
8 changes: 8 additions & 0 deletions railties/lib/initializer.rb
Expand Up @@ -38,6 +38,14 @@ def env
def cache
RAILS_CACHE
end

def public_path
@@public_path ||= File.join(self.root, "public")
end

def public_path=(path)
@@public_path = path
end
end

# The Initializer is responsible for processing the Rails configuration, such
Expand Down

0 comments on commit 420c4b3

Please sign in to comment.