Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (54 sloc) 3.92 KB
format title published_on
textile
SoundBytes: geddit?
Mon Aug 21 18:16:00 UTC 2006

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_linktag.

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.