Skip to content

Instantly share code, notes, and snippets.

@tulios
Created March 17, 2010 14:05
Show Gist options
  • Save tulios/335256 to your computer and use it in GitHub Desktop.
Save tulios/335256 to your computer and use it in GitHub Desktop.
path_to_resource
module ApplicationHelper
def path_to_resource(source)
generate_path(source)
end
private
def generate_path(source)
has_request = @controller.respond_to?(:request)
unless source =~ %r{^[-a-z]+://}
source = "/#{source}" unless source[0] == ?/
source = rewrite_asset_path(source)
if has_request
unless source =~ %r{^#{ActionController::Base.relative_url_root}/}
source = "#{ActionController::Base.relative_url_root}#{source}"
end
end
end
if source !~ %r{^[-a-z]+://}
host = compute_asset_host(source)
if has_request && !host.blank? && host !~ %r{^[-a-z]+://}
host = "#{@controller.request.protocol}#{host}"
end
"#{host}#{source}"
else
source
end
end
def compute_asset_host(source)
if host = ActionController::Base.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment