Skip to content

Commit

Permalink
Allow multiple sources in Sprockets helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
guilleiguaran committed Jun 5, 2011
1 parent 75e5610 commit db8eeaf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
62 changes: 34 additions & 28 deletions actionpack/lib/sprockets/helpers/rails_helper.rb
Expand Up @@ -15,42 +15,48 @@ def asset_paths
end
end

def javascript_include_tag(source, options = {})
def javascript_include_tag(*sources)
options = sources.extract_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
sources.collect do |source|
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
tag_options = {
'type' => "text/javascript",
'src' => asset_path(source, 'js', body)
}.merge(options.stringify_keys)

content_tag 'script', "", tag_options
end
end.join("\n").html_safe
end

def stylesheet_link_tag(source, options = {})
def stylesheet_link_tag(*sources)
options = sources.extract_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
sources.collect do |source|
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
tag_options = {
'rel' => "stylesheet",
'type' => "text/css",
'media' => "screen",

This comment has been minimized.

Copy link
@miguelperez

miguelperez Jun 6, 2011

would not be nice to be able to pass the media options of each of the css as well?

'href' => asset_path(source, 'css', body)
}.merge(options.stringify_keys)

tag 'link', tag_options
end
end.join("\n").html_safe
end

private
Expand Down
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions actionpack/test/template/sprockets_helper_test.rb
Expand Up @@ -94,6 +94,9 @@ def url_for(*args)

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>",
javascript_include_tag(:application, :debug => true)

assert_equal "<script src=\"/assets/xmlhr-d41d8cd98f00b204e9800998ecf8427e.js\" type=\"text/javascript\"></script>\n<script src=\"/assets/extra-d41d8cd98f00b204e9800998ecf8427e.js\" type=\"text/javascript\"></script>",
javascript_include_tag("xmlhr", "extra")
end

test "stylesheet path" do
Expand Down Expand Up @@ -127,5 +130,8 @@ def url_for(*args)

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\" />",
stylesheet_link_tag(:application, :debug => true)

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

0 comments on commit db8eeaf

Please sign in to comment.