has_metadata is a stupidly simple plug-in that allows you to store information in a model that isn’t important enough for its own column or table. It uses a single column called metadata to store metadata attributes as XML.

Unzip has_metadata into your vendor/plugins directory. Make sure your model’s table has a text column called metadata and add has_metadata to the model.

Animal < ActiveRecord::Base
  has_metadata
end

Example

has_metadata adds the following three methods to your model.

def set_metadata(name, key, value)
def get_metadata(name, key = nil)
def rm_metadata(name, key = nil)

It's pretty simple to use. Below is an example.

animal = Animal.new

animal.set_metadata("description", "haircolor", "brown")
animal.get_metadata("description") => {"haircolor" => "brown"}
animal.get_metadata("description", "haircolor") => "brown"

animal.rm_metadata("description", "haircolor")
animal.get_metadata("description", "haircolor") => nil

animal.rm_metadata("description")
animal.get_metadata("description") => nil

It seems that the to_xml method in Rails converts hash keys to strings, so you should always use strings.

Download: has_metadata.tar.bz2

  • Share/Bookmark

Tags: , ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>