21 August 2006
SoundBytes: geddit?
In the style of 37Signals “SunSpots” articles – a small collection of odds and ends not big enough to warrant an entire entry to themselves – I present to you SoundBytes; little bites of information (but bytes, because we are geeks, right?) that are, as any of my Northern friends would say, sound.
Just two small little bite-sized nuggets for this edition…
Automatic stylesheet inclusion with stylesheet_include_tag
First up, stylesheet_include_tag is a small Rails helper that I wrote to help me organise my application stylesheets. I named it _include_tag to distinguish it from Rails’ own stylesheet_link_tag.
First of all, here’s the code:
def stylesheet_include_tag(*sources)
if sources.include?(:controller)
sources.delete(:controller)
sources.push(controller_stylesheet_source) if stylesheet_exists(controller_stylesheet_source)
end
if sources.include?(:action)
sources.delete(:action)
sources.push(action_stylesheet_source) if stylesheet_exists(action_stylesheet_source)
end
if sources.include?(:defaults)
sources.delete(:defaults)
sources.unshift('application')
sources.push(controller_stylesheet_source) if stylesheet_exists(controller_stylesheet_source)
sources.push(action_stylesheet_source) if stylesheet_exists(action_stylesheet_source)
end
sources.collect { |source|
path = "/stylesheets/#{source}.css"
tag('link', { 'type' => 'text/css', 'rel' => 'stylesheet', 'href' => path})
}.join("\n")
end
protected
def controller_stylesheet_source
params[:controller]
end
def action_stylesheet_source
[ params[:controller], params[:action] ].join("_")
end
def stylesheet_path(source)
"#{RAILS_ROOT}/public/stylesheets/#{source}.css"
end
def stylesheet_exists(source)
File.exists?(stylesheet_path(source))
end
So how do you use this little helper? It’s quite simple really – it takes three options:
:controller– looks for a stylesheet under /public/stylesheets called controllername.css – if it finds one, it will automatically include it.:action– as above, except it looks for a stylesheet called controllername_actionname.css:defaults– a combination of the two above options, plus it also looks for a stylesheet called application.css – this is the one that I usually use.
So what do you gain? Simply, the ability to break apart your stylesheets into sensible chunks (with global styles in application.css) and not having to worry about adding a link tag for every stylesheet you want to use. Simply drop in a stylesheet under /public/stylesheets using the naming conventions above and it will automatically detect it and add a link tag for you.
Just drop the above code into your ApplicationHelper (or a separate file, and include it) and that is all there is to it. This is one of the many helpers that I’ve written that I just couldn’t live without.
Comment notification for Mephisto
As you might have read, I recently moved my blog over to Mephisto from Wordpress. I’m loving Mephisto, but one of the features I miss is email notification of new comments. You could just use the RSS feed but some people, including myself, prefer email notifications. Implementing email notifications was pretty trivial – it was just a case of hooking up an observer to the Comment class. I’ve packaged up the extension as a normal Rails plugin. As I’m using trunk/tags you probably won’t have much luck installing it with script/plugin, so use svn co instead:
$ svn co http://opensource.agileevolved.com/svn/root/rails_plugins/mephisto_comment_notification/trunk \
vendor/plugins/mephisto_comment_notification
Please report any problems with the above plugin on the Agile Evolved Open Source Trac.
Return to home page | Check out my tumblelog
4 Comments on this article
Return to home page | Check out my tumblelog
1. Comment by AnÃbal Rojas on 22 Aug 2006 at 02:08
Off Topic: This is just a quick note to invite you to register at RubyCorner.com, a meeting place for people interested in the Ruby Programming Language or any of the related technologies.
2. Comment by Chu Yeow on 22 Aug 2006 at 08:08
Awesome snippet (the stylesheets one), saw you post it at SitePoint Forums (I’m redemption) earlier and thought I’d steal it for my own use :)
3. Comment by Luke Redpath on 22 Aug 2006 at 10:08
Hi Anibal, thanks but I’m already on RubyCorner.com ;)
4. Comment by skaar on 22 Aug 2006 at 17:08
action_stylesheet_source has a missing open square bracket for the joined array.