Skip to content

Commit

Permalink
Move javascript/css helpers into the Sprockets helper and just have t…
Browse files Browse the repository at this point in the history
…hem override the old behavior
  • Loading branch information
wycats committed May 23, 2011
1 parent 4b79029 commit 76f947e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 55 deletions.
43 changes: 0 additions & 43 deletions actionpack/lib/action_view/helpers/sprockets_helper.rb
Expand Up @@ -9,49 +9,6 @@ def debug_assets?
params[:debug_assets] == 'true'
end

def asset_path(source, default_ext = nil, body = false)
source = source.logical_path if source.respond_to?(:logical_path)
path = asset_paths.compute_public_path(source, 'assets', default_ext, true)
body ? "#{path}?body=1" : path
end

def sprockets_javascript_include_tag(source, options = {})
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false

if debug && asset = asset_paths.asset_for(source, 'js')
asset.to_a.map { |dep|
sprockets_javascript_include_tag(dep, :debug => false, :body => true)
}.join("\n").html_safe
else
options = {
'type' => "text/javascript",
'src' => asset_path(source, 'js', body)
}.merge(options.stringify_keys)

content_tag 'script', "", options
end
end

def sprockets_stylesheet_link_tag(source, options = {})
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false

if debug && asset = asset_paths.asset_for(source, 'css')
asset.to_a.map { |dep|
sprockets_stylesheet_link_tag(dep, :debug => false, :body => true)
}.join("\n").html_safe
else
options = {
'rel' => "stylesheet",
'type' => "text/css",
'media' => "screen",
'href' => asset_path(source, 'css', body)
}.merge(options.stringify_keys)

tag 'link', options
end
end
end
end
end
44 changes: 44 additions & 0 deletions actionpack/lib/sprockets/helpers/rails_helper.rb
Expand Up @@ -9,6 +9,50 @@ def asset_paths
end
end

def asset_path(source, default_ext = nil, body = false)
source = source.logical_path if source.respond_to?(:logical_path)
path = asset_paths.compute_public_path(source, 'assets', default_ext, true)
body ? "#{path}?body=1" : path
end

def javascript_include_tag(source, options = {})
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false

if debug && asset = asset_paths.asset_for(source, 'js')
asset.to_a.map { |dep|
javascript_include_tag(dep, :debug => false, :body => true)
}.join("\n").html_safe
else
options = {
'type' => "text/javascript",
'src' => asset_path(source, 'js', body)
}.merge(options.stringify_keys)

content_tag 'script', "", options
end
end

def stylesheet_link_tag(source, options = {})
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false

if debug && asset = asset_paths.asset_for(source, 'css')
asset.to_a.map { |dep|
stylesheet_link_tag(dep, :debug => false, :body => true)
}.join("\n").html_safe
else
options = {
'rel' => "stylesheet",
'type' => "text/css",
'media' => "screen",
'href' => asset_path(source, 'css', body)
}.merge(options.stringify_keys)

tag 'link', options
end
end

class AssetPaths < ActionView::Helpers::AssetPaths #:nodoc:
def compute_public_path(source, dir, ext=nil, include_host=true)
super(source, 'assets', ext, include_host)
Expand Down
24 changes: 12 additions & 12 deletions actionpack/test/template/sprockets_helper_test.rb
Expand Up @@ -78,17 +78,17 @@ def url_for(*args)

test "javascript include tag" do
assert_equal '<script src="/assets/application-d41d8cd98f00b204e9800998ecf8427e.js" type="text/javascript"></script>',
sprockets_javascript_include_tag(:application)
javascript_include_tag(:application)

assert_equal '<script src="/assets/xmlhr-d41d8cd98f00b204e9800998ecf8427e.js" type="text/javascript"></script>',
sprockets_javascript_include_tag("xmlhr")
javascript_include_tag("xmlhr")
assert_equal '<script src="/assets/xmlhr-d41d8cd98f00b204e9800998ecf8427e.js" type="text/javascript"></script>',
sprockets_javascript_include_tag("xmlhr.js")
javascript_include_tag("xmlhr.js")
assert_equal '<script src="http://www.example.com/xmlhr" type="text/javascript"></script>',
sprockets_javascript_include_tag("http://www.example.com/xmlhr")
javascript_include_tag("http://www.example.com/xmlhr")

assert_equal "<script src=\"/assets/xmlhr-d41d8cd98f00b204e9800998ecf8427e.js?body=1\" type=\"text/javascript\"></script>\n<script src=\"/assets/application-d41d8cd98f00b204e9800998ecf8427e.js?body=1\" type=\"text/javascript\"></script>",
sprockets_javascript_include_tag(:application, :debug => true)
javascript_include_tag(:application, :debug => true)
end

test "stylesheet path" do
Expand All @@ -106,21 +106,21 @@ def url_for(*args)

test "stylesheet link tag" do
assert_equal '<link href="/assets/application-68b329da9893e34099c7d8ad5cb9c940.css" media="screen" rel="stylesheet" type="text/css" />',
sprockets_stylesheet_link_tag(:application)
stylesheet_link_tag(:application)

assert_equal '<link href="/assets/style-d41d8cd98f00b204e9800998ecf8427e.css" media="screen" rel="stylesheet" type="text/css" />',
sprockets_stylesheet_link_tag("style")
stylesheet_link_tag("style")
assert_equal '<link href="/assets/style-d41d8cd98f00b204e9800998ecf8427e.css" media="screen" rel="stylesheet" type="text/css" />',
sprockets_stylesheet_link_tag("style.css")
stylesheet_link_tag("style.css")

assert_equal '<link href="http://www.example.com/style.css" media="screen" rel="stylesheet" type="text/css" />',
sprockets_stylesheet_link_tag("http://www.example.com/style.css")
stylesheet_link_tag("http://www.example.com/style.css")
assert_equal '<link href="/assets/style-d41d8cd98f00b204e9800998ecf8427e.css" media="all" rel="stylesheet" type="text/css" />',
sprockets_stylesheet_link_tag("style", :media => "all")
stylesheet_link_tag("style", :media => "all")
assert_equal '<link href="/assets/style-d41d8cd98f00b204e9800998ecf8427e.css" media="print" rel="stylesheet" type="text/css" />',
sprockets_stylesheet_link_tag("style", :media => "print")
stylesheet_link_tag("style", :media => "print")

assert_equal "<link href=\"/assets/style-d41d8cd98f00b204e9800998ecf8427e.css?body=1\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />\n<link href=\"/assets/application-68b329da9893e34099c7d8ad5cb9c940.css?body=1\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />",
sprockets_stylesheet_link_tag(:application, :debug => true)
stylesheet_link_tag(:application, :debug => true)
end
end

0 comments on commit 76f947e

Please sign in to comment.