reorganised the the common asset helpers module into a class and have it include the id caching module, this class is now shared from the view instance to the asset include tag helpers (js and css)

This commit is contained in:
Josh Kalderimis 2010-11-15 17:30:15 +01:00 committed by José Valim
parent 6a609dbc82
commit 0ff1c5935f
6 changed files with 128 additions and 125 deletions

@ -1,6 +1,6 @@
require 'action_view/helpers/asset_tag_helpers/javascript_tag_helpers'
require 'action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers'
require 'action_view/helpers/asset_tag_helpers/asset_id_caching'
require 'action_view/helpers/asset_tag_helpers/asset_paths'
module ActionView
# = Action View Asset Tag Helpers
@ -191,8 +191,6 @@ module Helpers #:nodoc:
# RewriteEngine On
# RewriteRule ^/release-\d+/(images|javascripts|stylesheets)/(.*)$ /$1/$2 [L]
module AssetTagHelper
include CommonAssetHelpers
include AssetIdCaching
include JavascriptTagHelpers
include StylesheetTagHelpers
# Returns a link tag that browsers and news readers can use to auto-detect
@ -276,7 +274,7 @@ def favicon_link_tag(source='/favicon.ico', options={})
# The alias +path_to_image+ is provided to avoid that. Rails uses the alias internally, and
# plugin authors are encouraged to do so.
def image_path(source)
compute_public_path(source, 'images')
asset_paths.compute_public_path(source, 'images')
end
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
@ -291,7 +289,7 @@ def image_path(source)
# video_path("/trailers/hd.avi") # => /trailers/hd.avi
# video_path("http://www.railsapplication.com/vid/hd.avi") # => http://www.railsapplication.com/vid/hd.avi
def video_path(source)
compute_public_path(source, 'videos')
asset_paths.compute_public_path(source, 'videos')
end
alias_method :path_to_video, :video_path # aliased to avoid conflicts with a video_path named route
@ -306,7 +304,7 @@ def video_path(source)
# audio_path("/sounds/horse.wav") # => /sounds/horse.wav
# audio_path("http://www.railsapplication.com/sounds/horse.wav") # => http://www.railsapplication.com/sounds/horse.wav
def audio_path(source)
compute_public_path(source, 'audios')
asset_paths.compute_public_path(source, 'audios')
end
alias_method :path_to_audio, :audio_path # aliased to avoid conflicts with an audio_path named route
@ -433,6 +431,11 @@ def audio_tag(source, options = {})
tag("audio", options)
end
private
def asset_paths
@asset_paths ||= AssetPaths.new(config, controller)
end
end
end
end

@ -2,7 +2,6 @@
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/common_asset_helpers'
require 'action_view/helpers/asset_tag_helpers/asset_id_caching'
@ -11,17 +10,14 @@ module Helpers
module AssetTagHelper
class AssetIncludeTag
include CommonAssetHelpers
include AssetIdCaching
attr_reader :config, :controller
attr_reader :config, :asset_paths
class_attribute :expansions
self.expansions = { }
def initialize(config, controller)
def initialize(config, asset_paths)
@config = config
@controller = controller
@asset_paths = asset_paths
end
def asset_name
@ -64,11 +60,11 @@ def include_tag(*sources)
private
def path_to_asset(source)
compute_public_path(source, asset_name.to_s.pluralize, extension)
asset_paths.compute_public_path(source, asset_name.to_s.pluralize, extension)
end
def compute_paths(*args)
expand_sources(*args).collect { |source| compute_public_path(source, asset_name.pluralize, extension, false) }
expand_sources(*args).collect { |source| asset_paths.compute_public_path(source, asset_name.pluralize, extension, false) }
end
def expand_sources(sources, recursive)
@ -83,7 +79,7 @@ def expand_sources(sources, recursive)
def ensure_sources!(sources)
sources.each do |source|
asset_file_path!(compute_public_path(source, asset_name.pluralize, extension))
asset_file_path!(path_to_asset(source))
end
return sources
end
@ -124,7 +120,7 @@ def asset_file_path(path)
end
def asset_file_path!(path, error_if_file_is_uri = false)
if is_uri?(path)
if asset_paths.is_uri?(path)
raise(Errno::ENOENT, "Asset file #{path} is uri and cannot be merged into single file") if error_if_file_is_uri
else
absolute_path = asset_file_path(path)

@ -0,0 +1,106 @@
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
attr_reader :config, :controller
def initialize(config, controller)
@config = config
@controller = controller
end
# Add the the extension +ext+ if not present. Return full URLs otherwise untouched.
# Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
# 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, dir, ext = nil, include_host = true)
return source if is_uri?(source)
source = rewrite_extension(source, dir, ext) if ext
source = "/#{dir}/#{source}" unless source[0] == ?/
if controller.respond_to?(:env) && controller.env["action_dispatch.asset_path"]
source = rewrite_asset_path(source, controller.env["action_dispatch.asset_path"])
end
source = rewrite_asset_path(source, config.asset_path)
has_request = controller.respond_to?(:request)
source = rewrite_relative_url_root(source, controller.config.relative_url_root) if has_request && include_host
source = rewrite_host_and_protocol(source, has_request) if include_host
source
end
def is_uri?(path)
path =~ %r{^[-a-z]+://|^cid:}
end
private
def rewrite_extension(source, dir, ext)
source_ext = File.extname(source)
source_with_ext = if source_ext.empty?
"#{source}.#{ext}"
elsif ext != source_ext[1..-1]
with_ext = "#{source}.#{ext}"
with_ext if File.exist?(File.join(config.assets_dir, dir, with_ext))
end
source_with_ext || source
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, path = nil)
if path && path.respond_to?(:call)
return path.call(source)
elsif path && path.is_a?(String)
return path % [source]
else
handle_asset_id(source)
end
end
def rewrite_relative_url_root(source, relative_url_root)
relative_url_root && !source.starts_with?("#{relative_url_root}/") ? "#{relative_url_root}#{source}" : source
end
def rewrite_host_and_protocol(source, has_request)
host = compute_asset_host(source)
if has_request && host && !is_uri?(host)
host = "#{controller.request.protocol}#{host}"
end
"#{host}#{source}"
end
# 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 from
# invoking call if it's an object responding to call.
def compute_asset_host(source)
if host = config.asset_host
if host.is_a?(Proc) || host.respond_to?(:call)
case host.is_a?(Proc) ? host.arity : host.method(:call).arity
when 2
request = controller.respond_to?(:request) && controller.request
host.call(source, request)
else
host.call(source)
end
else
(host =~ /%d/) ? host % (source.hash % 4) : host
end
end
end
end
end
end
end

