Question

I've seen that certain Rails CMSes (like Radiant) have plugins that essentially compile Sass when a page is accessed. Is there a way to do this in a regular rails app? Is doing so performant? Basically, I'm looking at a way to remove the extra step of running Compass to compile my stylesheets.

Was it helpful?

Solution

I've not used compass specifically but there looks like there's a production flag so files are compiled - I couldn't imagine they'd build it to recompile per request in production, Radiant compiles it's css on Application startup and if you then commit those generated CSS files it doesn't try to generate them again AFAIK.

http://compass-style.org/docs/tutorials/production-css/

OTHER TIPS

Sass and Compass automatically integrate with Rails. If you're using Rails 3, all you have to do is add gem "haml" to your Gemfile and all .sass and .scss files in public/stylesheets/sass will get compiled to .css files in public/stylesheets.

Compile per request? I think it could be a hit for performance. You should definitely use a caching strategy in that case. So that it compiles the stylesheet only if it is not in the cache.

You could create a helper method setup_stylesheet that will take care of setting up the css stylesheet. You call this method on the application layout.

setup_stylesheet will check if the css stylesheet is on the cache, and if it is there then use it. If it is not, then compile it.

Another approach:

You could set up an initialiser that will call Compass to compile your SASS stylesheets when the App is launched.

  • Is doing so performant?

There will be a massive performance hit when compiling at run-time.

As Nex3 (author of Sass gem) pointed out on another forum, there's no need any need to run compass watch.

I strongly advise putting the following into production.rb: Sass::Plugin.options[:never_update] = true - this is especially important if you're on Heroku. (you could also do this in your rack file, where you can also specify other options

Hmm, good luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top