Skip to content

Commit

Permalink
Merge pull request #11707 from nathanstitt/extname_option
Browse files Browse the repository at this point in the history
javascript_include_tag "extname" option for use by asset_path
  • Loading branch information
rafaelfranca committed Aug 3, 2013
2 parents 73abbde + 0855f04 commit 376b13c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
14 changes: 14 additions & 0 deletions actionview/CHANGELOG.md
@@ -1,3 +1,17 @@
* Added an `extname` hash option for `javascript_include_tag` method.

Before:

javascript_include_tag('templates.jst')
# => <script src="/javascripts/templates.jst.js"></script>

After:

javascript_include_tag('templates.jst', extname: false )
# => <script src="/javascripts/templates.jst"></script>

*Nathan Stitt*

* Fix `current_page?` when the URL contains escaped characters and the
original URL is using the hexadecimal lowercased.

Expand Down
9 changes: 6 additions & 3 deletions actionview/lib/action_view/helpers/asset_tag_helper.rb
Expand Up @@ -26,7 +26,8 @@ module AssetTagHelper
# to <tt>assets/javascripts</tt>, 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 <tt>extname: false</tt> can be set on the options.
#
# You can modify the HTML attributes of the script tag by passing a hash as the
# last argument.
Expand All @@ -37,6 +38,9 @@ module AssetTagHelper
# javascript_include_tag "xmlhr"
# # => <script src="/assets/xmlhr.js?1284139606"></script>
#
# javascript_include_tag "template.jst", extname: false
# # => <script src="/assets/template.jst?1284139606"></script>
#
# javascript_include_tag "xmlhr.js"
# # => <script src="/assets/xmlhr.js?1284139606"></script>
#
Expand All @@ -51,8 +55,7 @@ module AssetTagHelper
# # => <script src="http://www.example.com/xmlhr.js"></script>
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)
Expand Down
7 changes: 7 additions & 0 deletions actionview/test/template/javascript_helper_test.rb
Expand Up @@ -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 "<script src='/foo.js'></script>", javascript_include_tag('/foo')
assert_dom_equal "<script src='/foo'></script>", javascript_include_tag('/foo', extname: false )
assert_dom_equal "<script src='/foo.bar'></script>", javascript_include_tag('/foo', extname: '.bar' )
end

def test_javascript_tag_with_options
assert_dom_equal "<script id=\"the_js_tag\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
javascript_tag("alert('hello')", :id => "the_js_tag")
Expand Down

0 comments on commit 376b13c

Please sign in to comment.