Skip to content

Commit

Permalink
Added automated timestamping to AssetTagHelper methods for stylesheet…
Browse files Browse the repository at this point in the history
…s, javascripts, and images when Action Controller is run under Rails [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4098 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Mar 29, 2006
1 parent 23fa039 commit 91383e0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
13 changes: 13 additions & 0 deletions actionpack/CHANGELOG
@@ -1,3 +1,16 @@
*SVN*

* Added automated timestamping to AssetTagHelper methods for stylesheets, javascripts, and images when Action Controller is run under Rails [DHH]. Example:

image_tag("rails.png") # => '<img alt="Rails" src="/images/rails.png?1143664135" />'

...to avoid frequent stats (not a problem for most people), you can set RAILS_ASSET_ID in the ENV to avoid stats:

ENV["RAILS_ASSET_ID"] = "2345"
image_tag("rails.png") # => '<img alt="Rails" src="/images/rails.png?2345" />'

This can be used by deployment managers to set the asset id by application revision

*1.12.0* (March 27th, 2005)

* Add documentation for respond_to. [Jamis Buck]
Expand Down
21 changes: 16 additions & 5 deletions actionpack/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -59,11 +59,16 @@ def javascript_path(source)
# <tt>controllers/application.rb</tt> and <tt>helpers/application_helper.rb</tt>.
def javascript_include_tag(*sources)
options = sources.last.is_a?(Hash) ? sources.pop.stringify_keys : { }

if sources.include?(:defaults)
sources = sources[0..(sources.index(:defaults))] + @@javascript_default_sources.dup + sources[(sources.index(:defaults) + 1)..sources.length]
sources = sources[0..(sources.index(:defaults))] +
@@javascript_default_sources.dup +
sources[(sources.index(:defaults) + 1)..sources.length]

sources.delete(:defaults)
sources << "application" if defined?(RAILS_ROOT) and File.exists?("#{RAILS_ROOT}/public/javascripts/application.js")
sources << "application" if defined?(RAILS_ROOT) && File.exists?("#{RAILS_ROOT}/public/javascripts/application.js")
end

sources.collect { |source|
source = javascript_path(source)
content_tag("script", "", { "type" => "text/javascript", "src" => source }.merge(options))
Expand Down Expand Up @@ -145,12 +150,18 @@ def image_tag(source, options = {})

private
def compute_public_path(source, dir, ext)
source = "/#{dir}/#{source}" unless source.first == "/" || source.include?(":")
source = "#{source}.#{ext}" unless source.split("/").last.include?(".")
source = "#{@controller.request.relative_url_root}#{source}" unless %r{^[-a-z]+://} =~ source
source = "/#{dir}/#{source}" unless source.first == "/" || source.include?(":")
source << ".#{ext}" unless source.split("/").last.include?(".")
source << '?' + rails_asset_id(source) if defined?(RAILS_ROOT)
source = "#{@controller.request.relative_url_root}#{source}" unless %r{^[-a-z]+://} =~ source
source = ActionController::Base.asset_host + source unless source.include?(":")
source
end

def rails_asset_id(source)
ENV["RAILS_ASSET_ID"] ||
File.stat(RAILS_ROOT + "/public/#{source}").mtime.to_i.to_s
end
end
end
end
Binary file added actionpack/test/fixtures/public/images/rails.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions actionpack/test/template/asset_tag_helper_test.rb
Expand Up @@ -27,6 +27,11 @@ def relative_url_root
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
end

def teardown
Object.send(:remove_const, :RAILS_ROOT) if defined?(RAILS_ROOT)
ENV["RAILS_ASSET_ID"] = nil
end

AutoDiscoveryToTag = {
%(auto_discovery_link_tag) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
%(auto_discovery_link_tag(:atom)) => %(<link href="http://www.example.com" rel="alternate" title="ATOM" type="application/atom+xml" />),
Expand Down Expand Up @@ -115,6 +120,17 @@ def test_image_tag
ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end

def test_timebased_asset_id
Object.send(:const_set, :RAILS_ROOT, File.dirname(__FILE__) + "/../fixtures/")
expected_time = File.stat(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).mtime.to_i.to_s
assert_equal %(<img alt="Rails" src="/images/rails.png?#{expected_time}" />), image_tag("rails.png")
end

def test_preset_asset_id
Object.send(:const_set, :RAILS_ROOT, File.dirname(__FILE__) + "/../fixtures/")
ENV["RAILS_ASSET_ID"] = "4500"
assert_equal %(<img alt="Rails" src="/images/rails.png?4500" />), image_tag("rails.png")
end
end

class AssetTagHelperNonVhostTest < Test::Unit::TestCase
Expand Down

0 comments on commit 91383e0

Please sign in to comment.