Skip to content

Instantly share code, notes, and snippets.

@tka
Created April 20, 2010 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tka/372217 to your computer and use it in GitHub Desktop.
Save tka/372217 to your computer and use it in GitHub Desktop.
@@asset_timestamps_cache = {}
@@asset_timestamps_cache_guard = Mutex.new
# Use the RAILS_ASSET_ID environment variable or the source's
# modification time as its cache-busting asset id.
def rails_asset_id(source)
if asset_id = ENV["RAILS_ASSET_ID"]
asset_id
else
if @@cache_asset_timestamps && (asset_id = @@asset_timestamps_cache[source])
asset_id
else
path = File.join(ASSETS_DIR, source)
asset_id = File.exist?(path) ? File.mtime(path).to_i.to_s : ''
if @@cache_asset_timestamps
@@asset_timestamps_cache_guard.synchronize do
@@asset_timestamps_cache[source] = asset_id
end
end
asset_id
end
end
end
# 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)
asset_id = rails_asset_id(source)
if asset_id.blank?
source
else
source + "?#{asset_id}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment