Skip to content

Instantly share code, notes, and snippets.

@jachenry
Created October 11, 2012 04:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jachenry/3870095 to your computer and use it in GitHub Desktop.
Save jachenry/3870095 to your computer and use it in GitHub Desktop.
SCSS Template Handler
require 'sass/plugin'
module Sass
module Rails
class TemplateHandler
class_attribute :default_format
self.default_format = Mime::CSS
def initialize
end
def call(template)
erb = ActionView::Template.registered_template_handler(:erb).call(template)
%{
environment = orig_environment = ::Rails.application.assets
environment = environment.instance_variable_get("@environment") if environment.is_a?(Sprockets::Index)
context = environment.context_class.new(environment, '', Pathname.new(''))
::Rails.application.assets = environment
options = Compass::Compiler.new(*Compass.configuration.to_compiler_arguments({
:syntax => :scss,
:custom => {
:resolver => ::Sass::Rails::Resolver.new(context)
}
})).options
::Rails.application.assets = orig_environment
Sass::Engine.new((begin;#{erb};end), options).render
}
end
end
end
end
ActiveSupport.on_load(:action_view) do
ActionView::Template.register_template_handler :scss, Sass::Rails::TemplateHandler.new
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment