Skip to content

Commit

Permalink
Updated asset_template_path to asset_path and have it also support a …
Browse files Browse the repository at this point in the history
…String [#4247 state:resolved]
  • Loading branch information
wycats committed Mar 29, 2010
1 parent 49bc6a2 commit 201e898
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
21 changes: 11 additions & 10 deletions actionpack/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -108,7 +108,7 @@ module Helpers #:nodoc:
# "http://asset%d.example.com", "https://asset1.example.com"
# )
#
# === Using asset path templates
# === Customizing the asset path
#
# By default, Rails appends asset's timestamps to all asset paths. This allows
# you to set a cache-expiration date for the asset far into the future, but
Expand Down Expand Up @@ -144,7 +144,7 @@ module Helpers #:nodoc:
# The easiest is to define the RAILS_ASSET_ID environment variable. The
# contents of this variable will always be used in preference to
# calculated timestamps. A more complex but flexible way is to set
# <tt>ActionController::Base.config.asset_path_template</tt> to a proc
# <tt>ActionController::Base.config.asset_path</tt> to a proc
# that takes the unmodified asset path and returns the path needed for
# your asset caching to work. Typically you'd do something like this in
# <tt>config/environments/production.rb</tt>:
Expand All @@ -163,7 +163,7 @@ module Helpers #:nodoc:
# stylesheet_link_tag("application")
# # => <link href="/release-12345/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
#
# Changing the asset_path_template does require that your web servers have
# Changing the asset_path does require that your web servers have
# knowledge of the asset template paths that you rewrite to so it's not
# suitable for out-of-the-box use. To use the example given above you
# could use something like this in your Apache VirtualHost configuration:
Expand Down Expand Up @@ -705,12 +705,7 @@ def compute_public_path(source, dir, ext = nil, include_host = true)

source += ".#{ext}" if rewrite_extension?(source, dir, ext)
source = "/#{dir}/#{source}" unless source[0] == ?/
asset_path_template = config.asset_path_template
if asset_path_template && asset_path_template.respond_to?(:call)
source = asset_path_template.call(source)
else
source = rewrite_asset_path(source)
end
source = rewrite_asset_path(source, config.asset_path)

has_request = controller.respond_to?(:request)
if has_request && include_host && source !~ %r{^#{controller.config.relative_url_root}/}
Expand Down Expand Up @@ -774,7 +769,13 @@ def rails_asset_id(source)

# Break out the asset path rewrite in case plugins wish to put the asset id
# someplace other than the query string.
def rewrite_asset_path(source)
def rewrite_asset_path(source, path = nil)
if path && path.respond_to?(:call)
return path.call(source)
elsif path && path.is_a?(String)
return path % [source]
end

asset_id = rails_asset_id(source)
if asset_id.blank?
source
Expand Down
9 changes: 8 additions & 1 deletion actionpack/test/template/asset_tag_helper_test.rb
Expand Up @@ -373,8 +373,15 @@ def test_timebased_asset_id
assert_equal %(<img alt="Rails" src="/images/rails.png?#{expected_time}" />), image_tag("rails.png")
end

def test_string_asset_id
@controller.config.asset_path = "/assets.v12345%s"

expected_path = "/assets.v12345/images/rails.png"
assert_equal %(<img alt="Rails" src="#{expected_path}" />), image_tag("rails.png")
end

def test_proc_asset_id
@controller.config.asset_path_template = Proc.new do |asset_path|
@controller.config.asset_path = Proc.new do |asset_path|
"/assets.v12345#{asset_path}"
end

Expand Down
2 changes: 1 addition & 1 deletion railties/guides/source/configuring.textile
Expand Up @@ -133,7 +133,7 @@ h4. Configuring Action Controller

* +config.action_controller.asset_host+ provides a string that is prepended to all of the URL-generating helpers in +AssetHelper+. This is designed to allow moving all javascript, CSS, and image files to a separate asset host.

* +config.action_controller.asset_template_path+ allows you to override the default asset path generation by providing your own instructions.
* +config.action_controller.asset_path+ allows you to override the default asset path generation by providing your own instructions.

* +config.action_controller.consider_all_requests_local+ is generally set to +true+ during development and +false+ during production; if it is set to +true+, then any error will cause detailed debugging information to be dumped in the HTTP response. For finer-grained control, set this to +false+ and implement +local_request?+ to specify which requests should provide debugging information on errors.

Expand Down

0 comments on commit 201e898

Please sign in to comment.