Created — forked from codebrew/assets.js.erb

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

javascript asset helper

View assets.js.erb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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.each do |img| %>
"<%= img %>" : "<%= asset_path(img) %>",
<% end %>
},
 
// 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;
}
}
View assets.js.erb
1 2 3 4 5 6 7
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
View assets.js.erb
1 2 3
<div class="example">
<img src="<%= App.assets.path('bookmarklet/add.png') %>">
<div>

I put the assets_util.rb in my helpers directory.

Any way to refresh the App.assets if the the Rails caching is not updating.....?

I am having to stop the server, clear the cache, and start the server after every creation, because it only loads at the start. Any way to call /serve this up again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.