Skip to content

Instantly share code, notes, and snippets.

@kares kares/assets.rb
Last active Nov 29, 2016

Embed
What would you like to do?
SASS (asset) path/url functions patch to make sure non absolute /assets URLs are returned (e.g. for bootstrap-sass)
if File.basename($0) == 'rake' && ARGV.include?('assets:precompile') # Rails.env.production?
puts "patching sass functions to return relative asset paths" if $VERBOSE
begin
require 'sprockets/sass/functions'
sass_functions = Sprockets::Sass::Functions
Autoload::Sass::Script rescue nil # loading
rescue LoadError # 2.x
require 'sprockets/sass_functions'
sass_functions = Sprockets::SassFunctions
end
sass_functions.module_eval do
# relativize path - called from sass (assuming a flat public/assets structure) :
# e.g. src:url("/assets/glyphicons-halflings-regular-xxx.eot") will end up
# as src:url("glyphicons-halflings-regular-xxx.eot")
# NOTE: currently this likely won't work for target assets not on assets.paths
# ... or when an (including) asset that ends up in the /assets sub-directory !
%w{ image_path font_path audio_path video_path javascript_path stylesheet_path }.each do |method|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(source, options = {})
asset_prefix = Rails.application.config.assets.prefix
path = sprockets_context.font_path(source.value)
::Sass::Script::String.new(path.sub(File.join(asset_prefix, '/'), ''), :string)
end
RUBY
end
%w{ image_url font_url audio_url video_url javascript_url stylesheet_url }.each do |method|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(source, options = {})
asset_prefix = Rails.application.config.assets.prefix
path = sprockets_context.font_path(source.value)
::Sass::Script::String.new("url(" + path.sub(File.join(asset_prefix, '/'), '') + ")")
end
RUBY
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.