aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
diff options
context:
space:
mode:
authorJosh Kalderimis <josh.kalderimis@gmail.com>2010-11-15 18:20:37 +0100
committerJosé Valim <jose.valim@gmail.com>2010-11-16 00:04:37 +0100
commitce1f87673c17338617222a4e4e0971cfb9f0655f (patch)
treeeca4e638cb16ba2d3d3e1fb41e56b730a7ea3cf9 /actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
parent0ff1c5935f84f86a8b88802078b115e9964c2249 (diff)
downloadrails-ce1f87673c17338617222a4e4e0971cfb9f0655f.tar.gz
rails-ce1f87673c17338617222a4e4e0971cfb9f0655f.tar.bz2
rails-ce1f87673c17338617222a4e4e0971cfb9f0655f.zip
corrected the AV railtie to use the new home for cache_asset_timestamps, and merged asset id caching and asset paths together.
Diffstat (limited to 'actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb')
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb49
1 files changed, 46 insertions, 3 deletions
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb b/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
index 5f884e391a..7a00c8e69d 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb
@@ -1,12 +1,17 @@
require 'active_support/core_ext/file'
-require 'action_view/helpers/asset_tag_helpers/asset_id_caching'
module ActionView
module Helpers
module AssetTagHelper
class AssetPaths
- include AssetIdCaching
+ # You can enable or disable the asset tag timestamps cache.
+ # With the cache enabled, the asset tag helper methods will make fewer
+ # expensive file system calls. However this prevents you from modifying
+ # any asset files while the server is running.
+ #
+ # ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
+ mattr_accessor :cache_asset_timestamps
attr_reader :config, :controller
@@ -36,6 +41,12 @@ module ActionView
source
end
+ def add_to_asset_timestamp_cache(source, asset_id)
+ self.asset_timestamps_cache_guard.synchronize do
+ self.asset_timestamps_cache[source] = asset_id
+ end
+ end
+
def is_uri?(path)
path =~ %r{^[-a-z]+://|^cid:}
end
@@ -62,8 +73,40 @@ module ActionView
return path.call(source)
elsif path && path.is_a?(String)
return path % [source]
+ end
+
+ asset_id = rails_asset_id(source)
+ if asset_id.empty?
+ source
+ else
+ "#{source}?#{asset_id}"
+ end
+ end
+
+ mattr_accessor :asset_timestamps_cache
+ self.asset_timestamps_cache = {}
+
+ mattr_accessor :asset_timestamps_cache_guard
+ self.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
- handle_asset_id(source)
+ if self.cache_asset_timestamps && (asset_id = self.asset_timestamps_cache[source])
+ asset_id
+ else
+ path = File.join(config.assets_dir, source)
+ asset_id = File.exist?(path) ? File.mtime(path).to_i.to_s : ''
+
+ if self.cache_asset_timestamps
+ add_to_asset_timestamp_cache(source, asset_id)
+ end
+
+ asset_id
+ end
end
end