Provide image_path that works with Sprockets

Prior to this, functions in sprockets-sass conflicted with ours and caused an infinite loop and StackError. This fixes thomas-mcdonald/bootstrap-sass#257

Note that Sprockets' asset_path requires configuration if it's not already configured by your framework. For example, in a simple Rack app, add the following to your config.ru:

environment.context_class.class_eval do
  def asset_path(path, options = {})
    "/assets/#{path}"
  end
end
This commit is contained in:
Tristan Harward 2013-03-21 13:06:19 -04:00
parent 2be2d7d130
commit 3e40c4caea
3 changed files with 13 additions and 8 deletions

View File

@ -9,7 +9,7 @@ desc 'Dumps output to a CSS file for testing'
task :debug do
require 'sass'
require './lib/bootstrap-sass/compass_functions'
require './lib/bootstrap-sass/rails_functions'
require './lib/bootstrap-sass/sass_functions'
path = './vendor/assets/stylesheets'
%w(bootstrap bootstrap-responsive).each do |file|
engine = Sass::Engine.for_file("#{path}/#{file}.scss", syntax: :scss, load_paths: [path])

View File

@ -1,14 +1,19 @@
# This contains functions for use with a project *only* using Compass.
module Sass::Script::Functions
# Define image_path for Compass to allow use of sprites without url() wrapper.
def image_path(asset)
if defined?(::Compass)
image_url(asset, Sass::Script::Bool.new(true))
def image_path(source, options = {})
if defined?(::Sprockets)
::Sass::Script::String.new sprockets_context.image_path(source.value).to_s, :string
elsif defined?(::Compass)
image_url(source, Sass::Script::Bool.new(true))
else
# Revert to the old compass-agnostic path determination
asset_sans_quotes = asset.value.gsub('"', '')
asset_sans_quotes = source.value.gsub('"', '')
Sass::Script::String.new("/images/#{asset_sans_quotes}", :string)
end
end
protected
def sprockets_context # :nodoc:
options[:custom][:sprockets_context]
end
end

View File

@ -4,4 +4,4 @@ require 'test/unit'
require 'sass'
require 'lib/bootstrap-sass/compass_functions'
require 'lib/bootstrap-sass/rails_functions'
require 'lib/bootstrap-sass/sass_functions'