Created
July 21, 2016 16:02
-
-
Save erik-megarad/bf485c310dc5a308ad620b76c0f9c912 to your computer and use it in GitHub Desktop.
Sprockets 3.x find_asset replacement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In Sprockets 2.x, you used to be able to find the location of an uncompiled asset by doing: | |
Rails.application.assets.find_asset(path) | |
# However, in Sprockets 3.x, `assets` is nil, `find_asset` isn't accessible without creating | |
# your own Sprockets::Manifest object, and there's no direct equivalent. The officially | |
# supported method is to use: | |
Rails.application.assets_manifest.assets[path] | |
# However, this only searches for *already compiled* assets. | |
# | |
# My use case: To embed fonts in a CSS file, I needed to find the path for | |
# fonts at precompile time, where the assets_manifest was effectively empty. | |
# Asset searching was relatively complicated because of the font-awesome gem | |
# and an in-house Engine which both had their own fonts. | |
# | |
# The solution: Basically fuck sprockets and do asset resolution yourself. | |
locations = Rails.application.config.assets[:paths] | |
locations.each do |location| | |
resolved_path = File.join(location, path) | |
if File.exists?(resolved_path) | |
return resolved_path | |
end | |
end | |
# Conclusion: Fuck sprockets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment