Thursday, January 3, 2008

my first rails plugin: acts_as_roxml

NOTE: Updated on 01-07-08 with Rails 2.0.2 info

In order for our little BPM to function we need to be able to marshal and unmarshal XML quickly and easily. I spent the day researching and coding a solution to this problem.

Rails Active Record models come baked in with fairly flexible to_xml and (as of 2.0) from_xml methods that allows you to serialize/deserialize model objects into transportable and editable xml documents. Unfortunately, the to_xml method (unlike to_json) does not support n-depth serialization. For example, you cannot serialize something like:
  • Books
    • Philosophy
      • Plato: Parmenides
      • Nietzche: Beyond Good & Evil

    • Fiction
      • Eliot: Middlemarch
      • Tolstoy: War and Peace



to_xml only allows the serialization of 1st and 2nd level, the individual books would be left out of the document. Additionally, I am not pleased with the method approach, I want to be able to add new attributes to my xml without having to change each instance of to_xml/from_xml. I also wanted more control over how my XML is structured.

The ROXML approach is closer to what I had in mind. I wanted the power and simplicity of ROXML baked right into my model objects. Simply including ROXML was not an option since it overrides the Active Record accessors. So I grabbed the ROXML source, made a few modifications and came up with acts_as_roxml.


acts_as_roxml allows you to add the annotations described here while providing a couple additional callbacks and helper functions that make it easy to make the following transformations: DB -> Object -> XML -> Object -> DB

The source can be found in our svn server under rails_sandbox/acts_as_roxml. To see it in action run: ruby test/unit/acts_as_roxml_test.rb I'll continue to tweak this while building the process engine. We may wind up with an extra plugin to contribute to the community.

Today:

  • Built acts_as_roxml plugin

4 comments:

pripple said...

This acts_as_roxml plugin sounds really wonderful. Are you planning to share this? It is just what I'm after for weeks now and as far as you describe it, it offers functionality that was often requested in mailing lists.
Could you please reply? Thanks a lot!
Take care -
Lawrence

Vodeclic said...

Hi,

Your acts_as_roxml plugin sound exactly of what i am looking for a while...

I m trying to do what you describe as DB->XML->DB... But the to_xml et hash.form_xml in rails make me crazy !!!

Is there a way you could share awesome code ?

Thanks in advance,

regards,
Guillaume.

pripple said...

Looks like we have a really tough nut to crack here... Mabe we should start a brand new plugin for rails here and share on the "standard" ways. Do you have any experience in that, Guillaume?
Take care-
Lawrence

Vodeclic said...

Well i don't... and that is too bad.... :(...

I m sure that kind of plugin could help so much the community...

We have to find that Martin who wrote the blog, he has a great plugin, why not to share !!