<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Ingo Weiss</title>
  <link rel="self" href="http://blog.ingoweiss.com/feed.atom"/>
  <link rel="alternate" href="http://blog.ingoweiss.com"/>
  <id>http://blog.ingoweiss.com/feed.atom</id>
  <updated>2010-05-12T16:39:41Z</updated>
  <author>
    <name>Ingo Weiss</name>
  </author>
  <entry>
    <title>Resource Awareness</title>
    <link rel="alternate" href="http://blog.ingoweiss.com/2010/05/10/resource-awareness.html"/>
    <id>http://blog.ingoweiss.com/2010/05/10/resource-awareness.html</id>
    <updated>2010-05-10T00:00:00Z</updated>
    <author>
      <name>Ingo Weiss</name>
    </author>
    <content type="html">
&lt;h1&gt;Resource Awareness&lt;/h1&gt;
&lt;p class="date"&gt;May 10, 2010 Ingo Weiss&lt;/p&gt;
&lt;p&gt;Resources have been a big topic in Rails ever since DHH&amp;#8217;s &lt;a href="http://www.scribemedia.org/2006/07/09/dhh/"&gt;keynote&lt;/a&gt; of 2006. All the more surprising I find the fact that four years later, the concept of resources in Rails is still largely limited to coding conventions materialized as generators (the &lt;em&gt;scaffold&lt;/em&gt; and &lt;em&gt;resource&lt;/em&gt; generators) and macros (such as the &lt;em&gt;resource/s&lt;/em&gt; routing &lt;span class="caps"&gt;DSL&lt;/span&gt; methods) and conspicuously unavailable to code at runtime. A Rails application does not know what resources it exposes, for example, nor does a controller know which resource it is handling. If you want to take advantage of the predictable behavior of standard Rails resources and jump to the next level of abstraction (as we do in a current project), however, then this is exactly the kind of information you are likely to need.&lt;/p&gt;
&lt;p&gt;In Rails the term &amp;#8216;&lt;em&gt;resource&lt;/em&gt;&amp;#8217; is most commonly used as representing the set of routes generated by the &lt;code&gt;resource/s&lt;/code&gt; routing &lt;span class="caps"&gt;DSL&lt;/span&gt; methods (We are really talking about a &lt;em&gt;group&lt;/em&gt; of related &lt;span class="caps"&gt;REST&lt;/span&gt; resources when we talk about &lt;em&gt;a resource&lt;/em&gt; in Rails). During the invocation of &lt;code&gt;resource/s&lt;/code&gt; in the Rails initialization sequence, instances of &lt;code&gt;ActionDispatch::Routing::Mapper::Resources::Resource&lt;/code&gt;  are created representing the resource. Those instances are only used for route instantiation, however, and unlike the route instances themselves who are kept around and accessible at runtime via &lt;code&gt;Rails.application.routes&lt;/code&gt;, they are thrown away after the routes for the resource are instantiated.&lt;/p&gt;
&lt;p&gt;What I would like to see happen is for the &lt;code&gt;ActionDispatch::Routing::Mapper::Resources::Resource&lt;/code&gt; class to be promoted to encapsulate all relevant information about a resource (not just what&amp;#8217;s needed for route instantiation), and it&amp;#8217;s instances to be kept around and be accessible at &lt;code&gt;Rails.application.resources&lt;/code&gt;. I plan to write a patch to that effect, but needed the functionality now so I wrote the &lt;a href="http://github.com/ingoweiss/resource_awareness"&gt;resource_awareness&lt;/a&gt; gem as a short-term fix. The gem makes information about the resources defined via &lt;code&gt;resource/s&lt;/code&gt; available at &lt;code&gt;Rails.application.resources&lt;/code&gt;, as a collection of &lt;code&gt;Rails::Resource&lt;/code&gt; instances (please see the &lt;a href="http://github.com/ingoweiss/resource_awareness"&gt;&lt;span class="caps"&gt;README&lt;/span&gt;&lt;/a&gt; for more information about their attributes). It also gives controllers a &lt;code&gt;resource&lt;/code&gt; method returning the resource they are handling, if any.&lt;/p&gt;
&lt;p&gt;I believe that making this information available to code at runtime opens the door for interesting new abstractions, and I hope to write about some in further posts.&lt;/p&gt;    </content>
  </entry>
  <entry>
    <title>Fun with ResourceAwareness and ActiveResource</title>
    <link rel="alternate" href="http://blog.ingoweiss.com/2010/05/12/resource-awareness-and-active-resource.html"/>
    <id>http://blog.ingoweiss.com/2010/05/12/resource-awareness-and-active-resource.html</id>
    <updated>2010-05-12T00:00:00Z</updated>
    <author>
      <name>Ingo Weiss</name>
    </author>
    <content type="html">
&lt;h1&gt;Fun with ResourceAwareness and ActiveResource&lt;/h1&gt;
&lt;p class="date"&gt;May 12, 2010 Ingo Weiss&lt;/p&gt;
&lt;p&gt;In my last post (&lt;a href="/2010/05/10/resource-awareness.html"&gt;Resource Awareness&lt;/a&gt;), I introduced the &lt;a href="http://github.com/ingoweiss/resource_awareness"&gt;resource_awareness&lt;/a&gt; gem and the thoughts behind it. In this post I would like to explore one interesting thing you can do with it, namely expose an application&amp;#8217;s resources as a resource in turn which makes it very easy for other applications to dynamically create ActiveResource clients for interacting with those resources. Let me demonstrate:&lt;/p&gt;
&lt;p&gt;Suppose you have an application with the following resources:&lt;/p&gt;
&lt;pre class="ruby"&gt;&lt;code class="ruby"&gt;# config/routes.rb
resources :countries, :controller =&amp;gt; 'countries' do
 resources :cities, :controller =&amp;gt; 'country_cities'
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&amp;#8217;s expose those resources like so:&lt;/p&gt;
&lt;pre class="ruby"&gt;&lt;code class="ruby"&gt;# Gemfile
gem 'resource_awareness'&lt;/code&gt;

&lt;code class="ruby"&gt;# config/routes.rb
resources :resources&lt;/code&gt;

&lt;code class="ruby"&gt;# app/controllers/resources_controller.rb
respond_to :xml&lt;/code&gt;

&lt;code class="ruby"&gt;def index
  respond_with Rails.application.resources
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Information about the app&amp;#8217;s resources is now available at &lt;code&gt;/resources.xml&lt;/code&gt;. Now let&amp;#8217;s start a server at port 3000 and fire up a console for another app to interact with the first app. First, we need an ActiveResource client to retrieve the first app&amp;#8217;s resource information:&lt;/p&gt;
&lt;pre class="ruby"&gt;&lt;code class="ruby"&gt;# console
&amp;gt; class RemoteResource &amp;lt; ActiveResource::Base; self.site = 'http://localhost:3000'; self.element_name = 'resource'; end
&amp;gt; RemoteResource.first.attributes
=&amp;gt; {"id"=&amp;gt;"country_cities", "path_prefix"=&amp;gt;"/countries/:country_id", "singular_name"=&amp;gt;"city", "path"=&amp;gt;"/countries/:country_id/cities", ...}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Turns out that two of the resource&amp;#8217;s attributes, &lt;code&gt;singular_name&lt;/code&gt; and &lt;code&gt;path_prefix&lt;/code&gt;, is all we need to create an ActiveResource client for it. In most real world cases we would probably want to do this dynamically and would not want to create named classes blindly, so let&amp;#8217;s use an anonymous ActiveResource subclass instead:&lt;/p&gt;
&lt;pre class="ruby"&gt;&lt;code class="ruby"&gt;# console
&amp;gt; r = RemoteResource.first
&amp;gt; client_class = Class.new(ActiveResource::Base){self.site = "http://localhost:3000#{r.path_prefix}"; self.element_name = r.singular_name}
&amp;gt; client_class.create(:country_id =&amp;gt; 1, :name =&amp;gt; 'Berlin').attributes
=&amp;gt; {"name"=&amp;gt;"Berlin", "created_at"=&amp;gt;Wed May 05 10:51:31 UTC 2010, ... }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Just a fun experiment at first, this approach has already proven very useful in a current project of ours which I hope will be the topic of another post. I could also imagine this being useful in circumstances where you would otherwise use something more heavyweight such as &lt;a href="http://en.wikipedia.org/wiki/Web_Application_Description_Language" title="Web Application Description Language"&gt;&lt;span class="caps"&gt;WADL&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;    </content>
  </entry>
</feed>
