Skip to content

Commit

Permalink
first draft of jammit_debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jan 20, 2011
1 parent bcbe8fe commit 0a67473
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
15 changes: 15 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ <h2 id="configuration">Configuration</h2>
to compress your JavaScript.
</td>
</tr>
<tr>
<td><b>allow_debugging</b></td>
<td><tt>on&nbsp;|&nbsp;off</tt></td>
<td class="definition">
Defaults to <b>on</b>. Leaving it on allows you to pass
<tt>jammit_debug=true</tt> as a query parameter, in order to load the
page with uncompressed, unpackaged assets in production.
</td>
</tr>
<tr>
<td><b>template_function</b></td>
<td><tt>on&nbsp;|&nbsp;off&nbsp;|&nbsp;...</tt></td>
Expand Down Expand Up @@ -349,6 +358,12 @@ <h2 id="usage">Usage</h2>
In all other environments, or if <b>package_assets</b> is set to "<b>always</b>",
you'll get tags for the merged packages.
</p>

<p>
If <b>allow_debugging</b> is left on, you'll be able to request the development
version of the assets for any given page, by passing <tt>jammit_debug=true</tt>
as a query parameter.
</p>

<p>
You can easily use Jammit within your Rakefile, and other scripts:
Expand Down
3 changes: 2 additions & 1 deletion lib/jammit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class << self
:embed_assets, :package_assets, :compress_assets, :gzip_assets,
:package_path, :mhtml_enabled, :include_jst_script, :config_path,
:javascript_compressor, :compressor_options, :css_compressor_options,
:template_extension, :template_extension_matcher
:template_extension, :template_extension_matcher, :allow_debugging
end

# The minimal required configuration.
Expand All @@ -70,6 +70,7 @@ def self.load_configuration(config_path, soft=false)
@embed_assets = conf[:embed_assets] || conf[:embed_images]
@compress_assets = !(conf[:compress_assets] == false)
@gzip_assets = !(conf[:gzip_assets] == false)
@allow_debugging = !(conf[:allow_debugging] == false)
@mhtml_enabled = @embed_assets && @embed_assets != "datauri"
@compressor_options = symbolize_keys(conf[:compressor_options] || {})
@css_compressor_options = symbolize_keys(conf[:css_compressor_options] || {})
Expand Down
8 changes: 6 additions & 2 deletions lib/jammit/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Helper
# compressed CSS, and in development the stylesheet URLs are passed verbatim.
def include_stylesheets(*packages)
options = packages.extract_options!
return individual_stylesheets(packages, options) unless Jammit.package_assets
return individual_stylesheets(packages, options) unless should_package?
disabled = (options.delete(:embed_assets) == false) || (options.delete(:embed_images) == false)
return html_safe(packaged_stylesheets(packages, options)) if disabled || !Jammit.embed_assets
return html_safe(embedded_image_stylesheets(packages, options))
Expand All @@ -26,7 +26,7 @@ def include_stylesheets(*packages)
# except in development, where it references the individual scripts.
def include_javascripts(*packages)
tags = packages.map do |pack|
Jammit.package_assets ? Jammit.asset_url(pack, :js) : Jammit.packager.individual_urls(pack.to_sym, :js)
should_package? ? Jammit.asset_url(pack, :js) : Jammit.packager.individual_urls(pack.to_sym, :js)
end
html_safe(javascript_include_tag(tags.flatten))
end
Expand All @@ -40,6 +40,10 @@ def include_templates(*packages)

private

def should_package?
Jammit.package_assets && !(Jammit.allow_debugging && params[:jammit_debug])
end

def html_safe(string)
string.respond_to?(:html_safe) ? string.html_safe : string
end
Expand Down
13 changes: 12 additions & 1 deletion test/unit/test_jammit_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def initialize(*args)
end
end

def params
@debug ? {:jammit_debug => true} : {}
end

def test_include_stylesheets
assert include_stylesheets(:css_test) == File.read('test/fixtures/tags/css_includes.html')
end
Expand All @@ -51,7 +55,7 @@ def test_include_templates_with_diff_ext
assert include_javascripts(:jst_test_diff_ext) == '<script src="/assets/jst_test_diff_ext.js?101" type="text/javascript"></script>'
end

def test_individual_assets_in_development_do
def test_individual_assets_in_development
Jammit.instance_variable_set(:@package_assets, false)
assert include_stylesheets(:css_test) == File.read('test/fixtures/tags/css_individual_includes.html')
assert include_javascripts(:js_test_with_templates) == File.read('test/fixtures/tags/js_individual_includes.html')
Expand All @@ -64,4 +68,11 @@ def test_individual_assets_in_development_do
Jammit.reload!
end

def test_individual_assets_while_debugging
@debug = true
assert include_stylesheets(:css_test) == File.read('test/fixtures/tags/css_individual_includes.html')
assert include_javascripts(:js_test_with_templates) == File.read('test/fixtures/tags/js_individual_includes.html')
@debug = false
end

end

0 comments on commit 0a67473

Please sign in to comment.