<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Nicksda</title>
	
	<link>http://nicksda.apotomo.de</link>
	<description>Nick Sutterer - respecting local traditions since 1981.</description>
	<lastBuildDate>Fri, 18 May 2012 15:53:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Nicksda" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="nicksda" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Ruby On REST 6: Pagination With Roar</title>
		<link>http://nicksda.apotomo.de/2012/05/ruby-on-rest-6-pagination-with-roar/</link>
		<comments>http://nicksda.apotomo.de/2012/05/ruby-on-rest-6-pagination-with-roar/#comments</comments>
		<pubDate>Fri, 18 May 2012 15:30:44 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[roar]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=1953</guid>
		<description><![CDATA[Yo! How&#8217;s it folks? Let&#8217;s do some more REST today. I&#8217;d like to show how easy it is to have paginated REST documents with Roar while using a nifty feature introduced in version 0.10.2. What&#8217;s In That Fruit Salad, Sir? Since we keep having fruit salads in the last posts I wanna write a service [...]]]></description>
			<content:encoded><![CDATA[<p>Yo! How&#8217;s it folks? Let&#8217;s do some more <span class="caps">REST</span> today. I&#8217;d like to show how easy it is to have paginated <span class="caps">REST</span> documents with <a href="https://github.com/apotonick/roar">Roar</a> while using a nifty feature introduced <a href="https://github.com/apotonick/roar/commit/46fb88d1a79338188a9d05d353fc721d6a058e4e">in version 0.10.2</a>.</p>
<h3>What&#8217;s In That Fruit Salad, Sir?</h3>
<p>Since we keep having fruit salads <a href="http://nicksda.apotomo.de/2012/03/ruby-on-rest-5-learn-hypermedia-by-making-fruit-salad/">in the last posts</a> I wanna write a service to display the ingredients of a particular fruit bowl in a list. Not just a list of fruits, a <em>paginated</em> list of fruits.</p>
<p>Let&#8217;s <span class="caps">GET</span> it.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">POST http:<span style="color:#006600; font-weight:bold;">//</span>bowls<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">/</span>fruits?page=<span style="color:#006666;">1</span></pre></div></div>

<p>Considering two fruit items per page this document will be returned.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;total_entries&quot;</span>:<span style="color:#006666;">5</span>,
 <span style="color:#996600;">&quot;links&quot;</span>:<span style="color:#006600; font-weight:bold;">&#91;</span>
  <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;rel&quot;</span>: <span style="color:#996600;">&quot;self&quot;</span>,
   <span style="color:#996600;">&quot;href&quot;</span>:<span style="color:#996600;">&quot;http://bowls/1/fruits?page=1&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;rel&quot;</span>: <span style="color:#996600;">&quot;next&quot;</span>,
   <span style="color:#996600;">&quot;href&quot;</span>:<span style="color:#996600;">&quot;http://bowls/1/fruits?page=2&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
 <span style="color:#996600;">&quot;items&quot;</span>:<span style="color:#006600; font-weight:bold;">&#91;</span>
  <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;title&quot;</span>:<span style="color:#996600;">&quot;Apple&quot;</span>,
   <span style="color:#996600;">&quot;links&quot;</span>:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;rel&quot;</span>:<span style="color:#996600;">&quot;self&quot;</span>,
              <span style="color:#996600;">&quot;href&quot;</span>:<span style="color:#996600;">&quot;http://fruits/Apple&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;title&quot;</span>:<span style="color:#996600;">&quot;Orange&quot;</span>,
   <span style="color:#996600;">&quot;links&quot;</span>:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;rel&quot;</span>:<span style="color:#996600;">&quot;self&quot;</span>,
             <span style="color:#996600;">&quot;href&quot;</span>:<span style="color:#996600;">&quot;http://fruits/Orange&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

<p>Ignore the <code>items</code> for now, the interesting part here are the pagination elements in the first lines. What we have here is:</p>
<ul>
<li>The number of total ingredients in the bowl (line 1).</li>
<li>The obligatory <code>self</code> link (line 3-4).</li>
<li>A link guiding us to the next page (line 5-6).</li>
</ul>
<h3>Wow! Code?</h3>
<p>The backing code in this service endpoint could look like the following snippet. As always, the code used to create this example <a href="https://github.com/apotonick/ruby-on-rest/commit/ed55c56c73f97a2c8f58257659b4dc223bd17f3e">can be found on github</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;">bowl = Bowl.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
page = bowl.<span style="color:#9900CC;">fruits</span>.<span style="color:#9900CC;">paginate</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, 
  <span style="color:#ff3333; font-weight:bold;">:per_page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
page.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>BowlPageRepresenter<span style="color:#006600; font-weight:bold;">&#41;</span>
page.<span style="color:#9900CC;">to_json</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:bowl</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@bowl</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>First task is to retrieve the viewed bowl (line 1). I use a static id in this example, feel free to replace it with something like <code>params[:id]</code> in your code. Next, I use <code>paginate</code> from the famous <a href="https://github.com/mislav/will_paginate">will_paginate</a> gem to get a paged subset of the included fruits. Again, the <code>:page</code> parameter should be refering to a variable, whatever (line 2-3).</p>
<p>Then I simply extend the collection (line 5) since it is a valid Ruby object and call <code>to_json</code> to render the list (line 6). Keep in mind that we pass the <code>:bowl</code> instance <em>into</em> the render method as an argument.</p>
<h3>The Bowl Representer Can Do Pagination </h3>
<p>To fully understand that example we need to look at the representer now.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> BowlPageRepresenter
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::Feature::Hypermedia</span>
&nbsp;
  property <span style="color:#ff3333; font-weight:bold;">:total_entries</span>
  collection <span style="color:#ff3333; font-weight:bold;">:items</span>, <span style="color:#ff3333; font-weight:bold;">:extend</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> FruitRepresenter
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:self</span> <span style="color:#9966CC; font-weight:bold;">do</span> |opts|
    bowl_url<span style="color:#006600; font-weight:bold;">&#40;</span>opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:bowl</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> current_page<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:next</span> <span style="color:#9966CC; font-weight:bold;">do</span> |opts|
    bowl_url<span style="color:#006600; font-weight:bold;">&#40;</span>opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:bowl</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> next_page<span style="color:#006600; font-weight:bold;">&#41;</span> \
      <span style="color:#9966CC; font-weight:bold;">if</span> next_page
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:previous</span> <span style="color:#9966CC; font-weight:bold;">do</span> |opts|
    bowl_url<span style="color:#006600; font-weight:bold;">&#40;</span>opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:bowl</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> previous_page<span style="color:#006600; font-weight:bold;">&#41;</span> \
      <span style="color:#9966CC; font-weight:bold;">if</span> previous_page
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> items
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Instead of refering to line number I&#8217;ll use code excerpts now. Do you like that better?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">  property <span style="color:#ff3333; font-weight:bold;">:total_entries</span></pre></div></div>

<p>In a paginated collection we got the <code>total_entries</code> method <a href="http://rubydoc.info/github/mislav/will_paginate/93e7b446900853d22e89/WillPaginate/Collection#total_entries-instance_method">as described in the will_paginate <span class="caps">API</span> docs</a>. To give our <span class="caps">REST</span> consumer a hint about the total amount of ingredients we can simply define a property after that.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">  collection <span style="color:#ff3333; font-weight:bold;">:items</span>, <span style="color:#ff3333; font-weight:bold;">:extend</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> FruitRepresenter</pre></div></div>

<p>To render the actual items in the doc we extend each fruit with the <code>FruitRepresenter</code> as we learned in <a href="http://nicksda.apotomo.de/2012/01/ruby-on-rest-3-one-model-multiple-representations/">an older post</a>. Note that Roar will use the <code>items</code> method to retrieve the collection of fruits.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> items
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Since we are already a collection all we have to do is return self &#8211; roar will iterate the paginated collection, extend each element with the <code>FruitRepresenter</code> and render the items.</p>
<h3>A Cool New Feature!</h3>
<p>The links to the next pages are defined using Roar&#8217;s hypermedia feature.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">  link <span style="color:#ff3333; font-weight:bold;">:next</span> <span style="color:#9966CC; font-weight:bold;">do</span> |opts|
    bowl_url<span style="color:#006600; font-weight:bold;">&#40;</span>opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:bowl</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> next_page<span style="color:#006600; font-weight:bold;">&#41;</span> \
      <span style="color:#9966CC; font-weight:bold;">if</span> next_page
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Two things happen here. First, note that this link is conditional. If <code>next_page</code>, another <span class="caps">API</span> method for a will_paginate collection, is evaluated to false, this link won&#8217;t be rendered.</p>
<p>Second, we use a new feature of Roar here to access variables passed into <code>to_json</code>. Remember how we called the render method?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">page.<span style="color:#9900CC;">to_json</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:bowl</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@bowl</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Right, we pass in some values from the outside since we don&#8217;t have access to the actual bowl instance within the represented collection. These parameters are accessible in the <code>link</code> block parameters. Isn&#8217;t that nice? I like it.</p>
<p>These few lines of code make it easy to render a paginated collection into a valid <span class="caps">REST</span> document.</p>
<h3>Writing A Generic Pagination Representer</h3>
<p>Now that all paginated documents share attributes (total entries, next and previous link, the self link, etc) why not abstract that into a generic representer? Most of the code can be reused.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> PaginationRepresenter
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::Feature::Hypermedia</span>
&nbsp;
  property <span style="color:#ff3333; font-weight:bold;">:total_entries</span>
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:self</span> <span style="color:#9966CC; font-weight:bold;">do</span> |opts|
    page_url<span style="color:#006600; font-weight:bold;">&#40;</span>opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:model</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> current_page<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:next</span> <span style="color:#9966CC; font-weight:bold;">do</span> |opts|
    page_url<span style="color:#006600; font-weight:bold;">&#40;</span>opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:model</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> next_page<span style="color:#006600; font-weight:bold;">&#41;</span> \
      <span style="color:#9966CC; font-weight:bold;">if</span> next_page
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:previous</span> <span style="color:#9966CC; font-weight:bold;">do</span> |opts|
    page_url<span style="color:#006600; font-weight:bold;">&#40;</span>opts<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:model</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:page</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> previous_page<span style="color:#006600; font-weight:bold;">&#41;</span> \
      <span style="color:#9966CC; font-weight:bold;">if</span> previous_page
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> items
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> page_url<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Implement me.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>All I did was calling a generic <code>page_url</code> method that must be implemented by the using representer. Also, I no longer use the <code>:bowl</code> keyword but a more generic <code>:model</code>, ok?</p>
<p>Nothing in this abstract representer module is related to stinky fruit salads anymore. Man, this thing could even represent a sixpack of beers (a domain I do prefer over fruits).</p>
<p>To inherit we just include the abstract into the concrete representer.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> BowlPageRepresenter
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> PaginationRepresenter
&nbsp;
  collection <span style="color:#ff3333; font-weight:bold;">:items</span>, <span style="color:#ff3333; font-weight:bold;">:extend</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> FruitRepresenter
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> page_url<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    bowl_url<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That is cool. All we have to do is defining the concrete items collection and how to compute the pagination URLs. Come on guys, that is easy!</p>
<p>Remember, we changed the incoming parameter, so the rendering call must change, too.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">page.<span style="color:#9900CC;">to_json</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:model</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@bowl</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<h3>Cheers!</h3>
<p>It is Friday eve, have a wonderful weekend and let me know how it was!</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2012/05/ruby-on-rest-6-pagination-with-roar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coercion Support in Roar with Virtus</title>
		<link>http://nicksda.apotomo.de/2012/05/coercion-support-in-roar-with-virtus/</link>
		<comments>http://nicksda.apotomo.de/2012/05/coercion-support-in-roar-with-virtus/#comments</comments>
		<pubDate>Sun, 13 May 2012 21:12:46 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[roar]]></category>
		<category><![CDATA[virtus]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=1939</guid>
		<description><![CDATA[Have you ever heard of coercion? Me neither. Anyway, people kept asking for coercion support in roar so I found out that coercion means converting strings into ruby types. Tireless I added a new feature with a whopping 3 lines of code &#8211; the cool thing is that this module marries roar with a nifty [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever heard of coercion? Me neither. Anyway, people kept asking for coercion support in <a href="https://github.com/apotonick/roar">roar</a> so I found out that coercion means <strong>converting strings into ruby types</strong>. Tireless I added a new feature with a whopping 3 lines of code &#8211; the cool thing is that this module marries roar with a nifty coercion gem called <a href="https://github.com/solnic/virtus">virtus</a> by my good friend <a href="http://solnic.eu/">Piotr Solnica</a>.</p>
<p>All you have to do is using the latest roar release (0.10.1), use the appropriate feature and the <code>:type</code> option.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ImmigrantSong
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::Feature::Coercion</span>
&nbsp;
  property <span style="color:#ff3333; font-weight:bold;">:composed_at</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">DateTime</span>, 
    <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;May 12th, 2012&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This will automatically convert the <code>composed_at</code> property to a decent <code>DateTime</code> object when parsing a document. Note the working <code>:default</code> option, too!</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">document = <span style="color:#996600;">&quot;{<span style="color:#000099;">\&quot;</span>composed_at<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>November 18th, 1983<span style="color:#000099;">\&quot;</span>}&quot;</span>
song = ImmigrantSong.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">from_json</span><span style="color:#006600; font-weight:bold;">&#40;</span>document<span style="color:#006600; font-weight:bold;">&#41;</span>
song.<span style="color:#9900CC;">composed_at</span> <span style="color:#008000; font-style:italic;">#=&gt; 1983-11-18T00:00:00+00:00</span></pre></div></div>

<p>The underlying virtus gem takes care of all the conversion work. Note that virtus also automatically adds accessors to the class.</p>
<h3>First World Problems</h3>
<p>A caveat is that this currently works with inline representers in classes, only. This is cause virtus doesn&#8217;t work within modules, <em>yet</em>. I guess we can expect a refactored virtus within days, right, solnic? As soon as this is working in virtus, you can savely use the conversion feature in modules, too. I keep you in the loop!</p>
<p>Oh, I forgot to thank <a href="https://twitter.com/myabc">Alex Coles</a> who made me aware of the simplicity of combining those two gems!</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2012/05/coercion-support-in-roar-with-virtus/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Why Is The Representer Called Representer?</title>
		<link>http://nicksda.apotomo.de/2012/05/why-is-the-representer-called-representer/</link>
		<comments>http://nicksda.apotomo.de/2012/05/why-is-the-representer-called-representer/#comments</comments>
		<pubDate>Fri, 11 May 2012 22:51:43 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[representer]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=1919</guid>
		<description><![CDATA[A number of users (hundreds, maybe thousands) have been asking why representers are called representers and not &#8220;representations&#8221;. It is a valid question and since I spent decades to come up with this term I&#8217;d like to discuss the decision here quickly. What Is A Representation? A representation in the REST style &#8211; and this [...]]]></description>
			<content:encoded><![CDATA[<p>A number of users (hundreds, maybe thousands) have been asking why representers are called representers and not &#8220;representations&#8221;. It is a valid question and since I spent decades to come up with this term I&#8217;d like to discuss the decision here quickly.</p>
<h3>What Is A Representation?</h3>
<p>A representation in the <span class="caps">REST</span> style &#8211; and this is what we&#8217;re talking about here &#8211; is <strong>a document describing a resource</strong>. Actually, it is illustrating the current state of that resource, usually with property/value lists, embedded resources and pointers to other resources using hypermedia. The format is not relevant in this context, it could be <span class="caps">JSON</span>, <span class="caps">HTML</span> or the accursed <span class="caps">XML</span>.</p>
<p>It is a document you can pull on your hard disk, save it and pop it out on christmas again. Did I mention that I&#8217;m writing this post in a cafe facing the beach with a cold Castle in my hand, blinded by the sun and intrigued to jump back into the waves?</p>
<h3>What Is A Representer?</h3>
<p>Now, on the other side, we have the representers as <a href="http://nicksda.apotomo.de/2011/12/ruby-on-rest-introducing-the-representer-pattern/">introduced by the roar gem</a>. Those guys are Ruby code and out in the wild you might find them written as modules, mostly. A representer may look like the following beautiful snippet.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> FruitRepresenter
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON</span>
&nbsp;
  property <span style="color:#ff3333; font-weight:bold;">:title</span>
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:self</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    fruit_url<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In short, injected into an object that very instance gets capable of <strong>rendering a document</strong> from its current state <em>and</em> updating its attributes by <strong>parsing a document</strong> &#8211; following the syntax definition from the representer module.</p>
<h3>Isn&#8217;t That A Presenter?</h3>
<p>Ok. A representer module enhances an object by enriching it with new methods. Isn&#8217;t that a presenter, that&#8217;s <a href="http://nicksda.apotomo.de/2011/12/ruby-on-rest-2-representers-and-the-dci-pattern/#comment-5255">what Jason wants to know</a>. </p>
<blockquote><p>The idea seems interesting, but I’m confused as to how this is different from a traditional presenter or decorator?
</p></blockquote>
<p>What I learnt <a href="http://www.youtube.com/watch?v=jk8FEssfc90">from the tech panel <em><span class="caps">OOP</span> vs. Rails</em></a> at the fantastic Wroclove.rb conference &#8211; if I listened to Steve Klabnik correctly &#8211; is</p>
<ul>
<li><strong>Decorator</strong> is a pattern that adds methods and optional state to an object. If I write a module introducing currency conversion methods into a float object it&#8217;s a Decorator.</li>
<li><strong>Presenter</strong> seems to be a subset of Decorator which adds functionality for, well, <em>presenting</em> the object. That could be an ActiveRecord row instance extended with methods to serialize to <span class="caps">JSON</span>.</li>
</ul>
<p><a href="http://twitter.com/#!/steveklabnik">Steve</a>, is that true?</p>
<p>So why is a representer not a presenter? Well, the answer is: <strong>it is a presenter, but it is more</strong>. The rendering behaviour as found in e.g. <code>to_json</code> in the representer is clearly presentation logic. However, the parsing mechanics are definitely beyond the scope of a Presenter. I agree that a representer still is a Decorator (adding functionality, great).</p>
<h3>Why Isn&#8217;t It Called Representation?</h3>
<p>People are worried about the &#8220;ter&#8221; ending (as in Termina<em>ter</em>). Scott got justified concerns that choosing &#8220;representer&#8221; as opposed to &#8220;representation&#8221; has roots in my Ruby hipster attitude.</p>
<blockquote><p>So, if &#8220;Representer&#8221; is a reflection of the popularity of &#8220;Presenter&#8221;,it would seem to be an example of throwing good money after bad. If &#8220;Presenter&#8221; is already somewhat tainted with inaccuracy, then so would &#8220;Representer&#8221;.
</p></blockquote>
<p>In an email he is refering to that confusion caused by the Presenter pattern name in the Ruby community (just google <em>presenter + ruby</em> if you can&#8217;t follow now). So his proposal</p>
<blockquote><p>The Fruit class is a &#8220;Representation&#8221;, not a representer.</p></blockquote>
<p>Oh no, call it representation!</p>
<p>And as if that wasn&#8217;t enough critics on <em>MY</em> pattern name, <a href="https://twitter.com/#!/cowboyd">Charles Lowell</a>, author of rubyracer, saviour of the poor and knight of the dark, reinforces the <a href="https://github.com/apotonick/roar-rails/issues/10">representation argument</a>.</p>
<blockquote><p>personally I prefer <code>FooRepresentation</code> as opposed to <code>FooRepresenter</code>, but that&#8217;s just me. It implies that &#8220;this defines what the representation <strong>is</strong>&#8220;, as opposed to &#8220;This defines is how the representation is generated&#8221;
</p></blockquote>
<p>That sounds conclusive to me and lead me to reflecting the terms once again.</p>
<h3>Now What, Representer or Representation?</h3>
<p>So, maybe the the concrete representation declaration (the module <em>using</em> roar and defining properties and hyperlinks) is a representation whereas the underlying abstracted module is a representer?</p>
<p>I speaking for myself still prefer the name representer as we not only define syntax and semantic of a representational document but also add functionality to the represented object as it gets enriched with e.g. <code>to_json</code> and <code>from_json</code>. <strong>That as-it makes the <code>FruitRepresenter</code> module &#8220;active&#8221;</strong> and justifies the &#8220;ter&#8221; ending, just as in &#8220;Terminater&#8221;, ok?</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2012/05/why-is-the-representer-called-representer/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Roar 0.10 with JSON-HAL Support and Representable 1.1.6 Released</title>
		<link>http://nicksda.apotomo.de/2012/04/roar-0-10-with-json-hal-support-and-representable-1-1-6-released/</link>
		<comments>http://nicksda.apotomo.de/2012/04/roar-0-10-with-json-hal-support-and-representable-1-1-6-released/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 15:55:41 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[roar]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=1911</guid>
		<description><![CDATA[Just a quick round-up for new things in the Roar world. Here we go! Roar Speaks JSON-HAL Now! In the last Ruby on REST post I explained the basics of the hypermedia type HAL which gives your document nesting and linking semantics. This promising format is now supported by Roar. By including the Representer::JSON::HAL module [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick round-up for new things in the <a href="https://github.com/apotonick/roar">Roar</a> world. Here we go!</p>
<h3>Roar Speaks <span class="caps">JSON-HAL</span> Now!</h3>
<p>In the last <a href="http://nicksda.apotomo.de/2012/03/ruby-on-rest-5-learn-hypermedia-by-making-fruit-salad/">Ruby on <span class="caps">REST</span> post</a> I explained the basics of the hypermedia type <a href="http://stateless.co/hal_specification.html">HAL</a> which <strong>gives your document nesting and linking</strong> semantics. This promising format is now supported by Roar.</p>
<p>By including the <code>Representer::JSON::HAL</code> module in your representer it will render and parse documents according to the <span class="caps">HAL</span> standard.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> OrderRepresenter
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON::HAL</span>
&nbsp;
  property <span style="color:#ff3333; font-weight:bold;">:id</span>
  collection <span style="color:#ff3333; font-weight:bold;">:items</span>,
    <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Item, 
    <span style="color:#ff3333; font-weight:bold;">:extend</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> ItemRepresenter,
    <span style="color:#ff3333; font-weight:bold;">:embedded</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:self</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#996600;">&quot;http://orders/#{id}&quot;</span> <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Links will now be pushed into the <code>_links</code> property. Also note the new <code>:embedded</code> option which will key the nested property under <code>_embedded</code>. And all that just by including a module &#8211; that&#8217;s the power of representers.</p>
<h3>Better Links in <span class="caps">JSON</span></h3>
<p><a href="https://github.com/apotonick/roar/issues/15">Some users asked</a> for another link format when <em>not</em> using the <span class="caps">HAL</span> representer. Usually links are handled like this.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;links&quot;</span>:<span style="color:#006600; font-weight:bold;">&#91;</span>
  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;rel&quot;</span>:  <span style="color:#996600;">&quot;self&quot;</span>, 
    <span style="color:#996600;">&quot;href&quot;</span>: <span style="color:#996600;">&quot;http://orders/42&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;rel&quot;</span>:  <span style="color:#996600;">&quot;items&quot;</span>, 
    <span style="color:#996600;">&quot;href&quot;</span>: <span style="color:#996600;">&quot;http://orders/42/items&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>This is just a format I came up with when writing Roar. Now, if you include <code>Representer::JSON::HAL::Links</code> in your representer, links will be rendered and parsed a bit different.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;links&quot;</span>: <span style="color:#006600; font-weight:bold;">&#123;</span> 
  <span style="color:#996600;">&quot;self&quot;</span>:  <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;href&quot;</span>: <span style="color:#996600;">&quot;http://orders/42&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, 
  <span style="color:#996600;">&quot;items&quot;</span>: <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;href&quot;</span>: <span style="color:#996600;">&quot;http://orders/42/items&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Here, <code>links</code> is a hash keyed by <code>rel</code> &#8211; very handy on the client side.</p>
<p>Also, you can now include more attributes in links.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">link <span style="color:#ff3333; font-weight:bold;">:rel</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:self</span>, <span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;That's me!&quot;</span></pre></div></div>

<p>Just provide a hash listing the attributes.</p>
<h3>Representable Got Conditional Properties!</h3>
<p>Last but not least, <a href="https://github.com/apotonick/representable">representable</a>, the underlying mapping gem of Roar got a new option in the 1.1.6 release. <code>:if</code> allows you to define blocks that are evaled when parsing or rendering (&#8220;representing&#8221;, yo!) the object. If the proc returns false, the property is ignored.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">property <span style="color:#ff3333; font-weight:bold;">:state</span>, <span style="color:#ff3333; font-weight:bold;">:if</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> authorized? <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>The block is executed in instance context allowing you to access instance methods and whatever else you need. Go for it!</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2012/04/roar-0-10-with-json-hal-support-and-representable-1-1-6-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby On REST 5: Learn Hypermedia By Making Fruit Salad</title>
		<link>http://nicksda.apotomo.de/2012/03/ruby-on-rest-5-learn-hypermedia-by-making-fruit-salad/</link>
		<comments>http://nicksda.apotomo.de/2012/03/ruby-on-rest-5-learn-hypermedia-by-making-fruit-salad/#comments</comments>
		<pubDate>Tue, 27 Mar 2012 10:55:05 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HATEOAS]]></category>
		<category><![CDATA[hypermedia]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=1874</guid>
		<description><![CDATA[Let me start this post by quoting something smart. &#8220;REST [is an] architectural style for distributed hypermedia systems&#8221; That actually is the first line, chapter 5 of Roy Fielding&#8217;s thesis about REST. I gave a couple of talks last year about hypermedia and the misunderstandings in REST in the Rails world and this post tries [...]]]></description>
			<content:encoded><![CDATA[<p>Let me start this post by quoting something smart.</p>
<blockquote><p>&#8220;<span class="caps">REST</span> [is an] architectural style for distributed hypermedia systems&#8221;</p></blockquote>
<p>That actually is the <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm">first line, chapter 5</a> of Roy Fielding&#8217;s thesis about <span class="caps">REST</span>. I gave a couple of talks last year about hypermedia and the misunderstandings in <span class="caps">REST</span> in the Rails world and this post tries to sum up with that by focusing on the <em>hypermedia</em> aspect of this architectural concept.</p>
<h3>Challenge: Make Fruit Salad!</h3>
<p>In the <a href="http://nicksda.apotomo.de/tag/rest/">last posts</a> we discussed using the <a href="http://github.com/apotonick/roar">Roar gem</a> for designing and implementing <span class="caps">REST</span> systems in Ruby. We were using fruits and bowls as an exemplary domain &#8211; let&#8217;s keep up with that and fix a fruit salad today.</p>
<p>As a first step it makes sense getting a bowl for all the ingredients. In our <span class="caps">REST</span> system, we just <span class="caps">POST</span> to the bowls <span class="caps">URL</span>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">POST http:<span style="color: #000000; font-weight: bold;">//</span>bowls
Content-type: application<span style="color: #000000; font-weight: bold;">/</span>json
<span style="color: #660033;">------------------------------</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;location&quot;</span>: <span style="color: #ff0000;">&quot;desk&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>The request body contains a minimal document specifying the location for the created bowl. Here&#8217;s what we get back.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;location&quot;</span>:<span style="color: #ff0000;">&quot;desk&quot;</span>,
 <span style="color: #ff0000;">&quot;fruits&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>,
 <span style="color: #ff0000;">&quot;links&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#91;</span>
  <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;rel&quot;</span>:<span style="color: #ff0000;">&quot;self&quot;</span>,  <span style="color: #ff0000;">&quot;href&quot;</span>:<span style="color: #ff0000;">&quot;http://bowls/desk&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>,
  <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;rel&quot;</span>:<span style="color: #ff0000;">&quot;fruits&quot;</span>,<span style="color: #ff0000;">&quot;href&quot;</span>:<span style="color: #ff0000;">&quot;http://bowls/desk/fruits&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>By reading this I figure out that the bowl on my desk was created, but doesn&#8217;t contain any fruits, yet. Also, these <em>&#8220;links&#8221;</em> seem understandable, while <code>self</code> might point to the bowl resource it<em>self</em> the <code>fruits</code> link probably directs to a collection resource listing all the ingredients in that very bowl.</p>
<p>Looks as if a <span class="caps">URL</span> gets a &#8220;meaning&#8221; by specifying a <code>rel</code> attribute with it.</p>
<p>I infered all that by intuition, as <span class="caps">JSON</span> is a pretty simple representation format. And this is the first problem when trying to understand hypermedia. <span class="caps">JSON</span> as it doesn&#8217;t include any semantics, the links are, well, useless as they&#8217;re not understandable to machines. What we need is a <strong>hyper-media type</strong>. Get it on!</p>
<h3>What Is A Media Type, Sir?</h3>
<p>A concept called <em>media type</em> gives both syntax and semantics to a document. It is important to understand that a simple media type like <span class="caps">JSON</span> doesn&#8217;t define anything. It just provides a medium (or format) to encode information.</p>
<p>Why not put it that way: <span class="caps">JSON</span> and <span class="caps">XML</span> are like primitive communication mechanics. Where <span class="caps">XML</span> uses smoke signals for exchanging messages, <span class="caps">JSON</span> is human voice. A poor example.</p>
<p>However, listening to my voice doesn&#8217;t mean you <em>understand</em> what I&#8217;m saying. Reading <span class="caps">JSON</span> is like listening to my <em>sounds</em>, but the noize might not make sense to you. Ok?</p>
<p>The media type is like a language. You understanding the media type &#8220;english&#8221; and me speaking (<span class="caps">JSON</span>) in english (media type) makes us happy chatters. And suddenly, a series of sounds turn into &#8220;Would you like another beer?&#8221;. You <em>understand</em> me since you know the syntax and the semantics of my sounds. That is a media type!</p>
<h3>Now That We Understand Hypermedia, What To Do With It?</h3>
<p>Instead of using plain, dumb <span class="caps">JSON</span> I&#8217;d like to introduce a real hypermedia format called <a href="http://stateless.co/hal_specification.html">HAL</a> which is &#8220;specifically for exposing RESTful hypermedia APIs&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">POST http:<span style="color: #000000; font-weight: bold;">//</span>bowls
Content-type: application<span style="color: #000000; font-weight: bold;">/</span>hal+json
<span style="color: #660033;">------------------------------</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;location&quot;</span>: <span style="color: #ff0000;">&quot;desk&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Note that we used the <code>application/hal+json</code> content type desparately hoping the web service understands this media format. Check the response.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;location&quot;</span>:<span style="color: #ff0000;">&quot;desk&quot;</span>,
 <span style="color: #ff0000;">&quot;_embedded&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;fruits&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>,
 <span style="color: #ff0000;">&quot;_links&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #ff0000;">&quot;self&quot;</span>:  <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;href&quot;</span>:<span style="color: #ff0000;">&quot;http://bowls/desk&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>,
  <span style="color: #ff0000;">&quot;fruits&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;href&quot;</span>:<span style="color: #ff0000;">&quot;http://bowls/desk/fruits&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>It obviously does, and now the links are encoded a little bit different, following the <span class="caps">HAL</span> standard. While this doesn&#8217;t make a real difference for you human being, it changes a lot in machine context. By using this media format, we added semantics: Now, the HAL-aware client <em>knows</em> that links can be found at the <code>_links</code> key, that they&#8217;re keyed by <code>rel</code> and so on.</p>
<p>Think about it briefly. Growing up with human voice you understood <span class="caps">JSON</span> before. However, now that you learned <span class="caps">HAL</span>, you understand all the bits and pieces in the document just by applying the specification rules.</p>
<h3>I&#8217;m Still Hungry!</h3>
<p>Sorry for that detailed discussion about hypermedia types, I&#8217;m really getting hungry as well, so let&#8217;s add some fruits to the bowl.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">POST http:<span style="color: #000000; font-weight: bold;">//</span>bowls<span style="color: #000000; font-weight: bold;">/</span>desk<span style="color: #000000; font-weight: bold;">/</span>fruits
Content-type: application<span style="color: #000000; font-weight: bold;">/</span>hal+json
<span style="color: #660033;">------------------------------</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;title&quot;</span>: <span style="color: #ff0000;">&quot;Apple&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>An apple a day keeps the doctor away. Assuming it is sufficient to post this mini <span class="caps">JSON</span> document to the <code>fruits</code> <span class="caps">URL</span> I add an apple to my salad. Did that work?</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">GET http:<span style="color: #000000; font-weight: bold;">//</span>bowls<span style="color: #000000; font-weight: bold;">/</span>desk
Content-type: application<span style="color: #000000; font-weight: bold;">/</span>hal+json
<span style="color: #660033;">------------------------------</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;location&quot;</span>:<span style="color: #ff0000;">&quot;desk&quot;</span>,
 <span style="color: #ff0000;">&quot;_embedded&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #ff0000;">&quot;fruits&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#91;</span>
   <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;title&quot;</span>:<span style="color: #ff0000;">&quot;Apple&quot;</span>,
    <span style="color: #ff0000;">&quot;colors&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>,
    <span style="color: #ff0000;">&quot;_links&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;self&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;href&quot;</span>:<span style="color: #ff0000;">&quot;http://fruits/apple&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>
  <span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>  
 <span style="color: #ff0000;">&quot;_links&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #ff0000;">&quot;self&quot;</span>:  <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;href&quot;</span>:<span style="color: #ff0000;">&quot;http://bowls/desk&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>,
  <span style="color: #ff0000;">&quot;fruits&quot;</span>:<span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">&quot;href&quot;</span>:<span style="color: #ff0000;">&quot;http://bowls/desk/fruits&quot;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Yeah! I can see an apple inside the <code>_embedded</code> key, which is HAL&#8217;s way to nest resources in representations. Also, the apple contains a link again pointing to its resource.</p>
<p>It is needless to note that I could go on like this and <span class="caps">POST</span> more fruits to the <code>fruits</code> collection resource in order to enrich my delicious salad.</p>
<h3>What Did I Just Do?</h3>
<p>This brief example was brief. Nevertheless, it demonstrated the use of hypermedia. Here&#8217;s what I did.</p>
<ol>
<li>First, I POSTed to <code>http://bowls</code> to create a bowl.</li>
<li>The returned document points me to its <code>fruits</code> resource.</li>
<li>To add fruits, I <span class="caps">POST</span> to <code>http://bowls/desk/fruits</code> as defined in the representation.</li>
<li>I retrieve the updated bowl by following the <code>self</code> <span class="caps">URL</span> of the bowl.</li>
</ol>
<p>Well, what&#8217;s so special about that?</p>
<p>The key is, I didn&#8217;t compute any <span class="caps">URL</span> except the initial <span class="caps">URL</span> in (1.) &#8211; this is what we call the <strong>single entry point</strong>. No knowledge about follow-up URLs was needed to operate my fruit salad <span class="caps">API</span>. Everything I need to know is included in the representations.</p>
<p>Actions (aka URLs) that are meaningful in the current application state can be found in the document without any need to build URLs myself. This concept is called <strong>HATEOAS</strong> &#8211; a beautiful name&#8230; for a cat.</p>
<h3>Thou Shall Not Make Any Fruit Salad Without Hypermedia!</h3>
<p>Roy Fielding, the inventor of <span class="caps">REST</span>, states that <a href="http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven">your <span class="caps">API</span> is <span class="caps">RESTFUL</span> if and only if it is hypermedia-driven</a>! In my words, that&#8217;ll mean no <span class="caps">URL</span> code should be hard-wired into your <span class="caps">REST</span> client &#8211; except for the single entry point <span class="caps">URL</span>. Any additional resource must be extracted from returned documents. And, hey, this is the very reason why <span class="caps">REST</span> is defined as a style for &#8220;distributed <em>hypermedia</em> systems&#8221;.</p>
<p>How that works with <a href="http://github.com/apotonick/roar">the Roar gem</a> is subject to discussion in my upcoming next post! Stay tuned and enjoy life!</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2012/03/ruby-on-rest-5-learn-hypermedia-by-making-fruit-salad/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Maximum Modularity with Cells and Rails Engines</title>
		<link>http://nicksda.apotomo.de/2012/03/maximum-modularity-with-cells-and-rails-engines/</link>
		<comments>http://nicksda.apotomo.de/2012/03/maximum-modularity-with-cells-and-rails-engines/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 17:14:48 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cells]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=464</guid>
		<description><![CDATA[Do you remember when we were writing a reusable sidebar element some time ago? The sidebar used in many controllers was implemented using a cell which encapsulated both assets and code in one place. Those were good times. When needed, we could render the box with a render_cell call anywhere in our app. #sidebar = [...]]]></description>
			<content:encoded><![CDATA[<p>Do you remember <a href="http://nicksda.apotomo.de/2010/11/lets-write-a-reusable-sidebar-component-in-rails-3/">when we were writing a reusable sidebar element</a> some time ago? The sidebar used in many controllers was implemented using a <a href="https://github.com/apotonick/cells">cell</a> which encapsulated both assets and code in one place. Those were good times.</p>
<p><img src="http://nicksda.apotomo.de/wp-content/uploads/2010/11/ap1.png" alt="" title="The sidebar widget." width="398" height="261" class="alignleft size-full wp-image-787" /></p>
<p>When needed, we could render the box with a <code>render_cell</code> call anywhere in our app.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#sidebar</span>
  = render_cell <span style="color:#ff3333; font-weight:bold;">:posts</span>, <span style="color:#ff3333; font-weight:bold;">:recent</span></pre></div></div>

<h3>Reusability With Engines</h3>
<p>Now, imagine this sidebar box was so universally usable that you wanna use it in another project. Code is usually distributed with gems in a Ruby environment &#8211; but how does that work with partials, helpers, view code?</p>
<p>We got engines in Rails. We got Cells. We got gems. So let&#8217;s pack the cell in an engine gem!</p>
<p><a href="http://railscasts.com/episodes/277-mountable-engines">Engines</a> were designed to distribute controllers, views and models between Rails projects. As an example, lots of the authorization gems around use engines for reusable login pages or password reset forms that can be used right in your project. There was no easy way for reusing partials and behaviour, though, unless you&#8217;re using cells.</p>
<h3>The Cell, Gemified.</h3>
<p>In order to ship the sidebar posts box I first need to create a Rails engine.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ rails plugin new sidebar <span style="color: #660033;">--mountable</span></pre></div></div>

<p>This creates a distributable directory with all the files needed to mount it into a Rails app.</p>
<p>As a second step we need to move the <code>PostsCell</code> into that gem.</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">mv</span> app<span style="color: #000000; font-weight: bold;">/</span>cells<span style="color: #000000; font-weight: bold;">/</span>posts sidebar<span style="color: #000000; font-weight: bold;">/</span>app<span style="color: #000000; font-weight: bold;">/</span>cells<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>You can now include the <code>sidebar</code> gem in any application using the <code>Gemfile</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">gem <span style="color:#996600;">'sidebar'</span>, <span style="color:#ff3333; font-weight:bold;">:path</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'vendor/plugins/sidebar'</span></pre></div></div>

<p>To render your reusable component, just use the <code>render_cell</code> call as you did in the originating application.</p>
<h3>Conclusion</h3>
<p>Damn, this conclusion came fast, but there is really not more to say. Oh, did I mention that you should update to Cells 3.8.3 for full engines support in Rails 3.0, 3.1 and even 3.2?!</p>
<p>Cells + Engines bring real reusability to Rails. It&#8217;s da shit.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2012/03/maximum-modularity-with-cells-and-rails-engines/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ruby on REST 4: Using Representers in Rails</title>
		<link>http://nicksda.apotomo.de/2012/03/ruby-on-rest-4-using-representers-in-rails/</link>
		<comments>http://nicksda.apotomo.de/2012/03/ruby-on-rest-4-using-representers-in-rails/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 18:49:41 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[roar]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=1834</guid>
		<description><![CDATA[It has been a while since I blogged about Roar and it&#8217;s representers &#8211; beautiful South Africa kept me from doing my homework. Nevertheless, in the meantime, the new roar-rails gem emerged bringing together Rails and Roar in a very easy going way. I&#8217;d like to describe how this gem works by implementing a fruit [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a while since I blogged about <a href="https://github.com/apotonick/roar">Roar</a> and it&#8217;s representers &#8211; <a href="http://rubyfuza.org/speakers?page=2">beautiful South Africa</a> kept me from doing my homework. Nevertheless, in the meantime, the new <a href="https://github.com/apotonick/roar-rails">roar-rails gem</a> emerged bringing together Rails and Roar in a very easy going way. I&#8217;d like to describe how this gem works by implementing a fruit and fruit bowl backend service following the <a href="http://nicksda.apotomo.de/2012/01/ruby-on-rest-3-one-model-multiple-representations/">examples from the last posts</a>.</p>
<p>Oh, and before I forget mentioning, the Rails example app <a href="https://github.com/apotonick/ruby-on-rest">is available on github</a> where I will keep adding examples from the <em>Ruby On REST</em> blog series.</p>
<h3>Setting It Up&#8230;</h3>
<p>To make things happen it is necessary adding the roar-rails gem to your <code>Gemfile</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">gem <span style="color:#996600;">'roar-rails'</span></pre></div></div>

<p>As we wanna use Rails&#8217; <span class="caps">URL</span> helpers in our representers we have to add one line to the environment.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">  config.<span style="color:#9900CC;">representer</span>.<span style="color:#9900CC;">default_url_options</span> = 
    <span style="color:#006600; font-weight:bold;">&#123;</span>:host <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;yum.my&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<h3>Rendering Fruits, Done Right.</h3>
<p>For simplicity let&#8217;s assume our <code>Fruit</code> model has the following underlying, highly complex database scheme.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">  create_table <span style="color:#996600;">&quot;fruits&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |t|
    t.<span style="color:#CC0066; font-weight:bold;">string</span>   <span style="color:#996600;">&quot;title&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Tough stuff, isn&#8217;t it?</p>
<p>Clients might want to retrieve fruit representations, that&#8217;s why we start with a functional test for handling <code>GET</code> requests on the <code>FruitsController</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FruitsControllerTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::TestCase</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Rails::TestCase</span>
&nbsp;
  test <span style="color:#996600;">&quot;GET returns fruit document&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    get <span style="color:#ff3333; font-weight:bold;">:show</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:json</span>
    assert_body <span style="color:#996600;">&quot;{<span style="color:#000099;">\&quot;</span>title<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>Apple<span style="color:#000099;">\&quot;</span>,<span style="color:#000099;">\&quot;</span>links<span style="color:#000099;">\&quot;</span>:[
      {<span style="color:#000099;">\&quot;</span>rel<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>self<span style="color:#000099;">\&quot;</span>,
       <span style="color:#000099;">\&quot;</span>href<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>http://yum.my/fruits/Apple<span style="color:#000099;">\&quot;</span>}]}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Including the <code>Roar::Rails::TestCase</code> module into the test gives us a couple of new or changed methods. One of those is <code>assert_body</code> and all it does it testing equality of the passed string and the last returned document. This test verifies our <span class="caps">REST</span> representations are rendered correctly.</p>
<p>Here&#8217;s the handling code in the <code>FruitsController</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FruitsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Rails::ControllerAdditions</span>
  respond_to <span style="color:#ff3333; font-weight:bold;">:json</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> show
    fruit = Fruit.<span style="color:#9900CC;">find_by_id</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    respond_with fruit
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This is all very familiar code! But, wait&#8230; what happens here behind the scenes? The inclusion of the <code>ControllerAdditions</code> module activates a new responder which will take care of the JSON-rendering that is invoked inside <code>respond_with</code>. So, behind the curtain, the following is executed.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">fruit.
  <span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>FruitRepresenter<span style="color:#006600; font-weight:bold;">&#41;</span>.
  <span style="color:#9900CC;">to_json</span></pre></div></div>

<p>The roar-rails gem infers the representer name used for rendering. </p>
<h3>The Fruit Representer</h3>
<p>Now, where is that representer? It lives in the <code>app/representers</code> directory and its implementation is quite simple.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> FruitRepresenter
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON</span>
&nbsp;
  property <span style="color:#ff3333; font-weight:bold;">:title</span>
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:self</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    fruit_url<span style="color:#006600; font-weight:bold;">&#40;</span>title<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>All it does is defining the <code>title</code> property and the <code>self</code> link. Please note that we can simply use <span class="caps">URL</span> helpers here, making it pretty easy to include hypermedia in our representations.</p>
<p>Amazing! This is all the code needed for rendering fruit documents, including hot hypermedia and handling <span class="caps">GET</span> requests. Now, what do we need to consume incoming documents, in, let&#8217;s say a <code>POST</code> request?</p>
<h3>Eat More Fruits!</h3>
<p>To create new fruits in our backend service we&#8217;d need to accept <code>POST</code> requests. Test it.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FruitsControllerTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::TestCase</span>
  <span style="color:#008000; font-style:italic;"># ...</span>
&nbsp;
  test <span style="color:#996600;">&quot;POST creates fruit&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    post <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#996600;">&quot;{<span style="color:#000099;">\&quot;</span>title<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>Orange<span style="color:#000099;">\&quot;</span>}&quot;</span>, 
      <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:json</span>
&nbsp;
    assert_body <span style="color:#996600;">&quot;{<span style="color:#000099;">\&quot;</span>title<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>Orange<span style="color:#000099;">\&quot;</span>,<span style="color:#000099;">\&quot;</span>links<span style="color:#000099;">\&quot;</span>:[
      {<span style="color:#000099;">\&quot;</span>rel<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>self<span style="color:#000099;">\&quot;</span>,
      <span style="color:#000099;">\&quot;</span>href<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>http://yum.my/fruits/Orange<span style="color:#000099;">\&quot;</span>}]}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Again, we use <code>assert_body</code> to assure the resource returns the freshly created representation of the orange. Now, check the <code>post</code>: The second argument is the actual <span class="caps">JSON</span> document sent by the client. This is an <span class="caps">API</span> change. We no longer pass hashes to the &#8220;POST&#8221;, but use real documents to test our services.</p>
<p>The consuming controller action is hilariously simple as well.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FruitsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    fruit = Fruit.<span style="color:#9900CC;">new</span>.
      <span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>FruitRepresenter<span style="color:#006600; font-weight:bold;">&#41;</span>.
      <span style="color:#9900CC;">from_json</span><span style="color:#006600; font-weight:bold;">&#40;</span>request.<span style="color:#9900CC;">body</span>.<span style="color:#CC0066; font-weight:bold;">string</span><span style="color:#006600; font-weight:bold;">&#41;</span>.
      <span style="color:#9900CC;">save</span>
&nbsp;
    respond_with fruit
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It creates a new <code>Fruit</code> instance, mixes in the respective representer, parses the incoming <span class="caps">JSON</span> document and updates the fruit&#8217;s attributes in the <code>#from_json</code> method and then renders the created model.</p>
<p>In the upcoming version of roar-rails, this code can also be written like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FruitsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    fruit = consume<span style="color:#006600; font-weight:bold;">&#40;</span>Fruit<span style="color:#006600; font-weight:bold;">&#41;</span>.
      <span style="color:#9900CC;">save</span>
&nbsp;
    respond_with fruit
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The <code>consume</code> method provided by roar-rails is still under discussion &#8211; feel free to add comments to this post if you have a groundbreaking idea how to simplify the consumption steps!</p>
<h3>Fruits On This Planet &#8211; Unite!</h3>
<p>If that was to easy for you, and I bet it was, why not implement the fruit bowl as well. Consider this model class.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Bowl <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:fruits</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Instead of wasting time with the rendering test and parsing code &#8211; which is identical to the fruit code &#8211; we jump directly into the parsing test.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> BowlsControllerTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::TestCase</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Rails::TestCase</span>
&nbsp;
  test <span style="color:#996600;">&quot;POST creates bowl with fruits&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    post <span style="color:#ff3333; font-weight:bold;">:create</span>, 
      <span style="color:#996600;">&quot;{<span style="color:#000099;">\&quot;</span>fruits<span style="color:#000099;">\&quot;</span>:[{<span style="color:#000099;">\&quot;</span>title<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>Orange<span style="color:#000099;">\&quot;</span>}]}&quot;</span>, 
      <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:json</span>
&nbsp;
    bowl = Bowl.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:last</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_body <span style="color:#996600;">&quot;{<span style="color:#000099;">\&quot;</span>fruits<span style="color:#000099;">\&quot;</span>:[
      {<span style="color:#000099;">\&quot;</span>title<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>Orange<span style="color:#000099;">\&quot;</span>,<span style="color:#000099;">\&quot;</span>links<span style="color:#000099;">\&quot;</span>:[
        {<span style="color:#000099;">\&quot;</span>rel<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>self<span style="color:#000099;">\&quot;</span>,
        <span style="color:#000099;">\&quot;</span>href<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>http://yum.my/fruits/Orange<span style="color:#000099;">\&quot;</span>}]}],
      <span style="color:#000099;">\&quot;</span>links<span style="color:#000099;">\&quot;</span>:[
        {<span style="color:#000099;">\&quot;</span>rel<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>self<span style="color:#000099;">\&quot;</span>,
        <span style="color:#000099;">\&quot;</span>href<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>http://bowls/#{bowl.id}<span style="color:#000099;">\&quot;</span>}]}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Sometimes I wish my blog was less narrow.</p>
<p>In the test, an initial bowl document is <code>POST</code>ed to the resource. We expect a fresh bowl representation containing fruits as the response.</p>
<p>Let&#8217;s look at the controller action to understand how representers work in a nested setup. Speaking of nesting, here&#8217;s the representer responsible for rendering and parsing bowls.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> BowlRepresenter
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON</span>
&nbsp;
  property <span style="color:#ff3333; font-weight:bold;">:location</span>
&nbsp;
  collection <span style="color:#ff3333; font-weight:bold;">:fruits</span>, 
    <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Fruit, 
    <span style="color:#ff3333; font-weight:bold;">:extend</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> FruitRepresenter
&nbsp;
  link <span style="color:#ff3333; font-weight:bold;">:self</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    bowl_url<span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In addition to the <code>location</code> property the collection defines a nesting <code>fruits</code>. The <code>BowlRepresenter</code> now knows that the collection contains <code>Fruit</code> instances that must be represented with the according <code>FruitRepresenter</code>. We already <a href="http://nicksda.apotomo.de/2011/12/ruby-on-rest-2-representers-and-the-dci-pattern/">discussed that in one of the last posts</a>.</p>
<p>Here&#8217;s the controller action using that representer.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> BowlsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#008000; font-style:italic;"># ...</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    bowl = consume<span style="color:#006600; font-weight:bold;">&#40;</span>Bowl<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># puts bowl.fruits.inspect</span>
    bowl.<span style="color:#9900CC;">fruits</span> = bowl.<span style="color:#9900CC;">fruits</span>.<span style="color:#9900CC;">collect</span> <span style="color:#9966CC; font-weight:bold;">do</span> |f|  
      Fruit.<span style="color:#9900CC;">find_by_title</span><span style="color:#006600; font-weight:bold;">&#40;</span>f.<span style="color:#9900CC;">title</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    bowl.<span style="color:#9900CC;">save</span>
&nbsp;
    respond_with bowl
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>I simply assume the <code>consume</code> call already works. Then, what is that loop? Well, when parsing the incoming document, the bowl representer creates a new <code>Fruit</code> instance for every fruit in the collection. If line 7 would be uncommented, it would output the following.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#008000; font-style:italic;">#&lt;Fruit id: nil, title: &quot;Orange&quot;&gt;]</span></pre></div></div>

<p>That is, before we reset the collection manually. The representer doesn&#8217;t know anything about the database &#8211; it just maps a fruit representation in the document to a fresh <code>Fruit</code> instance. This is why there is no id set, yet. We have to do that ourselves (lines 8-10). What might look clumsy to you is simplicity &#8211; it&#8217;s up to you how to map objects to database records.</p>
<h3>Conclusion &#8211; For Now?!</h3>
<p>Come on, it <em>is</em> easy to use representers in your Rails backends using the <a href="https://github.com/apotonick/roar-rails">roar-rails gem</a>. It helps you rendering documents and makes it quite easy to have object-oriented access to a parsed incoming representation &#8211; under Rails-conditions.</p>
<p>And, as always, this is just a start. Open source lives from discussion and criticism, so feel free to use the comments form below. In the next post I&#8217;ll discuss writing paginated collections using representers. Have a nice day!</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2012/03/ruby-on-rest-4-using-representers-in-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby On REST 3: One Model, Multiple Representations!</title>
		<link>http://nicksda.apotomo.de/2012/01/ruby-on-rest-3-one-model-multiple-representations/</link>
		<comments>http://nicksda.apotomo.de/2012/01/ruby-on-rest-3-one-model-multiple-representations/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 15:34:11 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[representation]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[roar]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=1804</guid>
		<description><![CDATA[Happy New Year to all my fellow readers! I hope you had a good party! In this post I wanna clarify why representers in Roar are much more versatile than one might think at first sight. Model != Resource I know, Rails teaches us that every resource is a model with exactly one representation. This [...]]]></description>
			<content:encoded><![CDATA[<p>Happy New Year to all my fellow readers! I hope you had a good party!</p>
<p>In this post I wanna clarify why <a href="https://github.com/apotonick/roar">representers in Roar</a> are much more versatile <a href="http://nicksda.apotomo.de/2011/12/ruby-on-rest-introducing-the-representer-pattern/#comment-5346">than one might think at first sight</a>.</p>
<h3>Model != Resource</h3>
<p>I know, Rails teaches us that every resource is a model with exactly one representation. This is wrong. Not only can a resource be just anything, ranging from simple 1-to-1 mappings of database rows to virtual models like a workflow but also can your business models be represented in completely different <em>contexts</em>.</p>
<p>Let&#8217;s get back to our fruity example to learn more about this.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">orange = Fruit.<span style="color:#9900CC;">find_by_title</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Orange&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
orange.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>FruitRepresenter<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_json</span>
<span style="color:#008000; font-style:italic;">#=&gt; {&quot;title&quot;:&quot;Orange&quot;,&quot;colors&quot;:[&quot;orange&quot;]}</span></pre></div></div>

<p><a href="http://nicksda.apotomo.de/2011/12/ruby-on-rest-introducing-the-representer-pattern">In the last discussion</a> we had a <code>Fruit</code> and a <code>Bowl</code> model which were represented in their respective resources. While the fruit just contains some properties the bowl was a composition thereof. This is pretty &#8220;rails-conform&#8221; (I use this word with a negative undertone) and can simply be abstracted to hundreds of projects where people expose users, blog posts or events in their resources. This is what we call a <strong>1-to-1 mapping of model to resource</strong>.</p>
<h3>Let&#8217;s have some Booze!</h3>
<p>We wrote a simple <code>FruitRepresenter</code> for mapping apples, lemons or oranges to documents, and we had the <code>BowlRepresenter</code> to provide documents for fruit collections. That was nice. But you can do more with fruits. Why not open a distillery and make some good booze from pears or grapes?</p>
<p>What that means for our <span class="caps">REST</span> <span class="caps">API</span> is that we have to represent fruits as ingredients. We also want to document what fruits are contained in a bottle of schnaps.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> IngredientRepresenter
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON</span>
&nbsp;
  property <span style="color:#ff3333; font-weight:bold;">:title</span>
  collection <span style="color:#ff3333; font-weight:bold;">:tastes_like</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The new <code>IngredientRepresenter</code> looks a bit similar to our <code>FruitRepresenter</code>, however, it <em>represents</em> a fruit in another <em>context</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">pear = Fruit.<span style="color:#9900CC;">find_by_title</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Pear&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
pear.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>IngredientRepresenter<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_json</span>
<span style="color:#008000; font-style:italic;">#=&gt; undefined method `tastes_like'</span></pre></div></div>

<p>An exception. While the pear exposes a <code>#title</code> method it does not have a <code>#tastes_like</code> method. Why did I do that? I wanted to emphasize that the representer doesn&#8217;t guess anything from the represented object (the pear). It is up to the fruit to provide decent accessors like the <code>#tastes_like</code> reader. No magic or implicit semantics in Roar. And this is a good thing.</p>
<h3>Another Context, Another Module.</h3>
<p>We could easily push the missing methods into the <code>IngredientRepresenter</code> itself, but we will learn in an upcoming post that it is better to provide additional accessors in a separate module.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Ingredient
  <span style="color:#9966CC; font-weight:bold;">def</span> tastes_like
    TastesLike.<span style="color:#9900CC;">query</span><span style="color:#006600; font-weight:bold;">&#40;</span>title<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># returns array.</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> tastes_like=<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In the reader, we query the world-famous TastesLike™ web service. Since we don&#8217;t need a writer, the second method ain&#8217;t doing nothing.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">pear.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>Ingredient, IngredientRepresenter<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_json</span>
<span style="color:#008000; font-style:italic;">#=&gt; {&quot;title&quot;:&quot;Pear&quot;,&quot;tastes_like&quot;:[&quot;pear&quot;,&quot;mango&quot;]}</span></pre></div></div>

<p>Cool, this is a fruit represented in a completely different context. What about the parsing, does that work, too?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">apple = Fruit.<span style="color:#9900CC;">new</span>
apple.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>Ingredient, IngredientRepresenter<span style="color:#006600; font-weight:bold;">&#41;</span>
apple.<span style="color:#9900CC;">from_json</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;{<span style="color:#000099;">\&quot;</span>title<span style="color:#000099;">\&quot;</span>:<span style="color:#000099;">\&quot;</span>Apple<span style="color:#000099;">\&quot;</span>,
  <span style="color:#000099;">\&quot;</span>tastes_like<span style="color:#000099;">\&quot;</span>:[<span style="color:#000099;">\&quot;</span>Pear<span style="color:#000099;">\&quot;</span>,<span style="color:#000099;">\&quot;</span>Apple<span style="color:#000099;">\&quot;</span>,<span style="color:#000099;">\&quot;</span>Mango<span style="color:#000099;">\&quot;</span>]}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; #&lt;Fruit title=&quot;Apple&quot;, colors=[]&gt;</span></pre></div></div>

<p>Yepp, parsing an ingredient and mapping it to a fruit also works fine.</p>
<h3>Fruits in a Bottle.</h3>
<p>Let&#8217;s see how collections can be represented in different contexts.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> BottleRepresenter
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Roar::Representer::JSON</span>
&nbsp;
  collection <span style="color:#ff3333; font-weight:bold;">:fruits</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Fruit, 
    <span style="color:#ff3333; font-weight:bold;">:extend</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span>Ingredient, IngredientRepresenter<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The bottle representer <em>knows</em> that it&#8217;s composed of <code>Fruit</code> instances. It does use the <code>IngredientRepresenter</code> for parsing and rendering, though. <span class="caps">BTW</span>, if you don&#8217;t like the long <code>:extend</code> assignment you are free to &#8220;hard-include&#8221; the modules directly in your models.</p>
<h3>Summary</h3>
<p>So what we&#8217;re doing here is having one model used in two completely different contexts: the conventional &#8220;fruit&#8221; context and the &#8220;ingredient&#8221; context. Imagine you&#8217;d be doing this with <code>#as_json</code> and different hash-parsing algorithms in your controllers &#8211; good luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2012/01/ruby-on-rest-3-one-model-multiple-representations/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Filters For Your Cells with cells-filters</title>
		<link>http://nicksda.apotomo.de/2012/01/filters-for-your-cells-with-cells-filters/</link>
		<comments>http://nicksda.apotomo.de/2012/01/filters-for-your-cells-with-cells-filters/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 21:35:27 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cells]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=1807</guid>
		<description><![CDATA[There&#8217;s nothing like giving a quick update about Cells on a rainy sunday evening. Almost a year ago we were happy to introduce state-args into Cells. State-args help to prevent saving state in instance variables and instead pass variables from #render_cell as arguments, making your code more explicit. One big problem was that filters couldn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s nothing like giving a quick update about <a href="https://github.com/apotonick/cells">Cells</a> on a rainy sunday evening. Almost a year ago we were happy <a href="http://nicksda.apotomo.de/2011/02/cells-3-5-release-party-summary/">to introduce state-args into Cells</a>. State-args help to prevent saving state in instance variables and instead pass variables from <code>#render_cell</code> as arguments, making your code more explicit.</p>
<p>One big problem was that filters couldn&#8217;t take advantage of that feature, thanks to a design flaw in Rails&#8217; callback implementation.</p>
<h3>Example?</h3>
<p>Consider the following call to render a freaky bassist cell.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">render_cell <span style="color:#ff3333; font-weight:bold;">:bassist</span>, <span style="color:#ff3333; font-weight:bold;">:play</span>, <span style="color:#996600;">&quot;C-sharp&quot;</span>, <span style="color:#996600;">&quot;4/4&quot;</span></pre></div></div>

<p>The cell class wants to process the incoming data in a <code>before_filter</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> BassistCell <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Cell::Rails</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Cell::Filters</span>
&nbsp;
  before_filter <span style="color:#ff3333; font-weight:bold;">:prepare</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> prepare<span style="color:#006600; font-weight:bold;">&#40;</span>state, tone, timing<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@before</span> = <span style="color:#996600;">&quot;In #{tone} and #{timing}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>What now works with the new <a href="https://github.com/apotonick/cells-filters">cells-filters</a> gem is that filters (here, a before filter) can receive the state-args. The implementation is very simple using the <a href="https://github.com/apotonick/hooks">hooks gem</a> for easy peasy hooks and callbacks in Ruby.</p>
<p>However, we introduce a dependency to another gem, that&#8217;s why cells-filters was released as a separate gem. Go and try it! We might need to add some more behaviour to it, but please don&#8217;t ask me to completely bloat it.</p>
<p>You can still use the old-school filters from the Rails <code>Callbacks</code> module but you won&#8217;t be able to retrieve state-args. I don&#8217;t feel like fixing this in the Rails core itself as <a href="https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/callbacks.rb#L145">the implementation is a bit too complex</a> for me.</p>
<h3>Cells Development and a Roadmap.</h3>
<p>Dudes, Cells is getting more and more of a standard in Rails development. In the last year, 2011, we had almost <a href="http://gemcutter.org/gems/cells">90.000 downloads</a> &#8211; this is awesome! Thank you so much.</p>
<p>We have more plans with Cells for 2012, here&#8217;s a short summary what we&#8217;re planning.</p>
<ul>
<li>Get rid of the Rails dependency at all. We <a href="http://nicksda.apotomo.de/2011/12/mounting-a-cell-to-a-route-with-cells-3-8/">already got <code>Cell::Base</code> to use cells without a controller</a>. Now we want to go one step further and make cells completely decoupled from Rails. Imagine cells (and, numerous &#8220;helper&#8221; gems like formtastic) being renderable in Sinatra or without a framework at all.</li>
<li>Use <a href="https://github.com/rtomayko/tilt">tilt</a> as an alternative rendering engine. The Rails rendering layer is outdated and should use an abstracting rendering gem like tilt &#8211; until we can achieve this in the core, we will provide this in cells. That&#8217;ll make huge parts of cells completely independent from Rails.</li>
</ul>
<ul>
<li>Screencasts! Yeah, I&#8217;m working on a screencast series about cells, best practices and interesting solutions users sent in over the recent years.</li>
</ul>
<p>Beside that, we&#8217;re permanently trying to incorporate the cells architecture into the Rails core. Let&#8217;s see where this will lead to in Rails 4.0. Happy new year, and&#8230; Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2012/01/filters-for-your-cells-with-cells-filters/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Mounting a Cell to a Route with Cells 3.8.</title>
		<link>http://nicksda.apotomo.de/2011/12/mounting-a-cell-to-a-route-with-cells-3-8/</link>
		<comments>http://nicksda.apotomo.de/2011/12/mounting-a-cell-to-a-route-with-cells-3-8/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 12:47:17 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cells]]></category>
		<category><![CDATA[rack]]></category>

		<guid isPermaLink="false">http://nicksda.apotomo.de/?p=1788</guid>
		<description><![CDATA[Those of you having asked for mountable cells will be happy with the recent 3.8.0 release! We entirely removed the ActionController dependency. That essentially means you can have cells outside of the standard Rails stack. Cool, heh? Cells 3.8.0 Released Two little things in 3.8 changed. Cell::Base and Cell::Rails are now different classes with different [...]]]></description>
			<content:encoded><![CDATA[<p>Those of you <a href="https://github.com/apotonick/cells/issues/15">having asked for mountable cells</a> will be happy with the recent <a href="https://rubygems.org/gems/cells/versions/3.8.0">3.8.0 release</a>! We entirely removed the ActionController dependency. That essentially means you can have cells outside of the standard Rails stack. Cool, heh?</p>
<h3>Cells 3.8.0 Released</h3>
<p>Two little things in 3.8 changed. <b><code>Cell::Base</code> and <code>Cell::Rails</code> are now different classes</b> with different semantics. Don&#8217;t panic &#8211; if you still derive your cells from <code>Cell::Rails</code> and use the <code>#render_cell</code> interface, only, nothing will change for you.</p>
<p>However, we had to tweak the <span class="caps">API</span> of some semi-public methods, here&#8217;s the new signatures.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Cell::Rails</span>.<span style="color:#9900CC;">create_cell_for</span><span style="color:#006600; font-weight:bold;">&#40;</span>name, controller<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#6666ff; font-weight:bold;">Cell::Rails</span>.<span style="color:#9900CC;">render_cell_for</span><span style="color:#006600; font-weight:bold;">&#40;</span>name, state, controller, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Well, nothing serious, it&#8217;s just the controller sliding to the end. If you use this anywhere, be sure to change it.</p>
<h3>What&#8217;s new?</h3>
<p>What is new is that <b><code>Cell::Base</code> is now decoupled from the ActionController</b>. There simply is no AC-reference in <code>Base</code> anymore. However, this reference is still managed in <code>Cell::Rails</code> on purpose:</p>
<ul>
<li>Calls to <code>#url_for</code> and friends automatically query the parent controller for host and action name. This makes it easy to embedd cells into a controller view and still having correct links in the cell.</li>
<li>Having the AC reference, people can still use request, params and session &#8211; if they need it. We strongly discourage accessing <span class="caps">HTTP</span> properties from a view component, though.</li>
</ul>
<p>Let&#8217;s see what the new decoupled <code>Base</code> brings us.</p>
<h3>Cells on Routes.</h3>
<p>Deriving your cell from <code>Base</code> makes it <em>routeable</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> PostsCell <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Cell::Base</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> show
    <span style="color:#0066ff; font-weight:bold;">@posts</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    render
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Routeable? Mountable? Well, let&#8217;s look at <code>config/routes.rb</code> to understand this.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">match <span style="color:#996600;">&quot;/posts&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |env|
  content = 
    <span style="color:#6666ff; font-weight:bold;">Cell::Base</span>.<span style="color:#9900CC;">render_cell_for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:posts</span>, <span style="color:#ff3333; font-weight:bold;">:show</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#006666;">200</span>, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span> content <span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>How cool is that? You can mount a cell to a route without the ActionController overhead and still use the rendering, the caching and testing you all love from Cells. And, hey, <strong>it&#8217;s super super fast now.</strong></p>
<p>The lack of the AC dependency makes cells even more versatile.</p>
<h3>More on Routing</h3>
<p>Since we got rid of the AC instance we have to take care of our URLs ourself. The cell still has access to all the <span class="caps">URL</span> helpers. Nevertheless, those helpers usually query the controller for host name portions, etc. That&#8217;s actually very simple with cells, too.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> PostsCell <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Cell::Base</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> default_url_options
    <span style="color:#006600; font-weight:bold;">&#123;</span>:host <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;apotomo.de&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Just override the <code>#default_url_options</code> method as used from AC. This allows using the <span class="caps">URL</span> helpers as usual.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">PostsCell.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">posts_url</span>
<span style="color:#008000; font-style:italic;">#=&gt; &quot;http://apotomo.de/posts&quot;</span></pre></div></div>

<p>Let me know how these changes work for you! I&#8217;m eager to see new cases of applications in the next weeks.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicksda.apotomo.de/2011/12/mounting-a-cell-to-a-route-with-cells-3-8/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

