Added automated timestamping to AssetTagHelper methods for stylesheets,...

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
上级 23fa0395
*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]
......
......@@ -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))
......@@ -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
......@@ -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" />),
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册