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

corrected the AV railtie to use the new home for cache_asset_timestamps, and merged asset id caching and asset paths together.

This commit is contained in:
Josh Kalderimis 2010-11-15 18:20:37 +01:00 committed by José Valim
parent 0ff1c5935f
commit ce1f87673c
4 changed files with 47 additions and 75 deletions

View file

@ -1,69 +0,0 @@
require 'thread'
require 'active_support/core_ext/file'
require 'active_support/concern'
module ActionView
module Helpers
module AssetTagHelper
module AssetIdCaching
extend ActiveSupport::Concern
included do
# 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
private
mattr_accessor :asset_timestamps_cache
self.asset_timestamps_cache = {}
mattr_accessor :asset_timestamps_cache_guard
self.asset_timestamps_cache_guard = Mutex.new
end
private
# 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 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
self.asset_timestamps_cache_guard.synchronize do
self.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.
# This is the default implementation
def handle_asset_id(source)
asset_id = rails_asset_id(source)
if asset_id.empty?
source
else
"#{source}?#{asset_id}"
end
end
end
end
end
end

View file

@ -2,8 +2,6 @@ require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/file'
require 'action_view/helpers/tag_helper'
require 'action_view/helpers/asset_tag_helpers/asset_id_caching'
module ActionView
module Helpers

View file

@ -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
handle_asset_id(source)
"#{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
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

View file

@ -11,7 +11,7 @@ module ActionView
initializer "action_view.cache_asset_timestamps" do |app|
unless app.config.cache_classes
ActiveSupport.on_load(:action_view) do
ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
ActionView::Helpers::AssetTagHelper::AssetPaths.cache_asset_timestamps = false
end
end
end