Bustikated

Equal parts geeknobabble & jackassery

CSS Media Fu

Posted by BM5k on March 21, 2008 at 10:51 PM

Inspired by a similar php solution found here, I decided to try and do this in Rails.

I quickly realized that something like this could be implemented as a plugin, in spite of it’s simplicity.

Google led me to this howto on creating generators, and following along with this and another of Urubatan’s guides, I was able to get a working plugin created.

Installation

As of right now, I do not have a public subversion repository. This should change within a week or two. The only way to install the plugin is to manually unzip it into your application’s vendor/plugin directory.

Get the zip here

This is my first plug-in, and was written more as a chance for me to explore plugin creation than out of necessity.

If you’re interested in creating your own plugin, you can read my notes by clicking ‘more’.

First things first

set up an app to do your plugin dev in. I created a program called ‘plugin’

cd ~/Projects/rails
rails plugins
cd plugins

I also created a simple model to use for testing later on


ruby script/generate scaffold task name:string id:integer
Create your plugin

ruby script/generate plugin css_media_fu
The init hook, in init.rb, simply makes the plugin module available to all of your application’s views:
ActionView::Base.send :include, CSS_Media_fu

The actual module in lib/css_media_fu.rb isn’t very complicated either, just 5 short methods.

For this plugin, I needed a simple generator, so I created the following tree inside vendor/plugins/css_media_fu

  • generators/
    • media_types/
      • media_types_generator.rb
      • templates/
        • default_media_types.yml

The simple migration code inside media_types_generator.rb copies templates/default_media_types.yml to your config/ folder as media_types.yml. The plugin depends on finding config/media_types.yml.

Hierarchy: previous, next

Comments

There are 0 comments on this post. Post yours →

Post a comment

Required fields in bold.