aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2011-05-22 15:10:53 -0500
committerJoshua Peek <josh@joshpeek.com>2011-05-22 15:10:53 -0500
commit27159dff0fd0d3f8a6faececc35fffe55a5f3032 (patch)
tree44415114d32755f66acf5baccf292a4de5e654ea
parentcdc9c3c4af6211bcb80cc00fdb4be46b674f6154 (diff)
downloadrails-27159dff0fd0d3f8a6faececc35fffe55a5f3032.tar.gz
rails-27159dff0fd0d3f8a6faececc35fffe55a5f3032.tar.bz2
rails-27159dff0fd0d3f8a6faececc35fffe55a5f3032.zip
Add debug_asset support
-rw-r--r--actionpack/lib/action_view/helpers/sprockets_helper.rb62
-rw-r--r--actionpack/test/fixtures/sprockets/app/javascripts/application.js1
-rw-r--r--actionpack/test/fixtures/sprockets/app/stylesheets/application.css1
-rw-r--r--actionpack/test/template/sprockets_helper_test.rb11
4 files changed, 58 insertions, 17 deletions
diff --git a/actionpack/lib/action_view/helpers/sprockets_helper.rb b/actionpack/lib/action_view/helpers/sprockets_helper.rb
index ab98da9624..e5d780225b 100644
--- a/actionpack/lib/action_view/helpers/sprockets_helper.rb
+++ b/actionpack/lib/action_view/helpers/sprockets_helper.rb
@@ -4,28 +4,53 @@ require 'action_view/helpers/asset_paths'
module ActionView
module Helpers
module SprocketsHelper
- def asset_path(source, default_ext = nil)
- sprockets_asset_paths.compute_public_path(source, 'assets', default_ext, true)
+ def debug_assets?
+ params[:debug_assets] == '1' ||
+ 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 = sprockets_asset_paths.compute_public_path(source, 'assets', default_ext, true)
+ body ? "#{path}?body=1" : path
end
def sprockets_javascript_include_tag(source, options = {})
- options = {
- 'type' => "text/javascript",
- 'src' => asset_path(source, 'js')
- }.merge(options.stringify_keys)
+ debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
+ body = options.key?(:body) ? options.delete(:body) : false
- content_tag 'script', "", options
+ if debug && asset = sprockets_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 = {})
- options = {
- 'rel' => "stylesheet",
- 'type' => "text/css",
- 'media' => "screen",
- 'href' => asset_path(source, 'css')
- }.merge(options.stringify_keys)
+ debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
+ body = options.key?(:body) ? options.delete(:body) : false
- tag 'link', options
+ if debug && asset = sprockets_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
private
@@ -39,6 +64,13 @@ module ActionView
end
class AssetPaths < ActionView::Helpers::AssetPaths #:nodoc:
+ def asset_for(source, ext)
+ source = source.to_s
+ return nil if is_uri?(source)
+ source = rewrite_extension(source, nil, ext)
+ assets[source]
+ end
+
def rewrite_asset_path(source, dir)
if source[0] == ?/
source
@@ -66,4 +98,4 @@ module ActionView
end
end
end
-end \ No newline at end of file
+end
diff --git a/actionpack/test/fixtures/sprockets/app/javascripts/application.js b/actionpack/test/fixtures/sprockets/app/javascripts/application.js
index e69de29bb2..e611d2b129 100644
--- a/actionpack/test/fixtures/sprockets/app/javascripts/application.js
+++ b/actionpack/test/fixtures/sprockets/app/javascripts/application.js
@@ -0,0 +1 @@
+//= require xmlhr
diff --git a/actionpack/test/fixtures/sprockets/app/stylesheets/application.css b/actionpack/test/fixtures/sprockets/app/stylesheets/application.css
index e69de29bb2..2365eaa4cd 100644
--- a/actionpack/test/fixtures/sprockets/app/stylesheets/application.css
+++ b/actionpack/test/fixtures/sprockets/app/stylesheets/application.css
@@ -0,0 +1 @@
+/*= require style */
diff --git a/actionpack/test/template/sprockets_helper_test.rb b/actionpack/test/template/sprockets_helper_test.rb
index 8d3be09a4f..4ce8c672cd 100644
--- a/actionpack/test/template/sprockets_helper_test.rb
+++ b/actionpack/test/template/sprockets_helper_test.rb
@@ -13,6 +13,7 @@ class SprocketsHelperTest < ActionView::TestCase
super
@controller = BasicController.new
+ @controller.stubs(:params).returns({})
@request = Class.new do
def protocol() 'http://' end
@@ -85,10 +86,13 @@ class SprocketsHelperTest < ActionView::TestCase
sprockets_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")
+
+ 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)
end
test "stylesheet path" do
- assert_equal "/assets/application-d41d8cd98f00b204e9800998ecf8427e.css", asset_path(:application, "css")
+ assert_equal "/assets/application-68b329da9893e34099c7d8ad5cb9c940.css", asset_path(:application, "css")
assert_equal "/assets/style-d41d8cd98f00b204e9800998ecf8427e.css", asset_path("style", "css")
assert_equal "/assets/dir/style-d41d8cd98f00b204e9800998ecf8427e.css", asset_path("dir/style.css", "css")
@@ -101,7 +105,7 @@ class SprocketsHelperTest < ActionView::TestCase
end
test "stylesheet link tag" do
- assert_equal '<link href="/assets/application-d41d8cd98f00b204e9800998ecf8427e.css" media="screen" rel="stylesheet" type="text/css" />',
+ assert_equal '<link href="/assets/application-68b329da9893e34099c7d8ad5cb9c940.css" media="screen" rel="stylesheet" type="text/css" />',
sprockets_stylesheet_link_tag(:application)
assert_equal '<link href="/assets/style-d41d8cd98f00b204e9800998ecf8427e.css" media="screen" rel="stylesheet" type="text/css" />',
@@ -115,5 +119,8 @@ class SprocketsHelperTest < ActionView::TestCase
sprockets_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")
+
+ 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)
end
end