Skip to content

Commit

Permalink
Add image_pack_tag helper
Browse files Browse the repository at this point in the history
  • Loading branch information
maschwenk committed Apr 2, 2018
1 parent 0bfb986 commit 2089704
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ def asset_pack_url(name, **options)
end
end

# Creates a image tag that references the named pack file.
#
# Example:
#
# <%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
# <img alt='Edit Entry' src='/packs/application-k344a6d59eef8632c9d1.png' width='16' height='10' />
def image_pack_tag(name, **options)
image_tag(asset_path(Webpacker.manifest.lookup!(name)), **options)
end

# Creates a script tag that references the named pack file, as compiled by webpack per the entries list
# in config/webpack/shared.js. By default, this list is auto-generated to match everything in
# app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
Expand Down
6 changes: 6 additions & 0 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def test_asset_pack_url
assert_equal "https://example.com/packs/bootstrap-c38deda30895059837cf.css", asset_pack_url("bootstrap.css")
end

def test_image_pack_tag
assert_equal \
"<img alt=\"Edit Entry\" src=\"/packs/application-k344a6d59eef8632c9d1.png\" width=\"16\" height=\"10\" />",
image_pack_tag("application.png", size: "16x10", alt: "Edit Entry")
end

def test_javascript_pack_tag
assert_equal \
%(<script src="/packs/bootstrap-300631c4f0e0f9c865bc.js"></script>),
Expand Down

0 comments on commit 2089704

Please sign in to comment.