aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/asset_tag_helper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_view/helpers/asset_tag_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb80
1 files changed, 47 insertions, 33 deletions
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index 8bbe74b7ef..0633d5414e 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -80,6 +80,12 @@ module ActionView
# end
# }
#
+ # You can also implement a custom asset host object that responds to the call method and tasks one or two parameters just like the proc.
+ #
+ # config.action_controller.asset_host = AssetHostingWithMinimumSsl.new(
+ # "http://asset%d.example.com", "https://asset1.example.com"
+ # )
+ #
# === Using asset timestamps
#
# By default, Rails will append all asset paths with that asset's timestamp. This allows you to set a cache-expiration date for the
@@ -151,7 +157,7 @@ module ActionView
# javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr.js
# javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js
def javascript_path(source)
- JavaScriptTag.create(self, @controller, source).public_path
+ JavaScriptTag.new(self, @controller, source).public_path
end
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route
@@ -314,7 +320,7 @@ module ActionView
# stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style.css
# stylesheet_path "http://www.railsapplication.com/css/style.js" # => http://www.railsapplication.com/css/style.css
def stylesheet_path(source)
- StylesheetTag.create(self, @controller, source).public_path
+ StylesheetTag.new(self, @controller, source).public_path
end
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route
@@ -359,6 +365,7 @@ module ActionView
# compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching
# is set to true (which is the case by default for the Rails production environment, but not for the development
# environment). Examples:
+
#
# ==== Examples
# stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
@@ -411,7 +418,7 @@ module ActionView
# image_path("/icons/edit.png") # => /icons/edit.png
# image_path("http://www.railsapplication.com/img/edit.png") # => http://www.railsapplication.com/img/edit.png
def image_path(source)
- ImageTag.create(self, @controller, source).public_path
+ ImageTag.new(self, @controller, source).public_path
end
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
@@ -527,20 +534,6 @@ module ActionView
Cache = {}
CacheGuard = Mutex.new
- def self.create(template, controller, source, include_host = true)
- CacheGuard.synchronize do
- key = if controller.respond_to?(:request)
- [self, controller.request.protocol,
- ActionController::Base.asset_host,
- ActionController::Base.relative_url_root,
- source, include_host]
- else
- [self, ActionController::Base.asset_host, source, include_host]
- end
- Cache[key] ||= new(template, controller, source, include_host).freeze
- end
- end
-
ProtocolRegexp = %r{^[-a-z]+://}.freeze
def initialize(template, controller, source, include_host = true)
@@ -551,8 +544,16 @@ module ActionView
@controller = controller
@source = source
@include_host = include_host
+ @cache_key = if controller.respond_to?(:request)
+ [self.class.name,controller.request.protocol,
+ ActionController::Base.asset_host,
+ ActionController::Base.relative_url_root,
+ source, include_host]
+ else
+ [self.class.name,ActionController::Base.asset_host, source, include_host]
+ end
end
-
+
def public_path
compute_public_path(@source)
end
@@ -573,7 +574,7 @@ module ActionView
private
def request
- @controller.request
+ request? && @controller.request
end
def request?
@@ -585,20 +586,32 @@ module ActionView
# roots. Rewrite the asset path for cache-busting asset ids. Include
# asset host, if configured, with the correct request protocol.
def compute_public_path(source)
- source += ".#{extension}" if missing_extension?(source)
- unless source =~ ProtocolRegexp
- source = "/#{directory}/#{source}" unless source[0] == ?/
- source = rewrite_asset_path(source)
- source = prepend_relative_url_root(source)
+ if source =~ ProtocolRegexp
+ source += ".#{extension}" if missing_extension?(source)
+ source = prepend_asset_host(source)
+ source
+ else
+ CacheGuard.synchronize do
+ Cache[@cache_key + [source]] ||= begin
+ source += ".#{extension}" if missing_extension?(source) || file_exists_with_extension?(source)
+ source = "/#{directory}/#{source}" unless source[0] == ?/
+ source = rewrite_asset_path(source)
+ source = prepend_relative_url_root(source)
+ source = prepend_asset_host(source)
+ source
+ end
+ end
end
- source = prepend_asset_host(source)
- source
end
-
+
def missing_extension?(source)
- extension && (File.extname(source).blank? || File.exist?(File.join(ASSETS_DIR, directory, "#{source}.#{extension}")))
+ extension && File.extname(source).blank?
end
-
+
+ def file_exists_with_extension?(source)
+ extension && File.exist?(File.join(ASSETS_DIR, directory, "#{source}.#{extension}"))
+ end
+
def prepend_relative_url_root(source)
relative_url_root = ActionController::Base.relative_url_root
if request? && @include_host && source !~ %r{^#{relative_url_root}/}
@@ -623,11 +636,12 @@ module ActionView
# Pick an asset host for this source. Returns +nil+ if no host is set,
# the host if no wildcard is set, the host interpolated with the
# numbers 0-3 if it contains <tt>%d</tt> (the number is the source hash mod 4),
- # or the value returned from invoking the proc if it's a proc.
+ # or the value returned from invoking the proc if it's a proc or the value from
+ # invoking call if it's an object responding to call.
def compute_asset_host(source)
if host = ActionController::Base.asset_host
- if host.is_a?(Proc)
- case host.arity
+ if host.is_a?(Proc) || host.respond_to?(:call)
+ case host.is_a?(Proc) ? host.arity : host.method(:call).arity
when 2
host.call(source, request)
else
@@ -735,7 +749,7 @@ module ActionView
end
def tag_sources
- expand_sources.collect { |source| tag_class.create(@template, @controller, source, false) }
+ expand_sources.collect { |source| tag_class.new(@template, @controller, source, false) }
end
def joined_contents