Skip to content

Commit

Permalink
Add asset helpers to facilitate generating asset urls via the asset p…
Browse files Browse the repository at this point in the history
…ipeline.
  • Loading branch information
chriseppstein committed Jun 27, 2011
1 parent 1352235 commit 95d5653
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.markdown
Expand Up @@ -44,6 +44,22 @@ The [list of supported options](http://sass-lang.com/docs/yardoc/file.SASS_REFER
files (containing mixins and variables) because it is difficult to control the
cascade ordering for imports that contain styles using this approach.

* **Asset Helpers**. When using the asset pipeline, paths to assets must be rewritten.
When referencing assets use the following asset helpers:

* `asset_path($relative-asset-path, $asset-class)` - Returns a string to the asset.
For example: `asset_path("rails.png", image)` becomes `"/assets/rails.png"`
* `asset_url($relative-asset-path, $asset-class)` - Returns url reference to the asset.

For example: `asset_url("rails.png", image)` becomes `url(/assets/rails.png)`
* As a convenience, for each of the following asset classes there are
corresponding `_path` and `_url` helpers:
image, font, video, audio, javascript, stylesheet.

For example: `image_url("rails.png")` becomes `url(/assets/rails.png)` and
`image_path("rails.png")` becomes `"/assets/rails.png"`.


## Running Tests

$ bundle install
Expand Down
1 change: 1 addition & 0 deletions lib/sass/rails.rb
Expand Up @@ -7,6 +7,7 @@ module Rails
require 'sass/rails/logger'
require 'sass/rails/railtie'
require 'sass/rails/monkey_patches'
require 'sass/rails/helpers'
require 'sass/rails/importer'
require 'sass/rails/template_handlers'
require 'sass/rails/version'
38 changes: 38 additions & 0 deletions lib/sass/rails/helpers.rb
@@ -0,0 +1,38 @@
module Sass
module Rails
module Helpers

def asset_path(asset, kind)
Sass::Script::String.new(public_path(asset.value, kind.value), true)
end

def asset_url(asset, kind)
Sass::Script::String.new(%Q{url(#{public_path(asset.value, kind.value)})})
end

[:image, :font, :video, :audio, :javascript, :stylesheet].each do |asset_class|
class_eval %Q{
def #{asset_class}_path(asset)
asset_path(asset, Sass::Script::String.new("#{asset_class}"))
end
def #{asset_class}_url(asset)
asset_url(asset, Sass::Script::String.new("#{asset_class}"))
end
}, __FILE__, __LINE__ - 7

This comment has been minimized.

Copy link
@spastorino

spastorino Jun 27, 2011

Contributor

should be -6 here

end

protected
def public_path(asset, kind)
options[:custom][:resolver].public_path(asset, kind.pluralize)
end
end
end
end

module Sass
module Script
module Functions
include Sass::Rails::Helpers
end
end
end
5 changes: 4 additions & 1 deletion lib/sass/rails/template_handlers.rb
Expand Up @@ -57,7 +57,10 @@ def sass_options(scope)
:line => line,
:syntax => syntax,
:importer => importer,
:load_paths => load_paths
:load_paths => load_paths,
:custom => {
:resolver => Resolver.new(scope)
}
)
end

Expand Down
Expand Up @@ -8,3 +8,9 @@
color: yellow;
@include background-from-partial(red);
}

.rails {
asset-path: asset-path("rails.png", image);
asset-url: asset-url("rails.png", image);
image-url: image-url("rails.png");
}
6 changes: 6 additions & 0 deletions test/sass_rails_test.rb
Expand Up @@ -54,6 +54,12 @@ class SassRailsTest < Sass::Rails::TestCase
assert_match css_output, /plain-old-css/
assert_match css_output, /another-plain-old-css/
end
test "sass asset paths work" do
css_output = sprockets_render("scss_project", "application.css.scss")
assert_match css_output, %r{asset-path:\s*"/assets/rails.png"}
assert_match css_output, %r{asset-url:\s*url\(/assets/rails.png\)}
assert_match css_output, %r{image-url:\s*url\(/assets/rails.png\)}
end
test "css compressor compresses" do
assert_equal "div{color:red}\n", Sass::Rails::CssCompressor.new.compress(<<CSS)
div {
Expand Down

0 comments on commit 95d5653

Please sign in to comment.