Maybe if I explain myself a bit better, I 'll get some more feedback
from people.
I've been thinking a lot about what a restful interface should look
like and I've been primarily looking at Rails' implementation. As I've
been thinking about it, and looking at Roy Fielding's talk on the
subject, I think that there is something missing with the XML
serialization in rails. This is based on my observations of how Rails
seems to be creating its XML representations.
The issue concerns associated objects; the has_many and belongs_to
crowd. According to my understanding of Roy's presentation (I'll read
his dissertation, I promise), a representation of a resource exposes
its associated resources through its URL.
Let me give an example. Lets say I have the following models:
class Blog < ActiveRecord::Base
belongs_to :author
end
class Author < ActiveRecord::Base
has_many :blogs
end
Assuming that these are both resources, you'll get something like:
# GET /blogs/1.xml
<blog>
<id type="integer">1</id>
<name type="string">My Blog</name>
<author-id type="integer">1</author-id>
</blog>
# GET /authors/1.xml
<author>
<id type="integer">1</id>
<name type="string">Adam</name>
</author>
From Roy's presentation I think that the "REST Way" to do things would
be more like:
# GET /blogs/1.xml
<blog uri="http://rest.example.com/blogs/1.xml">
<name type="string">My Blog</name>
<author uri="http://rest.example.com/authors/1.xml" />
</blog>
# GET /authors/1.xml
<author uri="http://rest.example.com/authors/1.xml">
<name type="string">Adam</name>
<blogs>
<blog uri="http://rest.example.com/blogs/1.xml" />
<blog uri="http://rest.example.com/blogs/7.xml" />
<blog uri="http://rest.example.com/blogs/123.xml" />
</blogs>
</author>
I might also put the URI in an element but this way seems a lot more
compact. Besides, the URI is really metadata in this sense.
I think that this approach has a number of advantages. First is
polymorphism. Not that it wouldn't be challenging for your framework
to handle but the following member representation seems very
compelling (I'm going to skip the type attributes as I think they're
tedious for examples):
<member uri="http://rest.example.com/members/1.xml">
<name>Jim Bob</name>
<subscriptions>
<subscription uri="http://rest.example.com/blogs/1.xml" />
<subscription uri="http://rest.example.com/galleries/
2132.xml" />
</subscriptions>
</member>
I've represented a polymorphic association without having to express
what types the members are. I can determine that when I retrieve the
associated representations.
The second advantage is crossing the domain boundary. I was thinking
that it might be nice to refer to members by their Open ID uri but I
suspect that might not be a good example. However, we might extend or
previous example a bit:
<member uri="http://rest.example.com/members/1.xml">
<name>Jim Bob</name>
<subscriptions>
<subscription uri="http://rest.example.com/blogs/1.xml" />
<subscription uri="http://rest.example.com/galleries/
2132.xml" />
<subscription uri="http://groups.google.com/group/rubyonrails-
talk" />
</subscriptions>
</member>
Depending on what is consuming your API that may or may not be
problematic. I wouldn't see a problem with this if I was building a
JavaScript application to consume the XML. Perhaps if you could
associate an ActiveResource with an XML Schema rather than a URL, this
would be feasible in Rails.
Since not all associated objects are made into resources (for instance
join objects for has_many :through), those associations should be
realized directly in the XML representation (I believe the :include
option does this now).
So back to my questions. I want to be able to do all this without
having to create view files for each model I create. Instead I want to
create a plugin that modifies how to_xml works to give me the
behaviour I want. The only problem is that I'm having a hard time
getting my head around which methods of which classes I need to
override to get all this to happen.
Thanks!
Adam