From 91383e04265f41c3a8ae9411b05384b7ef2a36c1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 29 Mar 2006 20:57:53 +0000 Subject: 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 --- actionpack/CHANGELOG | 13 +++++++++++++ .../lib/action_view/helpers/asset_tag_helper.rb | 21 ++++++++++++++++----- actionpack/test/fixtures/public/images/rails.png | Bin 0 -> 1787 bytes actionpack/test/template/asset_tag_helper_test.rb | 16 ++++++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 actionpack/test/fixtures/public/images/rails.png (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 82294a9299..7b3b44f7cb 100644 --- a/actionpack/CHANGELOG +++ b/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") # => 'Rails' + + ...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") # => 'Rails' + + 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] diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index f05d9a096b..de44ef6531 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -59,11 +59,16 @@ module ActionView # controllers/application.rb and helpers/application_helper.rb. 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)) @@ -145,12 +150,18 @@ 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 = "#{@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 diff --git a/actionpack/test/fixtures/public/images/rails.png b/actionpack/test/fixtures/public/images/rails.png new file mode 100644 index 0000000000..b8441f182e Binary files /dev/null and b/actionpack/test/fixtures/public/images/rails.png differ diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index bc7e3e0f7a..b3e062f4c8 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -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) => %(), %(auto_discovery_link_tag(:atom)) => %(), @@ -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 %(Rails), 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 %(Rails), image_tag("rails.png") + end end class AssetTagHelperNonVhostTest < Test::Unit::TestCase -- cgit v1.2.3