Skip to content

Commit

Permalink
avoid asset_path route helper collisions (#2511)
Browse files Browse the repository at this point in the history
If a Rails application has an `assets` route resource, than the
generated `asset_path` route helper will override the asset helper
of the same name.

Rails provides the aliases `path_to_asset` and `url_to_asset` to
avoid this, and uses them internally. We can do the same here.
  • Loading branch information
granth committed Apr 11, 2020
1 parent 6f43982 commit a84a4bb
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/webpacker/helper.rb
Expand Up @@ -7,8 +7,8 @@ def current_webpacker_instance
end

# Computes the relative path for a given Webpacker asset.
# Returns the relative path using manifest.json and passes it to asset_path helper.
# This will use asset_path internally, so most of their behaviors will be the same.
# Returns the relative path using manifest.json and passes it to path_to_asset helper.
# This will use path_to_asset internally, so most of their behaviors will be the same.
#
# Example:
#
Expand All @@ -19,13 +19,13 @@ def current_webpacker_instance
# <%= asset_pack_path 'calendar.css' %> # => "/packs/calendar-1016838bab065ae1e122.css"
def asset_pack_path(name, **options)
if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
asset_path(current_webpacker_instance.manifest.lookup!(name), options)
path_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
end
end

# Computes the absolute path for a given Webpacker asset.
# Returns the absolute path using manifest.json and passes it to asset_url helper.
# This will use asset_url internally, so most of their behaviors will be the same.
# Returns the absolute path using manifest.json and passes it to url_to_asset helper.
# This will use url_to_asset internally, so most of their behaviors will be the same.
#
# Example:
#
Expand All @@ -36,7 +36,7 @@ def asset_pack_path(name, **options)
# <%= asset_pack_url 'calendar.css' %> # => "http://example.com/packs/calendar-1016838bab065ae1e122.css"
def asset_pack_url(name, **options)
if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
asset_url(current_webpacker_instance.manifest.lookup!(name), options)
url_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
end
end

Expand Down Expand Up @@ -169,8 +169,8 @@ def sources_from_manifest_entrypoints(names, type:)

def resolve_path_to_image(name)
path = name.starts_with?("media/images/") ? name : "media/images/#{name}"
asset_path(current_webpacker_instance.manifest.lookup!(path))
path_to_asset(current_webpacker_instance.manifest.lookup!(path))
rescue
asset_path(current_webpacker_instance.manifest.lookup!(name))
path_to_asset(current_webpacker_instance.manifest.lookup!(name))
end
end

0 comments on commit a84a4bb

Please sign in to comment.