Permalink
Browse files

Stub out context asset url helpers

  • Loading branch information...
1 parent b261e9c commit 2ad18c48fa9e3f232ea9e15a2209619b4c830b4b @josh josh committed Aug 21, 2012
View
@@ -363,6 +363,7 @@ submit a pull request.
**2.5.0**
+* Provide stubbed implementation of context *_path helpers
* Add SassCompressor
**2.4.5** (July 10, 2012)
View
@@ -21,6 +21,7 @@ module Sprockets
autoload :Processor, "sprockets/processor"
autoload :SassCacheStore, "sprockets/sass_cache_store"
autoload :SassCompressor, "sprockets/sass_compressor"
+ autoload :SassFunctions, "sprockets/sass_functions"
autoload :SassImporter, "sprockets/sass_importer"
autoload :SassTemplate, "sprockets/sass_template"
autoload :ScssTemplate, "sprockets/scss_template"
View
@@ -221,6 +221,56 @@ def asset_data_uri(path)
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end
+ # Expands logical path to full url to asset.
+ #
+ # NOTE: This helper is currently not implemented and should be
+ # customized by the application. Though, in the future, some
+ # basics implemention may be provided with different methods that
+ # are required to be overridden.
+ def asset_path(path, options = {})
+ message = <<-EOS
+Custom asset_path helper is not implemented
+
+Extend your environment context with a custom method.
+
+ environment.context_class.class_eval do
+ def asset_path(path, options = {})
+ end
+ end
+ EOS
+ raise NotImplementedError, message
+ end
+
+ # Expand logical image asset path.
+ def image_path(path)
+ asset_path(path, :type => :image)
+ end
+
+ # Expand logical video asset path.
+ def video_path(path)
+ asset_path(path, :type => :video)
+ end
+
+ # Expand logical audio asset path.
+ def audio_path(path)
+ asset_path(path, :type => :audio)
+ end
+
+ # Expand logical font asset path.
+ def font_path(path)
+ asset_path(path, :type => :font)
+ end
+
+ # Expand logical javascript asset path.
+ def javascript_path(path)
+ asset_path(path, :type => :javascript)
+ end
+
+ # Expand logical stylesheet asset path.
+ def stylesheet_path(path)
+ asset_path(path, :type => :stylesheet)
+ end
+
private
# Annotates exception backtrace with the original template that
# the exception was raised in.
View
@@ -7,6 +7,7 @@ class ContentTypeMismatch < Error; end
class EncodingError < Error; end
class FileNotFound < Error; end
class FileOutsidePaths < Error; end
+ class NotImplementedError < Error; end
class UnserializeError < Error; end
module EngineError
@@ -0,0 +1,70 @@
+require 'sass'
+
+module Sprockets
+ module SassFunctions
+ def asset_path(path)
@rafaelfranca

rafaelfranca Oct 20, 2012

Collaborator

@josh This should not accepts an options argument?

@rafaelfranca

rafaelfranca Oct 20, 2012

Collaborator

I'm asking this because sprockets_context.asset_path, when using sprockets-rails, receive a second argument, the options hash. In prior versions of sass-rails asset-path receive a second argument that is kind and now it doesn't receive. This will make harder the upgrade path.

I was discussing with @jeremy and if we add a second argument for it and pass to sprockets_contexnt.asset_path we can add a deprecation message easily here

@jeremy

jeremy Oct 20, 2012

++ noticed this while updating an app using the old asset-url('...', font) signature, dies with an args error that doesn't explain what to do about it

@josh

josh Oct 21, 2012

Contributor

I don't know which format should be supported long term. Actually wish sass-core would just standardize this.

I think sass-rails could probably install the legacy functions for now.

