10.19.06

SimpleRCSS Plugin

by Chris Abad

I needed a way to embed ruby into my stylesheets. I primarily wanted the ability to develop helper methods to save myself typing on some of the repetitive CSS techniques. Josh Susser came up with a very easy way to accomplish this. I took this idea a step further and turned it into a plugin so I could re-use it for other project. I figured I’d make it available to the public in case others found it useful. Feel free to provide feedback on how to improve this plugin.

Installation

Really simple. First install the plugin:

./script/plugin install -x http://svn.integralserver.com/plugins/simple_rcss

Then, just add this to your config/routes.rb:

map.rcss 'stylesheets/:rcss.css', :controller => 'stylesheets', :action => 'rcss'

Usage

RCSS files are placed in:

$app_root
views/
stylesheets/
stylesheet_1.rcss
stylesheet_2.rcss

While in production mode, this plugin will automatically cache your stylesheets into public/stylesheets. If you would like to pre-cache your stylesheets, you can do so with the included rake task:
rake rcss:create_cache

There are a few other rake tasks available to you as well:

rcss:remove_cache
rcss:update_cache

If you have any files in public/stylesheets/ which do not originate from app/views/stylesheets, you will lose them if you use rcss_remove_cache and rcss_update_cache. The safest thing to do is manage all your stylesheets in app/views/stylesheets. You can migrate over existing stylesheets using:

rake rcss:from_css

There is also a generator task to generate stylesheets for you:
./script/generate stylesheet Application

which will create:

$app_root
app/
views/
stylesheets/
application.rcss

You can also call certain stylesheets using :defaults from within the stylesheet_link_tag helper:
stylesheet_link_tag :defaults
This will automatically include an application.rcss as well as #{controller.controller_name}.rcss if they exist.

That pretty much covers the basics. I’ve used this on a few projects so far and it’s been working out just fine. My next step is to begin to extract commonly used code from my stylesheets and make those available through helper methods. If you have any suggestions for these, feel free to send them my way.

Comments

There are no comments.

Leave a Comment