Skip to content

Instantly share code, notes, and snippets.

@johnthethird
Created April 16, 2013 17:53
Show Gist options
  • Select an option

  • Save johnthethird/5398019 to your computer and use it in GitHub Desktop.

Select an option

Save johnthethird/5398019 to your computer and use it in GitHub Desktop.
Compile an arbitrary string of SCSS code in Rails controller
# widget#custom_css is a string containing CSS or SCSS code
def custom_css
respond_to do |format|
format.css {
# I cant believe there isnt a better way to just compile a sass string. Sheesh.
sprockets_env = Rails.application.assets
opts = sprockets_env.context_class.sass_config
ctx = sprockets_env.context_class.new(sprockets_env, "inmemory", Pathname(sprockets_env.root+"/app/assets/stylesheets"))
opts = opts.merge( :syntax => :scss,
:load_paths => [Rails.root.join("app/assets/stylesheets")],
:filename => "embedded_css.css.scss",
:custom => {:resolver => Sass::Rails::Resolver.new(ctx)}
)
render :text => Sass.compile(widget.custom_css, opts), :content_type => "text/css"
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment