module ActionView::Helpers::AssetUrlHelper
def asset_path(source, options = {})
asset_path "application", type: :stylesheet # => /assets/application.css
asset_path "application", type: :javascript # => /assets/application.js
asset_path "application.js" # => /assets/application.js
All other asset *_path helpers delegate through this method.
to the corresponding public directory.
options is set, a file extension will be appended and scoped
Computes the path to asset in public directory. If :type
def asset_path(source, options = {}) raise ArgumentError, "nil is not a valid asset source" if source.nil? source = source.to_s return "" unless source.present? return source if source =~ URI_REGEXP tail, source = source[/([\?#].+)$/], source.sub(/([\?#].+)$/, ''.freeze) if extname = compute_asset_extname(source, options) source = "#{source}#{extname}" end if source[0] != ?/ source = compute_asset_path(source, options) end relative_url_root = defined?(config.relative_url_root) && config.relative_url_root if relative_url_root source = File.join(relative_url_root, source) unless source.starts_with?("#{relative_url_root}/") end if host = compute_asset_host(source, options) source = File.join(host, source) end "#{source}#{tail}" end
def asset_url(source, options = {})
asset_url "application.js", host: "http://cdn.example.com" # => http://cdn.example.com/assets/application.js
asset_url "application.js" # => http://example.com/assets/application.js
All other options provided are forwarded to +asset_path+ call.
+config.action_controller.asset_host+ setting.
will be the same. If :host options is set, it overwrites global
will use +asset_path+ internally, so most of their behaviors
Computes the full URL to an asset in the public directory. This
def asset_url(source, options = {}) path_to_asset(source, options.merge(:protocol => :request)) end
def audio_path(source, options = {})
audio_path("/sounds/horse.wav") # => /sounds/horse.wav
audio_path("sounds/horse.wav") # => /audios/sounds/horse.wav
audio_path("horse.wav") # => /audios/horse.wav
audio_path("horse") # => /audios/horse
Used internally by +audio_tag+ to build the audio path.
Full paths from the document root will be passed through.
Computes the path to an audio asset in the public audios directory.
def audio_path(source, options = {}) path_to_asset(source, {type: :audio}.merge!(options)) end
def audio_url(source, options = {})
audio_url "horse.wav", host: "http://stage.example.com" # => http://stage.example.com/horse.wav
options is set, it overwrites global +config.action_controller.asset_host+ setting.
Since +audio_url+ is based on +asset_url+ method you can set :host options. If :host
This will use +audio_path+ internally, so most of their behaviors will be the same.
Computes the full URL to an audio asset in the public audios directory.
def audio_url(source, options = {}) url_to_asset(source, {type: :audio}.merge!(options)) end
def compute_asset_extname(source, options = {})
Compute extname to append to asset path. Returns nil if
def compute_asset_extname(source, options = {}) return if options[:extname] == false extname = options[:extname] || ASSET_EXTENSIONS[options[:type]] extname if extname && File.extname(source) != extname end
def compute_asset_host(source = "", options = {})
or the value returned from invoking call on an object responding to call
numbers 0-3 if it contains %d (the number is the source hash mod 4),
the host if no wildcard is set, the host interpolated with the
Pick an asset host for this source. Returns +nil+ if no host is set,
def compute_asset_host(source = "", options = {}) request = self.request if respond_to?(:request) host = options[:host] host ||= config.asset_host if defined? config.asset_host if host.respond_to?(:call) arity = host.respond_to?(:arity) ? host.arity : host.method(:call).arity args = [source] args << request if request && (arity > 1 || arity < 0) host = host.call(*args) elsif host =~ /%d/ host = host % (Zlib.crc32(source) % 4) end host ||= request.base_url if request && options[:protocol] == :request return unless host if host =~ URI_REGEXP host else protocol = options[:protocol] || config.default_asset_host_protocol || (request ? :request : :relative) case protocol when :relative "//#{host}" when :request "#{request.protocol}#{host}" else "#{protocol}://#{host}" end end end
def compute_asset_path(source, options = {})
extensions can override this method to point to custom assets
Computes asset path to public directory. Plugins and
def compute_asset_path(source, options = {}) dir = ASSET_PUBLIC_DIRECTORIES[options[:type]] || "" File.join(dir, source) end
def font_path(source, options = {})
font_path("/dir/font.ttf") # => /dir/font.ttf
font_path("dir/font.ttf") # => /fonts/dir/font.ttf
font_path("font.ttf") # => /fonts/font.ttf
font_path("font") # => /fonts/font
Full paths from the document root will be passed through.
Computes the path to a font asset.
def font_path(source, options = {}) path_to_asset(source, {type: :font}.merge!(options)) end
def font_url(source, options = {})
font_url "font.ttf", host: "http://stage.example.com" # => http://stage.example.com/font.ttf
options is set, it overwrites global +config.action_controller.asset_host+ setting.
Since +font_url+ is based on +asset_url+ method you can set :host options. If :host
This will use +font_path+ internally, so most of their behaviors will be the same.
Computes the full URL to a font asset.
def font_url(source, options = {}) url_to_asset(source, {type: :font}.merge!(options)) end
def image_path(source, options = {})
The alias +path_to_image+ is provided to avoid that. Rails uses the alias internally, and
If you have images as application resources this method may conflict with their named routes.
image_path("http://www.example.com/img/edit.png") # => "http://www.example.com/img/edit.png"
image_path("/icons/edit.png") # => "/icons/edit.png"
image_path("icons/edit.png") # => "/assets/icons/edit.png"
image_path("edit.png") # => "/assets/edit.png"
image_path("edit") # => "/assets/edit"
Used internally by +image_tag+ to build the image path:
Full paths from the document root will be passed through.
Computes the path to an image asset.
def image_path(source, options = {}) path_to_asset(source, {type: :image}.merge!(options)) end
def image_url(source, options = {})
image_url "edit.png", host: "http://stage.example.com" # => http://stage.example.com/edit.png
options is set, it overwrites global +config.action_controller.asset_host+ setting.
Since +image_url+ is based on +asset_url+ method you can set :host options. If :host
This will use +image_path+ internally, so most of their behaviors will be the same.
Computes the full URL to an image asset.
def image_url(source, options = {}) url_to_asset(source, {type: :image}.merge!(options)) end
def javascript_path(source, options = {})
javascript_path "http://www.example.com/js/xmlhr" # => http://www.example.com/js/xmlhr
javascript_path "/dir/xmlhr" # => /dir/xmlhr.js
javascript_path "dir/xmlhr.js" # => /assets/dir/xmlhr.js
javascript_path "xmlhr" # => /assets/xmlhr.js
Used internally by +javascript_include_tag+ to build the script path.
Full paths from the document root will be passed through.
If the +source+ filename has no extension, .js will be appended (except for explicit URIs)
Computes the path to a JavaScript asset in the public javascripts directory.
def javascript_path(source, options = {}) path_to_asset(source, {type: :javascript}.merge!(options)) end
def javascript_url(source, options = {})
javascript_url "js/xmlhr.js", host: "http://stage.example.com" # => http://stage.example.com/assets/dir/xmlhr.js
options is set, it overwrites global +config.action_controller.asset_host+ setting.
Since +javascript_url+ is based on +asset_url+ method you can set :host options. If :host
This will use +javascript_path+ internally, so most of their behaviors will be the same.
Computes the full URL to a JavaScript asset in the public javascripts directory.
def javascript_url(source, options = {}) url_to_asset(source, {type: :javascript}.merge!(options)) end
def stylesheet_path(source, options = {})
stylesheet_path "http://www.example.com/css/style" # => http://www.example.com/css/style
stylesheet_path "/dir/style.css" # => /dir/style.css
stylesheet_path "dir/style.css" # => /assets/dir/style.css
stylesheet_path "style" # => /assets/style.css
Used internally by +stylesheet_link_tag+ to build the stylesheet path.
Full paths from the document root will be passed through.
If the +source+ filename has no extension, .css will be appended (except for explicit URIs).
Computes the path to a stylesheet asset in the public stylesheets directory.
def stylesheet_path(source, options = {}) path_to_asset(source, {type: :stylesheet}.merge!(options)) end
def stylesheet_url(source, options = {})
stylesheet_url "css/style.css", host: "http://stage.example.com" # => http://stage.example.com/css/style.css
options is set, it overwrites global +config.action_controller.asset_host+ setting.
Since +stylesheet_url+ is based on +asset_url+ method you can set :host options. If :host
This will use +stylesheet_path+ internally, so most of their behaviors will be the same.
Computes the full URL to a stylesheet asset in the public stylesheets directory.
def stylesheet_url(source, options = {}) url_to_asset(source, {type: :stylesheet}.merge!(options)) end
def video_path(source, options = {})
video_path("/trailers/hd.avi") # => /trailers/hd.avi
video_path("trailers/hd.avi") # => /videos/trailers/hd.avi
video_path("hd.avi") # => /videos/hd.avi
video_path("hd") # => /videos/hd
Used internally by +video_tag+ to build the video path.
Full paths from the document root will be passed through.
Computes the path to a video asset in the public videos directory.
def video_path(source, options = {}) path_to_asset(source, {type: :video}.merge!(options)) end
def video_url(source, options = {})
video_url "hd.avi", host: "http://stage.example.com" # => http://stage.example.com/hd.avi
options is set, it overwrites global +config.action_controller.asset_host+ setting.
Since +video_url+ is based on +asset_url+ method you can set :host options. If :host
This will use +video_path+ internally, so most of their behaviors will be the same.
Computes the full URL to a video asset in the public videos directory.
def video_url(source, options = {}) url_to_asset(source, {type: :video}.merge!(options)) end