1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/lib/action_view/helpers/sprockets_helper.rb

69 lines
1.9 KiB
Ruby
Raw Normal View History

2011-03-28 16:19:34 -04:00
require 'uri'
require 'action_view/helpers/asset_paths'
2011-03-28 16:19:34 -04:00
module ActionView
module Helpers
module SprocketsHelper
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)
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",
'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",
'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)
end
2011-04-19 15:14:55 -04:00
end
2011-03-28 16:19:34 -04:00
class AssetPaths < ActionView::Helpers::AssetPaths #:nodoc:
2011-04-19 15:14:55 -04:00
def rewrite_asset_path(source, dir)
2011-05-03 06:42:42 -04:00
if source[0] == ?/
2011-04-19 15:14:55 -04:00
source
2011-05-03 06:42:42 -04:00
else
assets.path(source, performing_caching?, dir)
2011-03-28 16:19:34 -04:00
end
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
def performing_caching?
2011-04-19 15:14:55 -04:00
@config ? @config.perform_caching : Rails.application.config.action_controller.perform_caching
end
2011-04-19 15:14:55 -04:00
end
2011-03-28 16:19:34 -04:00
end
end
end