1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added automated timestamping to AssetTagHelper methods for stylesheets, 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
This commit is contained in:
David Heinemeier Hansson 2006-03-29 20:57:53 +00:00
parent 23fa0395d8
commit 91383e0426
4 changed files with 45 additions and 5 deletions

View file

@ -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]

View file

@ -59,11 +59,16 @@ module ActionView
# <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))
@ -146,11 +151,17 @@ module ActionView
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 << ".#{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 not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View file

@ -27,6 +27,11 @@ class AssetTagHelperTest < Test::Unit::TestCase
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" />),
@ -115,6 +120,17 @@ class AssetTagHelperTest < Test::Unit::TestCase
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