-
-
Save michaeldelorenzo/5310860 to your computer and use it in GitHub Desktop.
JavaScript asset helper. Forked from https://gist.github.com/patrickberkeley/3879730, cleaned up the assignment to "images" in the App.assets object to not have an extra comma and to be a single line.
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
App.assets = { | |
// Returns an object containing all of asset pipeline's image paths. | |
// | |
// Sample: | |
// | |
// { | |
// avatars/missing_avatar.png: "/assets/avatars/missing_avatar.png" | |
// chosen-sprite.png: "/assets/chosen-sprite.png" | |
// circle_green.png: "/assets/circle_green.png" | |
// circle_orange.png: "/assets/circle_orange.png" | |
// circle_red.png: "/assets/circle_red.png" | |
// circle_yellow.png: "/assets/circle_yellow.png" | |
// document.png: "/assets/document.png" | |
// } | |
// | |
images: <%= (AssetsUtil.images.inject({}){|hash,img| hash[img] = asset_path(img);hash}.to_json) %>, | |
// Return a formatted URL for an asset. | |
// | |
// Sample: | |
// | |
// "/assets/document/png." | |
// | |
path: function(name) { | |
// If the file is in our images object, pull the path from there. | |
if (this.images && this.images[name]) { | |
return this.images[name]; | |
} | |
// Otherwise, create a generic asset path. | |
return '/assets/' + name; | |
} | |
} |
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
module AssetsUtil | |
def self.images | |
Dir.glob(Rails.root.join("app/assets/images/**/*.*")).map do |path| | |
path.gsub(Rails.root.join("app/assets/images/").to_s, "") | |
end | |
end | |
end |
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
<div class="example"> | |
<img src="<%= App.assets.path('bookmarklet/add.png') %>"> | |
<div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment