From 0855f041df5a46c2f3fd0820864e4139eb37a6c4 Mon Sep 17 00:00:00 2001 From: Nathan Stitt Date: Thu, 1 Aug 2013 13:10:36 -0500 Subject: [PATCH] Add "extname" option to javascript_include_tag ActionView::Helpers.asset_path is where the logic for javascript_include_tag resides. It takes an extname option for specifying the extension or false to not append it. This exposes that option to javascript_include_tag. Without the option files that didn't end with ".js" would get the extension appended to them. This broke JST templates and other file types that should be interpreted as JavaScript but who's file extension isn't ".js" --- actionview/CHANGELOG.md | 14 ++++++++++++++ .../lib/action_view/helpers/asset_tag_helper.rb | 9 ++++++--- actionview/test/template/javascript_helper_test.rb | 7 +++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index adbe8ac389ca5..98b78a7114ad1 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,17 @@ +* Added an `extname` hash option for `javascript_include_tag` method. + + Before: + + javascript_include_tag('templates.jst') + # => + + After: + + javascript_include_tag('templates.jst', extname: false ) + # => + + *Nathan Stitt* + * Fix `current_page?` when the URL contains escaped characters and the original URL is using the hexadecimal lowercased. diff --git a/actionview/lib/action_view/helpers/asset_tag_helper.rb b/actionview/lib/action_view/helpers/asset_tag_helper.rb index 2b3a3c6a29341..a13d0021eaf26 100644 --- a/actionview/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionview/lib/action_view/helpers/asset_tag_helper.rb @@ -26,7 +26,8 @@ module AssetTagHelper # to assets/javascripts, full paths are assumed to be relative to the document # root. Relative paths are idiomatic, use absolute paths only when needed. # - # When passing paths, the ".js" extension is optional. + # When passing paths, the ".js" extension is optional. If you do not want ".js" + # appended to the path extname: false can be set on the options. # # You can modify the HTML attributes of the script tag by passing a hash as the # last argument. @@ -37,6 +38,9 @@ module AssetTagHelper # javascript_include_tag "xmlhr" # # => # + # javascript_include_tag "template.jst", extname: false + # # => + # # javascript_include_tag "xmlhr.js" # # => # @@ -51,8 +55,7 @@ module AssetTagHelper # # => def javascript_include_tag(*sources) options = sources.extract_options!.stringify_keys - path_options = options.extract!('protocol').symbolize_keys - + path_options = options.extract!('protocol', 'extname').symbolize_keys sources.uniq.map { |source| tag_options = { "src" => path_to_javascript(source, path_options) diff --git a/actionview/test/template/javascript_helper_test.rb b/actionview/test/template/javascript_helper_test.rb index de6a6eaab3f49..4703111741b39 100644 --- a/actionview/test/template/javascript_helper_test.rb +++ b/actionview/test/template/javascript_helper_test.rb @@ -51,6 +51,13 @@ def test_javascript_tag assert_equal 'foo', output_buffer, 'javascript_tag without a block should not concat to output_buffer' end + # Setting the :extname option will control what extension (if any) is appended to the url for assets + def test_javascript_include_tag + assert_dom_equal "", javascript_include_tag('/foo') + assert_dom_equal "", javascript_include_tag('/foo', extname: false ) + assert_dom_equal "", javascript_include_tag('/foo', extname: '.bar' ) + end + def test_javascript_tag_with_options assert_dom_equal "", javascript_tag("alert('hello')", :id => "the_js_tag")