Created
April 16, 2013 17:53
-
-
Save johnthethird/5398019 to your computer and use it in GitHub Desktop.
Compile an arbitrary string of SCSS code in Rails controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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