Created

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
// helper to create proper asset paths if an asset host is configured
App.assets = {
 
assets : {
<% AssetsUtil.images.each do |img| %>
"<%= img %>" : "<%= asset_path(img) %>",
<% end %>
},
url : function(path) {
return window.ASSETS_URL ? window.ASSETS_URL + path : path;
},
asset_path : function(source) {
return this.url("/assets/"+source);
},
 
imageUrl : function(imageName) {
if(this.assets && this.assets[imageName]) return this.assets[imageName];
 
return this.asset_path(imageName);
},
}
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
module AssetsUtil
 
def self.assets_url
self.config["environments"][Rails.env]["assets"]
end
def self.server_host
self.config["environments"][Rails.env]["host"]
end
 
def self.config
@@config ||= YAML.load_file(File.join(Rails.root, 'config', 'assets.yml'))
 
@@config
end
 
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.imageUrl('bookmarklet/add.png') %>">
<div>

where does the assets_util.rb belong?

xentek commented

@thegreatmichael

(edited) If you save the file to lib/assets_util.rb, and add a config/initalizers/assets_util.rb that requires it (e.g. , `require 'lib/assets_util') you should be good to go.

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.