@ -1,100 +0,0 @@
require 'active_support/core_ext/file'
module ActionView
module Helpers
module AssetTagHelper
module CommonAssetHelpers
private
# Add the the extension +ext+ if not present. Return full URLs otherwise untouched.
# Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
# 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, dir, ext = nil, include_host = true)
return source if is_uri?(source)
source = rewrite_extension(source, dir, ext) if ext
source = "/#{dir}/#{source}" unless source[0] == ?/
if controller.respond_to?(:env) && controller.env["action_dispatch.asset_path"]
source = rewrite_asset_path(source, controller.env["action_dispatch.asset_path"])
end
source = rewrite_asset_path(source, config.asset_path)
has_request = controller.respond_to?(:request)
source = rewrite_relative_url_root(source, controller.config.relative_url_root) if has_request && include_host
source = rewrite_host_and_protocol(source, has_request) if include_host
source
end
def is_uri?(path)
path =~ %r{^[-a-z]+://|^cid:}
end
def rewrite_extension(source, dir, ext)
source_ext = File.extname(source)
source_with_ext = if source_ext.empty?
"#{source}.#{ext}"
elsif ext != source_ext[1..-1]
with_ext = "#{source}.#{ext}"
with_ext if File.exist?(File.join(config.assets_dir, dir, with_ext))
end
source_with_ext || source
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, path = nil)
if path && path.respond_to?(:call)
return path.call(source)
elsif path && path.is_a?(String)
return path % [source]
else
handle_asset_id(source)
end
end
# This is the default implementation
def handle_asset_id(source)
source
end
def rewrite_relative_url_root(source, relative_url_root)
relative_url_root && !source.starts_with?("#{relative_url_root}/") ? "#{relative_url_root}#{source}" : source
end
def rewrite_host_and_protocol(source, has_request)
host = compute_asset_host(source)
if has_request && host && !is_uri?(host)
host = "#{controller.request.protocol}#{host}"
end
"#{host}#{source}"
end
# 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 from
# invoking call if it's an object responding to call.
def compute_asset_host(source)
if host = config.asset_host
if host.is_a?(Proc) || host.respond_to?(:call)
case host.is_a?(Proc) ? host.arity : host.method(:call).arity
when 2
request = controller.respond_to?(:request) && controller.request
host.call(source, request)
else
host.call(source)
end
else
(host =~ /%d/) ? host % (source.hash % 4) : host
end
end
end
end
end
end
end

@ -1,7 +1,6 @@
require 'active_support/concern'
require 'active_support/core_ext/file'
require 'action_view/helpers/tag_helper'
require 'action_view/helpers/asset_tag_helpers/common_asset_helpers'
require 'action_view/helpers/asset_tag_helpers/asset_include_tag'
module ActionView
@ -43,9 +42,9 @@ def expand_sources(sources, recursive = false)
end
end
module JavascriptTagHelpers
extend ActiveSupport::Concern
include CommonAssetHelpers
module ClassMethods
# Register one or more javascript files to be included when <tt>symbol</tt>
@ -76,7 +75,7 @@ def register_javascript_expansion(expansions)
# javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr
# javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js
def javascript_path(source)
compute_public_path(source, 'javascripts', 'js')
asset_paths.compute_public_path(source, 'javascripts', 'js')
end
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route
@ -163,7 +162,7 @@ def javascript_path(source)
#
# javascript_include_tag :all, :cache => true, :recursive => true
def javascript_include_tag(*sources)
@javascript_include ||= JavascriptIncludeTag.new(config, controller)
@javascript_include ||= JavascriptIncludeTag.new(config, asset_paths)
@javascript_include.include_tag(*sources)
end

@ -1,7 +1,6 @@
require 'active_support/concern'
require 'active_support/core_ext/file'
require 'action_view/helpers/tag_helper'
require 'action_view/helpers/asset_tag_helpers/common_asset_helpers'
require 'action_view/helpers/asset_tag_helpers/asset_include_tag'
module ActionView
@ -28,9 +27,9 @@ def custom_dir
end
end
module StylesheetTagHelpers
extend ActiveSupport::Concern
include CommonAssetHelpers
module ClassMethods
# Register one or more stylesheet files to be included when <tt>symbol</tt>
@ -61,7 +60,7 @@ def register_stylesheet_expansion(expansions)
# stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style
# stylesheet_path "http://www.railsapplication.com/css/style.css" # => http://www.railsapplication.com/css/style.css
def stylesheet_path(source)
compute_public_path(source, 'stylesheets', 'css')
asset_paths.compute_public_path(source, 'stylesheets', 'css')
end
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route
@ -134,7 +133,7 @@ def stylesheet_path(source)
# stylesheet_link_tag :all, :concat => true
#
def stylesheet_link_tag(*sources)
@stylesheet_include ||= StylesheetIncludeTag.new(config, controller)
@stylesheet_include ||= StylesheetIncludeTag.new(config, asset_paths)
@stylesheet_include.include_tag(*sources)
end