2011-03-28 16:19:34 -04:00
|
|
|
require 'uri'
|
2011-04-19 16:01:25 -04:00
|
|
|
require 'action_view/helpers/asset_paths'
|
2011-03-28 16:19:34 -04:00
|
|
|
|
|
|
|
module ActionView
|
|
|
|
module Helpers
|
|
|
|
module SprocketsHelper
|
2011-04-19 13:05:07 -04:00
|
|
|
def asset_path(source, default_ext = nil)
|
2011-04-19 15:14:55 -04:00
|
|
|
sprockets_asset_paths.compute_public_path(source, 'assets', default_ext, true)
|
2011-04-19 12:07:14 -04:00
|
|
|
end
|
|
|
|
|
2011-03-28 16:19:34 -04:00
|
|
|
def sprockets_javascript_include_tag(source, options = {})
|
|
|
|
options = {
|
2011-03-29 16:42:31 -04:00
|
|
|
'type' => "text/javascript",
|
2011-04-19 13:05:07 -04:00
|
|
|
'src' => asset_path(source, 'js')
|
2011-03-28 16:19:34 -04:00
|
|
|
}.merge(options.stringify_keys)
|
|
|
|
|
|
|
|
content_tag 'script', "", options
|
|
|
|
end
|
|
|
|
|
|
|
|
def sprockets_stylesheet_link_tag(source, options = {})
|
|
|
|
options = {
|
|
|
|
'rel' => "stylesheet",
|
|
|
|
'type' => "text/css",
|
|
|
|
'media' => "screen",
|
2011-04-19 13:05:07 -04:00
|
|
|
'href' => asset_path(source, 'css')
|
2011-03-28 16:19:34 -04:00
|
|
|
}.merge(options.stringify_keys)
|
|
|
|
|
|
|
|
tag 'link', options
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2011-04-19 15:14:55 -04:00
|
|
|
def sprockets_asset_paths
|
|
|
|
@sprockets_asset_paths ||= begin
|
|
|
|
config = self.config if respond_to?(:config)
|
|
|
|
controller = self.controller if respond_to?(:controller)
|
|
|
|
SprocketsHelper::AssetPaths.new(config, controller)
|
2011-04-19 12:29:18 -04:00
|
|
|
end
|
2011-04-19 15:14:55 -04:00
|
|
|
end
|
2011-03-28 16:19:34 -04:00
|
|
|
|
2011-04-19 16:01:25 -04:00
|
|
|
class AssetPaths < ActionView::Helpers::AssetPaths #:nodoc:
|
2011-04-19 15:14:55 -04:00
|
|
|
def rewrite_asset_path(source, dir)
|
|
|
|
if source =~ /^\/#{dir}\/(.+)/
|
|
|
|
assets.path($1, performing_caching?, dir)
|
|
|
|
else
|
|
|
|
source
|
2011-03-28 16:19:34 -04:00
|
|
|
end
|
2011-04-19 12:29:18 -04:00
|
|
|
end
|
2011-03-28 16:19:34 -04:00
|
|
|
|
2011-04-19 15:14:55 -04:00
|
|
|
def rewrite_extension(source, dir, ext)
|
|
|
|
if ext && File.extname(source).empty?
|
|
|
|
"#{source}.#{ext}"
|
|
|
|
else
|
|
|
|
source
|
2011-03-28 16:19:34 -04:00
|
|
|
end
|
|
|
|
end
|
2011-03-29 16:42:31 -04:00
|
|
|
|
|
|
|
def assets
|
|
|
|
Rails.application.assets
|
|
|
|
end
|
2011-04-19 15:14:55 -04:00
|
|
|
|
|
|
|
# When included in Sprockets::Context, we need to ask the top-level config as the controller is not available
|
2011-04-19 13:05:07 -04:00
|
|
|
def performing_caching?
|
2011-04-19 15:14:55 -04:00
|
|
|
@config ? @config.perform_caching : Rails.application.config.action_controller.perform_caching
|
2011-04-19 13:05:07 -04:00
|
|
|
end
|
2011-04-19 15:14:55 -04:00
|
|
|
end
|
2011-03-28 16:19:34 -04:00
|
|
|
end
|
|
|
|
end
|
2011-04-19 15:49:28 -04:00
|
|
|
end
|