+ Sass::Script::String.new(sprockets_context.asset_path(path.value), :string)
+ end
+
+ def asset_url(path)
+ Sass::Script::String.new("url(" + sprockets_context.asset_path(path.value) + ")")
+ end
+
+ def image_path(path)
+ Sass::Script::String.new(sprockets_context.image_path(path.value), :string)
+ end
+
+ def image_url(path)
+ Sass::Script::String.new("url(" + sprockets_context.image_path(path.value) + ")")
+ end
+
+ def video_path(path)
+ Sass::Script::String.new(sprockets_context.video_path(path.value), :string)
+ end
+
+ def video_url(path)
+ Sass::Script::String.new("url(" + sprockets_context.video_path(path.value) + ")")
+ end
+
+ def audio_path(path)
+ Sass::Script::String.new(sprockets_context.audio_path(path.value), :string)
+ end
+
+ def audio_url(path)
+ Sass::Script::String.new("url(" + sprockets_context.audio_path(path.value) + ")")
+ end
+
+ def font_path(path)
+ Sass::Script::String.new(sprockets_context.font_path(path.value), :string)
+ end
+
+ def font_url(path)
+ Sass::Script::String.new("url(" + sprockets_context.font_path(path.value) + ")")
+ end
+
+ def javascript_path(path)
+ Sass::Script::String.new(sprockets_context.javascript_path(path.value), :string)
+ end
+
+ def javascript_url(path)
+ Sass::Script::String.new("url(" + sprockets_context.javascript_path(path.value) + ")")
+ end
+
+ def stylesheet_path(path)
+ Sass::Script::String.new(sprockets_context.stylesheet_path(path.value), :string)
+ end
+
+ def stylesheet_url(path)
+ Sass::Script::String.new("url(" + sprockets_context.stylesheet_path(path.value) + ")")
+ end
+
+ protected
+ def sprockets_context
+ options[:sprockets][:context]
+ end
+
+ def sprockets_environment
+ options[:sprockets][:environment]
+ end
+ end
+end
@@ -10,11 +10,17 @@ class SassTemplate < Tilt::Template
self.default_mime_type = 'text/css'
def self.engine_initialized?
- defined? ::Sass::Engine
+ defined?(::Sass::Engine) && defined?(::Sass::Script::Functions) &&
+ Sass::Script::Functions < Sprockets::SassFunctions
end
def initialize_engine
require_template_library 'sass'
+
+ # Install custom functions. It'd be great if this didn't need to
+ # be installed globally, but could be passed into Engine as an
+ # option.
+ Sass::Script::Functions.send :include, Sprockets::SassFunctions
end
def prepare
@@ -0,0 +1,9 @@
+div {
+ url: url(asset-path("foo.svg"));
+ url: url(image-path("foo.png"));
+ url: url(video-path("foo.mov"));
+ url: url(audio-path("foo.mp3"));
+ url: url(font-path("foo.woff"));
+ url: url(javascript-path("foo.js"));
+ url: url(stylesheet-path("foo.css"));
+}
@@ -0,0 +1,9 @@
+div {
+ url: asset-url("foo.svg");
+ url: image-url("foo.png");
+ url: video-url("foo.mov");
+ url: audio-url("foo.mp3");
+ url: font-url("foo.woff");
+ url: javascript-url("foo.js");
+ url: stylesheet-url("foo.css");
+}
View
@@ -239,3 +239,41 @@ class TestSassCompressor < TestTiltSass
end
end
end
+
+class TestSassFunctions < TestSprocketsSass
+ def setup
+ super
+
+ @env.context_class.class_eval do
+ def asset_path(path, options = {})
+ "/#{path}"
+ end
+ end
+ end
+
+ test "path functions" do
+ assert_equal <<-EOS, render('sass/paths.scss')
+div {
+ url: url("/foo.svg");
+ url: url("/foo.png");
+ url: url("/foo.mov");
+ url: url("/foo.mp3");
+ url: url("/foo.woff");
+ url: url("/foo.js");
+ url: url("/foo.css"); }
+ EOS
+ end
+
+ test "url functions" do
+ assert_equal <<-EOS, render('sass/urls.scss')
+div {
+ url: url(/foo.svg);
+ url: url(/foo.png);
+ url: url(/foo.mov);
+ url: url(/foo.mp3);
+ url: url(/foo.woff);
+ url: url(/foo.js);
+ url: url(/foo.css); }
+ EOS
+ end
+end

2 comments on commit 2ad18c4

usage of image-tag video-tag is tedious. Just this:

module Sass
  module Script
    module Functions
      def url(asset)
        Sass::Script::String.new(%Q{url("#{asset_path(asset.value)}")})
      end
    end
  end
end

and everyone is happy
/cc @josh @sstephenson

P.S. it requires " around path: url("path") but anyway it's KISS way

Please sign in to comment.