<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Apotomo Cookbook</title>
	<atom:link href="http://apotomo.de/feed/" rel="self" type="application/rss+xml" />
	<link>http://apotomo.de</link>
	<description>Stateful widgets for Rails.</description>
	<pubDate>Sat, 17 Apr 2010 13:16:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Writing an interactive form (Part 4)</title>
		<link>http://apotomo.de/2010/04/writing-an-interactive-form-part-4/</link>
		<comments>http://apotomo.de/2010/04/writing-an-interactive-form-part-4/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 13:12:13 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[Your 12 Minutes of Apotomo]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=676</guid>
		<description><![CDATA[I&#8217;m really considering shooting screencasts, blogging is tedious. I could talk stupid things and upload. How&#8217;d you like that?

Ok, last week we had a working app with a list widget showing the items. Today we&#8217;re gonna write a form widget to add new items. The code for this lesson is at tag 0.7.
1First thing to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m really considering shooting screencasts, blogging is tedious. I could talk stupid things and upload. How&#8217;d you like that?</p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/04/app-with-form.png"><img src="http://apotomo.de/wp-content/uploads/2010/04/app-with-form-150x150.png" alt="app-with-form" title="app-with-form" width="150" height="150" class="alignleft size-thumbnail wp-image-680" /></a></p>
<p>Ok, last week we had a working app with a list widget showing the items. Today we&#8217;re gonna w<strong>rite a form widget</strong> to add new items. The code for this lesson is at tag <a href="http://github.com/apotonick/dumdidoo/tree/0.7">0.7</a>.</p>
<p><i>1</i>First thing to do is generating stubs.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ script<span style="color: #000000; font-weight: bold; ">/</span>generate widget item_form display create --haml
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>item_form
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>item_form.rb
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>item_form<span style="color: #000000; font-weight: bold; ">/</span>display.html.haml
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>item_form<span style="color: #000000; font-weight: bold; ">/</span>create.html.haml</pre></div></div>

<p>The <code>app/cells/item_form.rb</code> file looks like this, after some editing.</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"><span style="color: #7A1775; font-weight: bold;">class</span> ItemForm <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">Apotomo::StatefulWidget</span>
  <span style="color: #7A1775; font-weight: bold;">def</span> display
    <span style="color: #CF28C6; ">@todo</span> = Todo.<span style="color: #CF28C6; ">new</span>
    render
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>I create a new <code>Todo</code> so I can use it in the view.</p>
<p><i>2</i>The view needs to be implemented, too!</p>
<p><code>app/cells/item_form/display.html.haml</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre>%h3 Create new item
&nbsp;
= error_messages_for :todo
&nbsp;
- form_to_event(:submit) do
  = text_area :todo, :text, :rows =&gt; 2
  = submit_tag &quot;Create Todo!&quot;</pre></td></tr></table></div>

<p>Easy. The only interesting here is the call to <code>#form_to_event</code><br />
 which basically <strong>triggers</strong> a <code>:submit</code> event when the form is, nah, submitted (line 5).</p>
<p><i>3</i><br />
Let&#8217;s append the form widget to our widget tree in <code>app/controllers/dashboard_controller.rb</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> DashboardController <span style="color: #000000; font-weight: bold; ">&lt;</span> ApplicationController
  <span style="color: #7A1775; font-weight: bold;">include</span> <span style="color: #CF28C6; ">Apotomo::ControllerMethods</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> index
    use_widgets <span style="color: #7A1775; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold; ">|</span>root<span style="color: #000000; font-weight: bold; ">|</span>
      root <span style="color: #000000; font-weight: bold; ">&lt;&lt;</span> widget<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:item_list</span>, <span style="color: #363636; ">'dashboard_list'</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
      root <span style="color: #000000; font-weight: bold; ">&lt;&lt;</span> widget<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:item_form</span>, <span style="color: #363636; ">'new_item'</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
    <span style="color: #7A1775; font-weight: bold;">end</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>I call the widget <code>new_item</code> (line 7).</p>
<p><i>4</i>Pluggin&#8217; the widget into the controller again happens in <code>app/views/dashboard/index.html.erb</code> (line 10 and 12).</p>

<div class="wp_syntax"><div class="code"><pre>&lt;h2&gt;Dashboard&lt;/h2&gt;
&nbsp;
&lt;div class=&quot;slot&quot;&gt;
  &lt;%= render_widget 'dashboard_list' %&gt;
&lt;/div&gt;
&nbsp;
&lt;div class=&quot;slot&quot;&gt;
  &lt;%= render_widget 'new_item' %&gt;
&lt;/div&gt;</pre></div></div>

<p><i>5</i>In order to process the form, we need to observe that <code>:submit</code> event, so I will extend the widget class a bit.</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
</pre></td><td class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> ItemForm <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">Apotomo::StatefulWidget</span>
  <span style="color: #7A1775; font-weight: bold;">def</span> display
    respond_to_event <span style="color: #4d4d4d; ">:submit</span>, <span style="color: #4d4d4d; ">:with</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #4d4d4d; ">:create</span>
&nbsp;
    <span style="color: #CF28C6; ">@todo</span> = Todo.<span style="color: #CF28C6; ">new</span>
    render
  <span style="color: #7A1775; font-weight: bold;">end</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> create
    <span style="color: #CF28C6; ">@todo</span>.<span style="color: #CF28C6; ">update_attributes</span><span style="color: #000000; font-weight: bold; ">&#40;</span>param<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:todo</span><span style="color: #000000; font-weight: bold; ">&#41;</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
&nbsp;
    render <span style="color: #4d4d4d; ">:view</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #4d4d4d; ">:display</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>The event handler (line 3) will execute the <code>#create</code> method when the form is submitted.</p>
<p>Nothing unusual happens there, I update the <code>@todo</code> and instruct the widget to render the former view <code>display.html.haml</code>.</p>
<p><i>6</i>Entering some text into the form and pressing Submit yields the expected result! The new item gets saved.</p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/04/app-with-form.png"><img src="http://apotomo.de/wp-content/uploads/2010/04/app-with-form-150x150.png" alt="app-with-form" title="app-with-form" width="150" height="150" class="alignleft size-thumbnail wp-image-680" /></a></p>
<p>However, the list doesn&#8217;t update so we can not see what we did. We should implement some notification so the list updates when there was a new item created.</p>
<p>That&#8217;s what we&#8217;re gonna do in the next lesson, the 12 minutes are over!</p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/04/writing-an-interactive-form-part-4/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using Cells View Inheritance to clean up your views</title>
		<link>http://apotomo.de/2010/04/using-cells-view-inheritance-to-clean-up-your-views/</link>
		<comments>http://apotomo.de/2010/04/using-cells-view-inheritance-to-clean-up-your-views/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 19:46:08 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[Cells]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=635</guid>
		<description><![CDATA[
In every Rails app is some reoccurring element.
In my blog example, this is a small teaser box (to the right!) showing a snippet from the newest post, and a footer section that provides a link to the article.

However. My imaginary blog also has a page about RubyFlow where I&#8217;d list the links I posted there, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://apotomo.de/wp-content/uploads/2010/04/teaser-blog.png"><img src="http://apotomo.de/wp-content/uploads/2010/04/teaser-blog-300x153.png" alt="To the right!" title="To the right!" width="200" class="alignleft size-medium wp-image-638" /></a></p>
<p>In every Rails app is some reoccurring element.</p>
<p>In my blog example, this is a small teaser box (to the right!) showing a snippet from the newest post, and a footer section that provides a link to the article.</p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/04/teaser-rubyflow.png"><img src="http://apotomo.de/wp-content/uploads/2010/04/teaser-rubyflow-300x151.png" alt="The teaser on my RubyFlow portal page." title="The teaser on my RubyFlow portal page." width="200" class="alignleft size-medium wp-image-641" /></a></p>
<p>However. My imaginary blog also has a page about RubyFlow where I&#8217;d list the links I posted there, just to proof how cool I am. </p>
<p>On the RubyFlow page, I don&#8217;t want the teaser to show a link to the article, but a <strong>red button to RubyFlow</strong>. Of course, this doesn&#8217;t make sense at all.</p>
<p>My god, I hate that red in combination with the pink on this site.</p>
<h3>The medieval if-cascade</h3>
<p>Things like that are usually implemented in the partial with some if/else part.</p>

<div class="wp_syntax"><div class="code"><pre>if @current_page == 'rubyflow'
  Don't read it, better go to rubyflow
else
  = link_to &quot;Read it!&quot;, &quot;/blog/read/#{@post.id}&quot;</pre></div></div>

<p>Pretty ugly.</p>
<p>Things start getting really ugly when there are more conditions to respect and end up in a <a href="http://blog.hma-info.de/2008/03/20/if-cascade/">medieval if-cascade</a>. It&#8217;s geting even worse when those <em>deciders</em> are copied and spread across multiple files.</p>
<h3>How we do it</h3>
<p>Cells brings back the power of OOP to your views. It offers a feature called View Inheritance that saves us here. I will briefly discuss that while showing you how I implemented the different teasers.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ script<span style="color: #000000; font-weight: bold; ">/</span>generate cell teaser box body footer --haml
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>teaser_cell.rb
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>teaser<span style="color: #000000; font-weight: bold; ">/</span>box.html.haml   
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>teaser<span style="color: #000000; font-weight: bold; ">/</span>body.html.haml
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>teaser<span style="color: #000000; font-weight: bold; ">/</span>footer.html.haml</pre></div></div>

<p>The <code>TeaserCell</code> has three states that don&#8217;t do much right now.</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
</pre></td><td class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> TeaserCell <span style="color: #000000; font-weight: bold; ">&lt;</span> ::<span style="color: #CF28C6; ">Cell::Base</span>
  <span style="color: #7A1775; font-weight: bold;">def</span> box
    <span style="color: #CF28C6; ">@post</span> = Post.<span style="color: #CF28C6; ">find</span> <span style="color: #4d4d4d; ">:first</span>
    render
  <span style="color: #7A1775; font-weight: bold;">end</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> body
    render
  <span style="color: #7A1775; font-weight: bold;">end</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> footer
    render
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>Just grabbing the newest post in line 3.</p>
<h3>Nesting states</h3>
<p>I use the <code>box</code> state as a container. It renders the body and footer in its view <code>app/cells/teaser/box.html.haml</code>.</p>

<div class="wp_syntax"><div class="code"><pre>#teaser
  %h3
    Nick says
&nbsp;
  .body
    = render :state =&gt; :body
  .footer
    = render :state =&gt; :footer</pre></div></div>

<p>That&#8217;s right, I use <code>render :state</code> in a cell view. That&#8217;s somehow comparable to rendering a partial, although it first executes the <code>#body</code> method, which in turn renders its view.</p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/04/teaser-with-generic-body-and-footer.png"><img src="http://apotomo.de/wp-content/uploads/2010/04/teaser-with-generic-body-and-footer-150x150.png" alt="teaser-with-generic-body-and-footer" title="teaser-with-generic-body-and-footer" width="150" height="150" class="alignleft size-thumbnail wp-image-656" /></a></p>
<p>That&#8217;s how the teaser currently looks like as we didn&#8217;t customize the body and footer view, yet.</p>
<p>It is pretty obvious that our body view should print out the teaser text, whereas the footer displays the link.</p>
<p><code>app/cells/teaser/body.html.haml</code></p>

<div class="wp_syntax"><div class="code"><pre>%h2 #{@post.title}
%p  #{@post.body}</pre></div></div>

<p>And the footer in <code>app/cells/teaser/footer.html.haml</code>.</p>

<div class="wp_syntax"><div class="code"><pre>= link_to &quot;Read it!&quot;, &quot;/blog/read/#{@post.id}&quot;</pre></div></div>

<h3>Enriching the controller</h3>
<p>In the blog view I then call <code>#render_cell</code> to integrate the teaser in my blog ERB page.</p>

<div class="wp_syntax"><div class="code"><pre>&lt;div id=&quot;right_menu&quot;&gt;
  &lt;%= render_cell :teaser, :box %&gt;
&lt;/div&gt;</pre></div></div>

<p>Ok, so now we got a beautiful teaser in the blog page. How do we get that teaser to show a red button on the RubyFlow page?</p>
<h3>Inheritance strikes again!</h3>
<p>The answer is - unsurprisingly: we <em>derive</em> a new <code>RubyflowTeaserCell</code> from the <code>TeaserCell</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ script<span style="color: #000000; font-weight: bold; ">/</span>generate cell rubyflow_teaser footer --haml
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>rubyflow_teaser_cell.rb
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>rubyflow_teaser<span style="color: #000000; font-weight: bold; ">/</span>footer.html.haml</pre></div></div>

<p>The actual inheritance happens in the class.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> RubyflowTeaserCell <span style="color: #000000; font-weight: bold; ">&lt;</span> TeaserCell
<span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<p>Note that we don&#8217;t need any methods as they all come from <code>TeaserCell</code>. The same applies to the views, they&#8217;re <strong>inherited</strong> as well!</p>
<p>But wait, we planned on overwriting the footer so here&#8217;s how the <code>app/cells/rubyflow_teaser/footer.html.haml</code> looks like.</p>

<div class="wp_syntax"><div class="code"><pre>Don't read it, better go to
%span rubyflow</pre></div></div>

<p>In the Rubyflow controller view I add the cells rendering.</p>

<div class="wp_syntax"><div class="code"><pre>&lt;div id=&quot;right_menu&quot;&gt;
  &lt;%= render_cell :rubyflow_teaser, :box %&gt;
&lt;/div&gt;</pre></div></div>

<p>That&#8217;s a call to <code>RubyflowTeaserCell</code>. Now, what happens here.</p>
<ul>
<li>the <strong>derived</strong> <code>RubyflowTeaserCell</code> calls the <code>#box</code> method which is inherited</li>
<li>as the respective view is not found in <code>app/cells/rubyflow_teaser</code> it <strong>is looked up in the parent directory</strong> <code>app/cells/teaser/</code> and found</li>
<li>the same happens with the <code>:body</code> state</li>
<li>when rendering the footer, the view <strong>is found in the cells own view directory</strong> and thus not inherited</li>
</ul>
<h3>Conclusion</h3>
<p>View inheritance comes into play whenever complex deciders threaten the straightforwardness of your view code. When you encounter if-cascades with more than two <code>if</code>s, think about using a cell with inheritance there.</p>
<h3>Code?</h3>
<p>The example code can be found <a href="http://github.com/apotonick/cells_examples">at github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/04/using-cells-view-inheritance-to-clean-up-your-views/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ActiveHelper + Rails is no pain in the ass!</title>
		<link>http://apotomo.de/2010/04/activehelper-rails-is-no-pain-in-the-ass/</link>
		<comments>http://apotomo.de/2010/04/activehelper-rails-is-no-pain-in-the-ass/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 19:20:17 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[ActiveHelper]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=594</guid>
		<description><![CDATA[Recently (about 6 minutes ago) I added support for Rails in ActiveHelper. The library as-it is still completely independent from any MVC framework and simply provides a generic helper API.
However, we got some Rails bindings now.
If you&#8217;re not yet familiar with ActiveHelper, no problem, it&#8217;s brandnew. Consider skimming the introducing post first. Or directly jump [...]]]></description>
			<content:encoded><![CDATA[<p>Recently (about 6 minutes ago) I added support for Rails in ActiveHelper. The library as-it is still completely independent from any MVC framework and simply provides a <strong>generic helper API</strong>.</p>
<p>However, we got some Rails bindings now.</p>
<p>If you&#8217;re not yet familiar with ActiveHelper, no problem, it&#8217;s brandnew. Consider skimming the <a href="http://apotomo.de/2010/03/activehelper-010-released/">introducing post</a> first. Or directly jump to the <a href="http://github.com/apotonick/active_helper">github repository</a>.</p>
<h3>Writing the helper</h3>
<p>Let&#8217;s assume you want to create a new helper to replace the <code>#link_to</code> method. Why not name it <code>LinkingHelper</code>?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> LinkingHelper <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">ActiveHelper::Base</span>
  provides <span style="color: #4d4d4d; ">:link_for</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> link_for<span style="color: #000000; font-weight: bold; ">&#40;</span>title, url<span style="color: #000000; font-weight: bold; ">&#41;</span>
    <span style="color: #363636; ">'&lt;a href=&quot;'</span> <span style="color: #000000; font-weight: bold; ">+</span> url <span style="color: #000000; font-weight: bold; ">+</span> <span style="color: #363636; ">'&quot;&gt;'</span> <span style="color: #000000; font-weight: bold; ">+</span> title <span style="color: #000000; font-weight: bold; ">+</span> <span style="color: #363636; ">'&lt;/a&gt;'</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<p>You&#8217;d put this into <code>app/active_helpers/linking_helper.rb</code>.</p>
<h3>Preparing the controller</h3>
<p>Bla bla bla, it&#8217;s obvious how to use ActiveHelpers in controllers.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> BlogController <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">ActionController::Base</span>
  active_helper LinkingHelper
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> show
    <span style="color: #CF28C6; ">@post</span> = Post.<span style="color: #CF28C6; ">find</span> params<span style="color: #000000; font-weight: bold; ">&#91;</span><span style="color: #4d4d4d; ">:id</span><span style="color: #000000; font-weight: bold; ">&#93;</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<h3>Helping the view</h3>
<p>As soon as the controller did include the <code>LinkingHelper</code> its <strong>provided methods</strong> are around in the view.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre>Bookmarkable link: &lt;%= link_for @post.title, @post.url %&gt;
&nbsp;
&lt;h1&gt;&lt;%= @post.title %&gt;&lt;/h1&gt;
&lt;p&gt;
  &lt;%= @post.body %&gt;
&lt;/p&gt;</pre></td></tr></table></div>

<p>You used your <code>#link_for</code> to generate a link in line 1.</p>
<h3>So&#8230; where&#8217;s the benefit?</h3>
<p>Being a good programmer you instantly decide to <strong>extract the markup logic</strong> from the <code>LinkingHelper</code> to some separate <code>MarkupHelper</code>.</p>
<p>The latter would provide a method <code>#tag</code> for generating markup.</p>
<p>Your former helper now looks like this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> LinkingHelper <span style="color: #000000; font-weight: bold; ">&lt;</span> MarkupHelper
  provides <span style="color: #4d4d4d; ">:link_for</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> link_for<span style="color: #000000; font-weight: bold; ">&#40;</span>title, url<span style="color: #000000; font-weight: bold; ">&#41;</span>
    tag<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:a</span>, title, <span style="color: #4d4d4d; ">:href</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> url<span style="color: #000000; font-weight: bold; ">&#41;</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>Noticed the inheritance in line 1? Wow- that&#8217;s <strong>OOP features</strong> in helpers. Nice.</p>
<h3>Reusing old-school helpers</h3>
<p>Now what if you still want to use <code>#url_for</code> to setup the link address? It&#8217;s hidden somewhere in Rails gloomy mixin magic.</p>
<p>You know it&#8217;s floating around in your view. So go and use it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> LinkingHelper <span style="color: #000000; font-weight: bold; ">&lt;</span> MarkupHelper
  provides <span style="color: #4d4d4d; ">:link_for</span>
  needs <span style="color: #4d4d4d; ">:url_for</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> link_for<span style="color: #000000; font-weight: bold; ">&#40;</span>title, url<span style="color: #000000; font-weight: bold; ">&#41;</span>
    url = url_for<span style="color: #000000; font-weight: bold; ">&#40;</span>url<span style="color: #000000; font-weight: bold; ">&#41;</span>
    tag<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:a</span>, title, <span style="color: #4d4d4d; ">:href</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> url<span style="color: #000000; font-weight: bold; ">&#41;</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>Step-by-step, dude. You simply use <code>#url_for</code> in line 6. Your helper knows about it since you <strong>defined the dependency</strong> (line 3).</p>
<p>That&#8217;s gonna work- but&#8230; <em>why?</em></p>
<p>Well, any ActiveHelper simply <strong>delegates dependencies</strong> back to where it was imported into. In our case, any call to <code>#url_for</code> is routed to the ActionView instance which has to care about that.</p>
<h3>Delegation for a better world</h3>
<p>The new keywords in the Helper World are <strong>delegation</strong> and <strong>interfaces</strong>.</p>
<p>Besides that, we got back OOP and <strong>inheritance to helpers</strong>.</p>
<p>Lemme end this boring post by quoting Manuel who&#8217;s just diving into ActiveHelpers.</p>
<blockquote><p>It&#8217;s funny- ActiveHelpers is nothing more than 80 lines of code. Anyway, it&#8217;s <em>forcing</em> you to write clean code&#8230; in a gentle way!</p></blockquote>
<p>Isn&#8217;t that great?</p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/04/activehelper-rails-is-no-pain-in-the-ass/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Making unobstrusive pagination (Part 3)</title>
		<link>http://apotomo.de/2010/04/making-unobstrusive-pagination-part-3/</link>
		<comments>http://apotomo.de/2010/04/making-unobstrusive-pagination-part-3/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 13:49:48 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[Your 12 Minutes of Apotomo]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=561</guid>
		<description><![CDATA[In the last lesson I demonstrated how we could quickly write our own pagination using #link_to_event.
A couple of guys (including mislav himself) told me to make the pagination unobstrusive, so here&#8217;s how i did this. 

You&#8217;re gonna learn how to

pack unobstrusive JavaScript in Apotomo widgets
use Helpers in widgets
configure your widget view with #render

1I didn&#8217;t have [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://apotomo.de/2010/03/what-about-pagination-part-2/">last lesson</a> I demonstrated how we could quickly write our own pagination using <code>#link_to_event</code>.</p>
<p>A couple of guys (including mislav himself) told me to make the pagination <strong>unobstrusive</strong>, so here&#8217;s how i did this. </p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/03/simple_paging.jpg"><img src="http://apotomo.de/wp-content/uploads/2010/03/simple_paging-150x150.jpg" alt="simple_paging" title="simple_paging" width="150" height="150" class="alignleft size-thumbnail wp-image-475" /></a></p>
<p>You&#8217;re gonna learn how to</p>
<ul>
<li>pack unobstrusive JavaScript in Apotomo widgets</li>
<li>use Helpers in widgets</li>
<li>configure your widget view with <code>#render</code></li>
</ul>
<p><i>1</i>I didn&#8217;t have any clue what <em>unobstrusive JavaScript</em> refers to. It means &#8220;separating JavaScript and markup&#8221;. After lots of tries, I figured out how to let <code>will_paginate</code> handle the markup and do the JS part myself.</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
16
17
18
19
20
21
</pre></td><td class="code"><pre>%h3 Things
%p
  = page_entries_info @items
&nbsp;
%ul
  - @items.each do |item|
    %li
      = item.text
&nbsp;
= will_paginate @items
&nbsp;
- javascript_tag do
  $(document.body).observe('click', function(e) {
  var el = e.element()
&nbsp;
  if (el.match('.pagination a')) {
  page = el.href.split(&quot;page=&quot;).last(); // I don't know how to do it better.
  = &quot;new Ajax.Request('#{url_for_event(:paginate)}'+'&amp;page='+page, { method: 'get' })&quot;
&nbsp;
  e.stop()
  }})</pre></td></tr></table></div>

<p>Instead of modifying <code>will_paginate'</code>s output  (line 10) I add an observer that triggers the <code>:pagination</code> event (line 13).</p>
<p>I have to extract the <em>page</em> from the element href, that&#8217;s a little bit fiddling. Maybe someone can tell me how to do it better (line 17)?</p>
<p>The interesting part here is the call to <strong><code>#url_for_event</code></strong> (line 18). It computes the url to trigger the paginate event, which happens in Ruby!</p>
<p>That works. After clicking a page, the widgets updates itself via AJAX.</p>
<p><i>2</i>Anyway, I keeps adding observers as the widgets updates and re-injects the JS into the page over and over again. There are multiple solutions to the problem.</p>
<ul>
<li>Fix the JS to add the observer only once</li>
<li>Use two different views in your widget, the initial one with, the following without the JS</li>
<li>Write the JS to a global variable using <code>#capture</code> that&#8217;s evaluated only once when the page is rendered completely</li>
</ul>
<p>Personally <strong>I hate using global variables</strong> (that&#8217;s what <code>#capture</code> does). I will use it anyway cause it shows a few interesting concepts.</p>
<p><i>3</i>We need to render the JavaScript to the page footer.</p>
<p>Cells provide a view method <code>#global_capture</code> for sending local content to the global controller view. That&#8217;s not nice. Widgets shouldn&#8217;t know anything about the global page.</p>
<p>Apotomo widgets are based on cells, so they may use that technique as well.</p>
<p>In the <code>display.html.erb</code> view</p>

<div class="wp_syntax"><div class="code"><pre>...
- global_capture :ujs do
  - javascript_tag do
    $(document.body).observe('click', function(e) {
...</pre></div></div>

<p>I embed the <code>#javascript_tag</code> into <code>#global_capture</code> that saves the whole block to the global view&#8217;s instance variable <code>@ujs</code>, which is a brutal interface violation.</p>
<p>In the app&#8217;s <code>views/layouts/application.html.erb</code> I have to render <code>@ujs</code> in order to embed the JavaScript into the page only once. I still hate myself for doing that.</p>

<div class="wp_syntax"><div class="code"><pre>  &lt;/body&gt;
  &lt;%= @ujs %&gt;
&lt;/html&gt;</pre></div></div>

<p>That&#8217;s all for the markup.</p>
<p><i>4</i> The <code>#global_capture</code> method is not automatically available in the widget view, it&#8217;s provided by a <strong>helper</strong>, so let&#8217;s introduce it to our view.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> ItemList <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">Apotomo::StatefulWidget</span>
  helper <span style="color: #CF28C6; ">Cells::Helpers::CaptureHelper</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> display
  <span style="color: #7e7e7e; ">#...</span></pre></div></div>

<p>That looks like in a controller. Note that you can use <code>#helper </code>to pull <em>any</em> helper in your widget view.</p>
<p><i>5</i>Ok, the paging is working in some unobstrusive way. I&#8217;m still not sure if unobstrusive JavaScript is something I like.</p>
<p>What I like is CSS and how it can help us to beautify elements without adding <em>obstrusive</em> CSS.</p>
<p>To add a background to our item list I add a css class to it&#8217;s surrounding div.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> ItemList <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">Apotomo::StatefulWidget</span>
  helper <span style="color: #CF28C6; ">Cells::Helpers::CaptureHelper</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> display
    respond_to_event <span style="color: #4d4d4d; ">:paginate</span>, <span style="color: #4d4d4d; ">:with</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #4d4d4d; ">:display</span>
&nbsp;
    <span style="color: #CF28C6; ">@items</span> = Todo.<span style="color: #CF28C6; ">paginate</span> <span style="color: #4d4d4d; ">:page</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> param<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:page</span><span style="color: #000000; font-weight: bold; ">&#41;</span>, <span style="color: #4d4d4d; ">:per_page</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #000000; ">10</span>, <span style="color: #4d4d4d; ">:order</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #363636; ">'created_at DESC'</span>
&nbsp;
    render <span style="color: #4d4d4d; ">:html_options</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #000000; font-weight: bold; ">&#123;</span>:<span style="color: #7A1775; font-weight: bold;">class</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #363636; ">'lined'</span><span style="color: #000000; font-weight: bold; ">&#125;</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>All I need is adding <code>:html_options</code> to the <code>#render</code> invocation (line 9), this will add the attributes to our list. Obviously I also added some class to the CSS document, but that&#8217;s not part of this course. My CSS-skills suck.</p>
<p><i>6</i>Let&#8217;s view our page again.</p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/04/lined_list.jpg"><img src="http://apotomo.de/wp-content/uploads/2010/04/lined_list-150x150.jpg" alt="lined_list" title="lined_list" width="150" height="150" class="alignleft size-thumbnail wp-image-585" /></a></p>
<p>Looks cooooool, doesn&#8217;t it?</p>
<p>I&#8217;d love to see some JS fix to prevent the usage of <code>#global_capture</code>. Or do you like it? It&#8217;s a common technique in Rails, though. Doesn&#8217;t mean that this is a good thing.</p>
<p>In the next lesson we&#8217;ll <strong>add an interactive form</strong> to add Todo items which processes and updates like the list widget, via AJAX and superfast.</p>
<p><a href="http://apotomo.de/2010/04/writing-an-interactive-form-part-4/"><img src="http://apotomo.de/wp-content/uploads/2010/03/next_lesson_cool1.png" alt="Go to: Writing forms" title="Go to: Writing forms" width="187" height="70" class="aligncenter size-full wp-image-497" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/04/making-unobstrusive-pagination-part-3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Making a Rails Helper predictable</title>
		<link>http://apotomo.de/2010/04/making-a-rails-helper-predictable/</link>
		<comments>http://apotomo.de/2010/04/making-a-rails-helper-predictable/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 14:25:21 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[ActiveHelper]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=527</guid>
		<description><![CDATA[Rails helpers are pretty messy. Everyone who&#8217;s ever tried to simply include, say, the UrlHelper into an object did instantly crash with some NoMethodError either complaining about 	

a missing method that should be included in the helper already, but isn&#8217;t
an undefined method for nil:NilClass


target = Object.new
target.extend ActionView::Helpers::UrlHelper
target.url_for&#40;'/fix/me'&#41;
&#160;
#=&#62; url_helper.rb:94:in `url_for': undefined method `escape_once' for #&#60;Object:0xb7496300&#62; (NoMethodError)

That&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Rails helpers are pretty messy. Everyone who&#8217;s ever tried to simply include, say, the <code>UrlHelper</code> into an object did instantly crash with some <strong><code>NoMethodError</code></strong> either complaining about 	</p>
<ul>
<li>a <strong>missing method</strong> that should be included in the helper already, but isn&#8217;t</li>
<li>an undefined method for <strong><code>nil:NilClass</code></strong></li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="ruby">target = <span style="color: #000000; ">Object</span>.<span style="color: #CF28C6; ">new</span>
target.<span style="color: #CF28C6; ">extend</span> <span style="color: #CF28C6; ">ActionView::Helpers::UrlHelper</span>
target.<span style="color: #CF28C6; ">url_for</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #363636; ">'/fix/me'</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
&nbsp;
<span style="color: #7e7e7e; ">#=&gt; url_helper.rb:94:in `url_for': undefined method `escape_once' for #&lt;Object:0xb7496300&gt; (NoMethodError)</span></pre></div></div>

<p>That&#8217;s due to dependencies between different helpers and the instances they&#8217;re mixed into.</p>
<h3>Wrapping the UrlHelper</h3>
<p>I&#8217;m gonna explain how <a href="http://github.com/apotonick/active_helper">ActiveHelper</a> can help solving this problem by wrapping the <code>UrlHelper</code> step-by-step.</p>
<p>This will make the <code>UrlHelper</code> a predictable, well-behaving class. Helpers should be good kids, nothin&#8217; more.</p>
<p>Here&#8217;s a first version for the new helper.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">module</span> Rails
  <span style="color: #7A1775; font-weight: bold;">class</span> UrlHelper <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">ActiveHelper::Base</span>
    <span style="color: #7A1775; font-weight: bold;">include</span> <span style="color: #CF28C6; ">ActionView::Helpers::UrlHelper</span>
&nbsp;
    provides <span style="color: #4d4d4d; ">:url_for</span>, <span style="color: #4d4d4d; ">:link_to</span>, <span style="color: #4d4d4d; ">:button_to</span> <span style="color: #7e7e7e; "># add more if you need.</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<p>We derive the new helper from <code>ActiveHelper::Base</code>, include the original module and define our interface with <code>provides</code>.</p>
<h3>A rails scenario setup</h3>
<p>In Rails, this is what roughly happens when a view is rendered.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">controller = <span style="color: #CF28C6; ">ActionController::Base</span>.<span style="color: #CF28C6; ">new</span>
view       = <span style="color: #CF28C6; ">ActionView::Base</span>.<span style="color: #CF28C6; ">new</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; font-weight: bold; ">&#91;</span><span style="color: #000000; font-weight: bold; ">&#93;</span>, <span style="color: #000000; font-weight: bold; ">&#123;</span><span style="color: #000000; font-weight: bold; ">&#125;</span>, controller<span style="color: #000000; font-weight: bold; ">&#41;</span>
&nbsp;
view.<span style="color: #CF28C6; ">extend</span> ActiveHelper
view.<span style="color: #CF28C6; ">use</span> <span style="color: #CF28C6; ">Rails::UrlHelper</span></pre></div></div>

<p>Usually the <code>use</code> call would happen in the controller&#8217;s action processing, or in <code>#render</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">view.<span style="color: #CF28C6; ">url_for</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #363636; ">'fix/me'</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
<span style="color: #7e7e7e; "># =&gt; url_helper.rb:85:in `send': undefined method `url_for' for nil:NilClass (NoMethodError)</span></pre></div></div>

<p>Ok, <code>send</code> is accessing <code>nil</code>. Let&#8217;s inspect the original helper.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">  <span style="color: #7A1775; font-weight: bold;">def</span> url_for<span style="color: #000000; font-weight: bold; ">&#40;</span>options = <span style="color: #000000; font-weight: bold; ">&#123;</span><span style="color: #000000; font-weight: bold; ">&#125;</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
    ...
    <span style="color: #CF28C6; ">@controller</span>.<span style="color: #CF28C6; ">send</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:url_for</span>, options<span style="color: #000000; font-weight: bold; ">&#41;</span></pre></div></div>

<p>The rails helpers blindly accesses an instance var <code>@controller</code>, it simply assumes the instance it is mixed into does provide it.<br />
Even worse, it uses <code>send</code> to invoke a method on the controller. That&#8217;s one of rails major drawbacks and <strong>must be fixed</strong>.</p>
<h3>Providing the @controller</h3>
<p>Unfortunately, we won&#8217;t touch the original <code>UrlHelper</code> and open pandora&#8217;s box.</p>
<p>Remember, we got a helper <em>instance</em> which mixed-in the original <code>UrlHelper</code> module. That means we&#8217;d just have to provide the <code>@controller</code> instance variable in our helper instance.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">module</span> Rails
  <span style="color: #7A1775; font-weight: bold;">class</span> UrlHelper <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">ActiveHelper::Base</span>
    <span style="color: #7A1775; font-weight: bold;">include</span> <span style="color: #CF28C6; ">ActionView::Helpers::UrlHelper</span>
&nbsp;
    provides <span style="color: #4d4d4d; ">:url_for</span>, <span style="color: #4d4d4d; ">:link_to</span>, <span style="color: #4d4d4d; ">:button_to</span> <span style="color: #7e7e7e; "># add more if you need.</span>
&nbsp;
    <span style="color: #7A1775; font-weight: bold;">def</span> initialize<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; font-weight: bold; ">*</span>args<span style="color: #000000; font-weight: bold; ">&#41;</span>
      <span style="color: #7A1775; font-weight: bold;">super</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; font-weight: bold; ">*</span>args<span style="color: #000000; font-weight: bold; ">&#41;</span>
      <span style="color: #CF28C6; ">@controller</span> = parent.<span style="color: #CF28C6; ">controller</span>
    <span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<p>We setup an ivar <code>@controller</code> by accessing <code>parent</code>, that&#8217;s the object which is using <em>us</em>, so it&#8217;s the <code>ActionView</code> and that itself does provide a public accessor to its <code>controller</code>.</p>
<h3>Fixing another helper dependency</h3>
<p>Not nice, but anyway, cleaner than things were before.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">view.<span style="color: #CF28C6; ">url_for</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #363636; ">'fix/me'</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
url_helper.<span style="color: #CF28C6; ">rb</span>:<span style="color: #000000; ">94</span>:<span style="color: #7A1775; font-weight: bold;">in</span> <span style="color: #363636; ">`url_for': undefined method `</span>escape_once<span style="color: #363636; ">' for #&lt;Rails::UrlHelper:0xb747b794&gt; (NoMethodError)</span></pre></div></div>

<p>Oh. <code>#escape_once</code> is needed in rails&#8217; original helper, too. I will spare the details about the code now, the method is defined in <code>TagHelper</code>. However it is not mixed in <code>UrlHelper</code>. Shitty helpers.</p>
<p>That&#8217;s another broken dependency we&#8217;ll have to fix.</p>
<p>We now have three possibilities </p>
<ul>
<li>we could wrap <code>TagHelper</code> in an <code>ActiveHelper</code> as well, and <strong>derive</strong> <code>UrlHelper</code></li>
<li>wrapping and <strong><code>use</code></strong>&#8216;ing it inside <code>UrlHelper</code> would be the second solution</li>
<li>or simply <strong><code>include</code></strong> the original module, hoping all will go well</li>
</ul>
<p>For now I&#8217;ll choose the third, anyway, that&#8217;s far from good architecture.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">module</span> Rails
  <span style="color: #7A1775; font-weight: bold;">class</span> UrlHelper <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">ActiveHelper::Base</span>
    <span style="color: #7A1775; font-weight: bold;">include</span> <span style="color: #CF28C6; ">ActionView::Helpers::UrlHelper</span>
    <span style="color: #7A1775; font-weight: bold;">include</span> <span style="color: #CF28C6; ">ActionView::Helpers::TagHelper</span>
&nbsp;
    provides <span style="color: #4d4d4d; ">:url_for</span>, <span style="color: #4d4d4d; ">:link_to</span>, <span style="color: #4d4d4d; ">:button_to</span> <span style="color: #7e7e7e; "># add more if you need.</span>
&nbsp;
    <span style="color: #7A1775; font-weight: bold;">def</span> initialize<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; font-weight: bold; ">*</span>args<span style="color: #000000; font-weight: bold; ">&#41;</span>
      <span style="color: #7A1775; font-weight: bold;">super</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #000000; font-weight: bold; ">*</span>args<span style="color: #000000; font-weight: bold; ">&#41;</span>
      <span style="color: #CF28C6; ">@controller</span> = parent.<span style="color: #CF28C6; ">controller</span>
    <span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<p>Let&#8217;s see if it works.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">view.<span style="color: #CF28C6; ">url_for</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #363636; ">'fix/me'</span><span style="color: #000000; font-weight: bold; ">&#41;</span> <span style="color: #7e7e7e; ">#=&gt; &quot;fix/me&quot;</span>
view.<span style="color: #CF28C6; ">button_for</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #363636; ">'fix/me'</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
<span style="color: #7e7e7e; ">#=&gt; url_helper.rb:298:in `button_to': undefined method `protect_against_forgery?' for #&lt;Rails::UrlHelper:0xb74a7eac&gt; (NoMethodError)</span></pre></div></div>

<p>Now what&#8217;s that again? Looks as if <code>#button_for</code> needs <code>protect_against_forgery?</code>. </p>
<h3>Delegating dependency to the <code>ActionView</code></h3>
<p>Let&#8217;s express that dependency.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">module</span> Rails
  <span style="color: #7A1775; font-weight: bold;">class</span> UrlHelper <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">ActiveHelper::Base</span>
    <span style="color: #7A1775; font-weight: bold;">include</span> <span style="color: #CF28C6; ">ActionView::Helpers::UrlHelper</span>
    <span style="color: #7A1775; font-weight: bold;">include</span> <span style="color: #CF28C6; ">ActionView::Helpers::TagHelper</span>
&nbsp;
    provides <span style="color: #4d4d4d; ">:url_for</span>, <span style="color: #4d4d4d; ">:link_to</span>, <span style="color: #4d4d4d; ">:button_to</span> <span style="color: #7e7e7e; "># add more if you need.</span>
&nbsp;
    needs <span style="color: #4d4d4d; ">:protect_against_forgery</span>?
    ...</pre></div></div>

<p>Having that defined, calls to <code>protect_against_forgery?</code> will be delegated back to the view. That should work.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">view.<span style="color: #CF28C6; ">button_for</span><span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #363636; ">&quot;Click me!&quot;</span>, <span style="color: #363636; ">'fix/me'</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
<span style="color: #7e7e7e; ">#=&gt; &lt;form method=&quot;post&quot; action=&quot;fix/me&quot; class=&quot;button-to&quot;&gt;&lt;div&gt;&lt;input type=&quot;submit&quot; value=&quot;Click me!&quot; /&gt;&lt;/div&gt;&lt;/form&gt;</span></pre></div></div>

<p>Yeah!</p>
<h3>Where to go from here?</h3>
<p>Now that you know how easy it is to clean up the Rails helpers (or Merb, or &#8230;!!!), go and do it. I&#8217;d love to see <a href="http://github.com/apotonick/active_helper">ActiveHelper</a> being used to replace nasty modules and bringing back the power of OOP to your views.</p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/04/making-a-rails-helper-predictable/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ActiveHelper 0.1.0 released</title>
		<link>http://apotomo.de/2010/03/activehelper-010-released/</link>
		<comments>http://apotomo.de/2010/03/activehelper-010-released/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 12:22:31 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[ActiveHelper]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=501</guid>
		<description><![CDATA[Finally – helpers with proper encapsulation, delegation, interfaces and inheritance!
When I was prototyping a new JavaScript generator library using Rails&#8217; helpers I came to a conclusion: Helpers suck.
Typically helpers in ruby frameworks are implemented as simple modules. However, despite the word simple, they suffer from two problems:

Dependency helper modules assume there are other helpers mixed [...]]]></description>
			<content:encoded><![CDATA[<h4>Finally – helpers with proper encapsulation, delegation, interfaces and inheritance!</h4>
<p>When I was prototyping a new JavaScript generator library using Rails&#8217; helpers I came to a conclusion: <strong>Helpers suck.</strong></p>
<p>Typically helpers in ruby frameworks are implemented as simple modules. However, despite the word <em>simple</em>, they suffer from two problems:</p>
<ul>
<li><strong>Dependency</strong> helper modules assume there are other helpers mixed into the target and blindly use foreign methods</li>
<li><strong>Dependency</strong> helper modules rely on instance variables from the target they were mixed into.</li>
</ul>
<h3>ActiveHelper - An example</h3>
<p>ActiveHelper puts helpers back to where they belong: <strong>classes.</strong></p>
<p>Here&#8217;s how a typical MVC helper would look like with ActiveHelper. And, hey, if this is too brief, go and check out the <a href="http://github.com/apotonick/active_helper#readme">README</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> View
  <span style="color: #7A1775; font-weight: bold;">include</span> ActiveHelper
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> https_request?; <span style="color: #000000; ">false</span>; <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<p>We have an exemplary <code>View</code> class that knows if an HTTP request is secure or not.</p>
<h3><code>use</code></h3>
<p>The <code>include</code> pendant for helpers and target instances is <code>use</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold; ">&gt;</span> view.use FormHelper</pre></div></div>

<p>The instance can <strong><code>use</code></strong> a helper to gain he-man&#8217;s form building capabilities.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> FormHelper <span style="color: #000000; font-weight: bold; ">&lt;</span> TagHelper
  provides <span style="color: #4d4d4d; ">:form_tag</span>
  uses UrlHelper
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> form_tag<span style="color: #000000; font-weight: bold; ">&#40;</span>destination<span style="color: #000000; font-weight: bold; ">&#41;</span>
    destination = url_for<span style="color: #000000; font-weight: bold; ">&#40;</span>destination<span style="color: #000000; font-weight: bold; ">&#41;</span>  <span style="color: #7e7e7e; "># in UrlHelper.</span>
    tag<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:form</span>, <span style="color: #363636; ">&quot;action=#{destination}&quot;</span><span style="color: #000000; font-weight: bold; ">&#41;</span> <span style="color: #7e7e7e; "># in TagHelper.</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<h3><code>provides</code></h3>
<p>The <code>FormHelper</code> exposes its interface as it <strong><code>provides</code></strong> a method <code>#form_tag</code> that itself uses two foreign methods <code>#tag</code> and <code>#url_for</code>.</p>
<p>The <code>#tag</code> method is simply <strong>inherited</strong> from the <code>TagHelper</code>, which <code>provides :tag </code>again. See the inheritance?</p>
<p>The latter, <code>#url_for</code>, is implemented in <code>UrlHelper</code>, so the form lib references that dependency when it <strong><code>uses</code></strong> the required helper.</p>
<h3><code>needs</code></h3>
<p>Let&#8217;s look into that url thing helper.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> UrlHelper <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">ActiveHelper::Base</span>
  provides  <span style="color: #4d4d4d; ">:url_for</span>
  needs     <span style="color: #4d4d4d; ">:https_request</span>?
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> url_for<span style="color: #000000; font-weight: bold; ">&#40;</span>url<span style="color: #000000; font-weight: bold; ">&#41;</span>
    protocol = https_request? ? <span style="color: #363636; ">'https'</span> : <span style="color: #363636; ">'http'</span>
    <span style="color: #363636; ">&quot;#{protocol}://#{url}&quot;</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<p>You already got it, don&#8217;t you?</p>
<p>The <code>UrlHelper</code> relies on a mysterious method <code>#https_request?</code>. There is no magical inclusion somewhere. As soon as it <strong><code>needs</code></strong> a method an accessor will be created <em>delegated</em> all calls to the <code>View</code> instance, which was the target.</p>
<h3>Handling dependencies</h3>
<p>So it&#8217;s the importing instance that has to handle methods declared with <code>needs</code>.</p>
<p>If the view wouldn&#8217;t expose the required method, it would be like</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold; ">&gt;</span> view.form_tag<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #363636; ">'apotomo.de'</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
<span style="color: #808080; font-style: italic;"># =&gt; 11:in `url_for': undefined method `https_request?' for #&lt;View:0xb749d4fc&gt; (NoMethodError)</span></pre></div></div>

<p>which is a clear, debugable issue.</p>
<h3>Come on!</h3>
<p>The approach found with ActiveHelper is completely generic, not bound to a specific framework like Rails, Merb or Sinatra. It may (and should!) be used in order to make helpers cleaner, use OOP features and make them easier to debug.</p>
<p>What&#8217;s your impression, what&#8217;s your problems and experiences with helpers? Don&#8217;t be shy, tell us!</p>
<p>Curious about the source? Check out my <a href="http://github.com/apotonick/active_helper">repository at the github</a>, trans.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/03/activehelper-010-released/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What about pagination? (Part 2)</title>
		<link>http://apotomo.de/2010/03/what-about-pagination-part-2/</link>
		<comments>http://apotomo.de/2010/03/what-about-pagination-part-2/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 12:52:57 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[Your 12 Minutes of Apotomo]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=467</guid>
		<description><![CDATA[The list we wrote yesterday was just a basic example (we finished with tag 0.3). Today we&#8217;re gonna paginate the list using the plugin will_paginate.
That serves a couple of purposes:

limiting the amount of items will help keeping the UI clean
using will_paginate proofs that helpers in widgets are just like helpers in controllers
switching to AJAX pagination [...]]]></description>
			<content:encoded><![CDATA[<p>The list we wrote <a href="http://apotomo.de/2010/03/meet-the-statefulwidget-part-1/">yesterday</a> was just a basic example (we finished with tag <code>0.3</code>). Today we&#8217;re gonna paginate the list using the plugin <a href="http://wiki.github.com/mislav/will_paginate/">will_paginate</a>.<br />
That serves a couple of purposes:</p>
<ul>
<li>limiting the amount of items will help <strong>keeping the UI clean</strong></li>
<li>using will_paginate proofs that <strong>helpers in widgets</strong> are just like helpers in controllers</li>
<li>switching to AJAX pagination will touch Apotomo&#8217;s <strong>simple event handling</strong></li>
</ul>
<p><i>1</i>Limiting the item list means limiting the <code>#find</code> operation in our list widget.<br />
<code class="file">app/cells/item_list.rb</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> ItemList <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">Apotomo::StatefulWidget</span>
  <span style="color: #7A1775; font-weight: bold;">def</span> display
    <span style="color: #CF28C6; ">@items</span> = Todo.<span style="color: #CF28C6; ">paginate</span> <span style="color: #4d4d4d; ">:page</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> param<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:page</span><span style="color: #000000; font-weight: bold; ">&#41;</span>, <span style="color: #4d4d4d; ">:per_page</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #000000; ">10</span>, <span style="color: #4d4d4d; ">:order</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #363636; ">'created_at DESC'</span>
&nbsp;
    render
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<p>That&#8217;s dead simple, we just exchanged <code>#find</code> with <code>#paginate</code>. Accessing request parameters can happen with <code>params[]</code>, however, in widgets it&#8217;s a good practice to stick to <code>#param</code>.<br />
The later prevents you from accessing parameters <em>not</em> bound to your widget.</p>
<p><i>2</i> Obviously we need to fix our view as well, as pagination only makes sense when we got buttons to switch pages.<br />
<code class="file">app/cells/item_list/display.html.haml</code></p>

<div class="wp_syntax"><div class="code"><pre>%h3 Things
%p
  = page_entries_info @items
&nbsp;
%ul
  - @items.each do |item|
    %li
      = item.text
&nbsp;
= will_paginate @items</pre></div></div>

<p><a href="http://apotomo.de/wp-content/uploads/2010/03/simple_paging.jpg"><img src="http://apotomo.de/wp-content/uploads/2010/03/simple_paging-150x150.jpg" alt="simple_paging" title="simple_paging" width="150" height="150" class="alignleft size-thumbnail wp-image-475" /></a><br />
When viewing our page, we got a basic paging list, clicking a page button issues a new request.</p>
<p><i>3</i>That works (tag <code>0.4</code>), but it&#8217;s not the concept behind widgets. Widgets usually want to update via AJAX to save load time and to assure proper encapsulation.</p>
<p>Why should we reload the whole page for updating a single list? That&#8217;s overhead!</p>
<p>Keeping that in our collective mind we should add ajaxed buttons.</p>
<p><i>4</i>Unfortunately <code>will_paginate</code> doesn&#8217;t provide a simple way to change the rendering of links (what about some block that&#8217;d define the link markup, mislav?) so we&#8217;re goin&#8217; that way by foot.</p>
<p><code class="file">app/cells/item_list/display.html.haml</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre>%h3 Things
%p
  = page_entries_info @items
&nbsp;
%ul
  - @items.each do |item|
    %li
      = item.text
&nbsp;
- (1..@items.total_pages).each do |page|
  = link_to_event &quot;|#{page}| &quot;, :paginate, :page =&gt; page</pre></td></tr></table></div>

<p>We still use the handy <code>#page_entries_info</code> in line 3. To generate the page list we iterate through all available pages (line 10) calling a yet fameless method <code>#link_to_event</code>.</p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/03/ajax_paging.jpg"><img src="http://apotomo.de/wp-content/uploads/2010/03/ajax_paging-150x150.jpg" alt="ajax_paging" title="ajax_paging" width="150" height="150" class="alignleft size-thumbnail wp-image-481" /></a><br />
<i>5</i> The small helper method creates linked page numbers (<code>|1| |2|</code>). When clicking, nothing happens.</p>
<p>Well, you just fired an event! Looking at firebug&#8217;s XHTTP tracking, a <code>:paginate</code> event is triggered in the <code>'dashboard_list'</code>. That&#8217;s our baby!</p>
<p><i>6</i>Alright, so let&#8217;s handle this event. In Apotomo, triggered events can be observed, maybe we should try that.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> ItemList <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">Apotomo::StatefulWidget</span>
  <span style="color: #7A1775; font-weight: bold;">def</span> display
    respond_to_event <span style="color: #4d4d4d; ">:paginate</span>, <span style="color: #4d4d4d; ">:with</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #4d4d4d; ">:display</span>
&nbsp;
    <span style="color: #CF28C6; ">@items</span> = Todo.<span style="color: #CF28C6; ">paginate</span> <span style="color: #4d4d4d; ">:page</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> param<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:page</span><span style="color: #000000; font-weight: bold; ">&#41;</span>, <span style="color: #4d4d4d; ">:per_page</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #000000; ">10</span>, <span style="color: #4d4d4d; ">:order</span> <span style="color: #000000; font-weight: bold; ">=&gt;</span> <span style="color: #363636; ">'created_at DESC'</span>
&nbsp;
    render
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>Another apotomo&#8217;ed method, <code>#respond_to_event</code>, instructs our list to run the <code>#display</code> state again when it encounters the <code>:paginate</code> event (line 3).</p>
<p>After clicking a page link the updated list appears instantaneously. At the right place. Suuuperfast. Via AJAX. That means: </p>
<ul>
<li>the <code>:paginate</code> event is triggered</li>
<li>it&#8217;s catched by our list widget using <code>#respond_to_event</code></li>
<li>the list invokes the <code>#display</code> method again</li>
<li>the new content replaces the old content, automatically</li>
</ul>
<p>Cool.</p>
<p>But there&#8217;s more.</p>
<p>In the next lessons we&#8217;ll polish our list view and add another widget for posting new items. Hope to see you, guys! COMMENT.</p>
<p><a href="http://apotomo.de/2010/04/making-unobstrusive-pagination-part-3/"><img src="http://apotomo.de/wp-content/uploads/2010/03/next_lesson_cool1.png" alt="Go to: Unobstrusive JavaScript" title="Go to: Unobstrusive JavaScript" width="187" height="70" class="aligncenter size-full wp-image-497" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/03/what-about-pagination-part-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Meet the StatefulWidget! (Part 1)</title>
		<link>http://apotomo.de/2010/03/meet-the-statefulwidget-part-1/</link>
		<comments>http://apotomo.de/2010/03/meet-the-statefulwidget-part-1/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 01:11:07 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[Your 12 Minutes of Apotomo]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=433</guid>
		<description><![CDATA[Hey, great, you decided to dive into Apotomo. Most people didn&#8217;t regret that, so far. For Will, Apotomo is &#8220;what was missing in my two years of study of Ruby on Rails&#8221;, for Andy &#8220;it&#8217;s like a breath of fresh air for rails&#8221;, and for Markus, it&#8217;s just &#8220;sexy&#8221;.

In this tutorial we&#8217;re going to model [...]]]></description>
			<content:encoded><![CDATA[<p>Hey, great, you decided to dive into Apotomo. Most people didn&#8217;t regret that, so far. For Will, Apotomo is &#8220;what was missing in my two years of study of Ruby on Rails&#8221;, for Andy &#8220;it&#8217;s like a breath of fresh air for rails&#8221;, and for Markus, it&#8217;s just &#8220;sexy&#8221;.</p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/03/paging_list.jpg"><img src="http://apotomo.de/wp-content/uploads/2010/03/paging_list-150x150.jpg" alt="A very simple item list." title="A very simple item list." width="150" height="150" class="alignleft size-thumbnail wp-image-438" /></a><br />
In this tutorial we&#8217;re going to model and implement a small todo application, namely <em>dumdidoo</em>, which saves and displays things you have to do.</p>
<p><i>1</i> It&#8217;s on <a href="http://github.com/apotonick/dumdidoo/">github</a>, so go and get it. Be sure to change to the <code>0.1</code> tag, so you&#8217;re at the very beginning of our exciting way.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ git clone git:<span style="color: #000000; font-weight: bold; ">//</span>github.com<span style="color: #000000; font-weight: bold; ">/</span>apotonick<span style="color: #000000; font-weight: bold; ">/</span>dumdidoo.git
$ <span style="color: #000000; ">cd</span> dumdidoo<span style="color: #000000; font-weight: bold; ">/</span>
$ git submodule init
$ git submodule update
&nbsp;
$ script<span style="color: #000000; font-weight: bold; ">/</span>server</pre></div></div>

<p>When browsing to http://localhost:3000/dashboard you see the raw view of the <code>#index</code> action in the <code>DashboardController</code>.</p>
<p><i>2</i>Luckily, I already provide you with some <code>Todo</code> model and random items in the database. What about some <strong>list widget</strong> to show the items?</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ script<span style="color: #000000; font-weight: bold; ">/</span>generate widget item_list display --haml
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>item_list
      create  <span style="color: #000000; ">test</span><span style="color: #000000; font-weight: bold; ">/</span>widgets
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>item_list.rb
      create  app<span style="color: #000000; font-weight: bold; ">/</span>cells<span style="color: #000000; font-weight: bold; ">/</span>item_list<span style="color: #000000; font-weight: bold; ">/</span>display.html.haml
      create  <span style="color: #000000; ">test</span><span style="color: #000000; font-weight: bold; ">/</span>widgets<span style="color: #000000; font-weight: bold; ">/</span>item_list_test.rb</pre></div></div>

<p>Just telling the widget generator to create things for me. It spits out the widget class in <code>app/cells/item_list.rb</code> and a bare view for the <code>display</code> state.</p>
<p>In Apotomo a <em>widget</em> is like a controller in Rails, and a <em>state</em> is similar to an action. The standard state of a widget is usually <code>#display</code>, that&#8217;s somehow matching to the index action in a controller.</p>
<p>So if you look into the widget class and view files you&#8217;ll see they look like good old controllers!</p>
<p><i>3</i>To plug the widget into a page we have to work in the controller.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby"><span style="color: #7A1775; font-weight: bold;">class</span> DashboardController <span style="color: #000000; font-weight: bold; ">&lt;</span> ApplicationController
  <span style="color: #7A1775; font-weight: bold;">include</span> <span style="color: #CF28C6; ">Apotomo::ControllerMethods</span>
&nbsp;
  <span style="color: #7A1775; font-weight: bold;">def</span> index
    use_widgets <span style="color: #7A1775; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold; ">|</span>root<span style="color: #000000; font-weight: bold; ">|</span>
      root <span style="color: #000000; font-weight: bold; ">&lt;&lt;</span> widget<span style="color: #000000; font-weight: bold; ">&#40;</span><span style="color: #4d4d4d; ">:item_list</span>, <span style="color: #363636; ">'dashboard_list'</span><span style="color: #000000; font-weight: bold; ">&#41;</span>
    <span style="color: #7A1775; font-weight: bold;">end</span>
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>In Apotomo, widgets are organized in a tree. The <code>#use_widgets</code> method yields this ominous root node, and we attach a widget of type <code>item_list</code> to it, identified by &#8216;<code>dashboard_list</code>&#8216;.</p>
<p>We need the id to render that widget.</p>
<p><code class="file">app/views/dashboard/index.html.erb</code></p>

<div class="wp_syntax"><div class="code"><pre>&lt;h2&gt;Dashboard&lt;/h2&gt;
&nbsp;
&lt;%= render_widget 'dashboard_list' %&gt;</pre></div></div>

<p>Ok, so we can call <code>#render_widget</code> in controller views to render widgets that we defined earlier with <code>#use_widgets</code>. Widgets, widgets, widgets&#8230;</p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/03/blank_cell.jpg"><img src="http://apotomo.de/wp-content/uploads/2010/03/blank_cell-150x150.jpg" alt="blank_cell" title="blank_cell" width="150" height="150" class="alignleft size-thumbnail wp-image-452" /></a></p>
<p>When viewing the page, the widget&#8217;s default view appears within the controller&#8217;s view. That looks trivial, but, hey, you implemented your first widget! Congratulations!</p>
<p><i>4</i>Now it&#8217;s time to extend this <code>item_list</code> widget and add functionality to list the items.</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"><span style="color: #7A1775; font-weight: bold;">class</span> ItemList <span style="color: #000000; font-weight: bold; ">&lt;</span> <span style="color: #CF28C6; ">Apotomo::StatefulWidget</span>
  <span style="color: #7A1775; font-weight: bold;">def</span> display
    <span style="color: #CF28C6; ">@items</span> = Todo.<span style="color: #CF28C6; ">find</span> <span style="color: #4d4d4d; ">:all</span>
    render
  <span style="color: #7A1775; font-weight: bold;">end</span>
<span style="color: #7A1775; font-weight: bold;">end</span></pre></td></tr></table></div>

<p><code class="file">app/cells/item_list.rb</code></p>
<p>This will collect all the <code>Todo</code> items and push &#8216;em to an instance variable of our widget.</p>
<p>The <code>render</code> call without any arguments just looks for the corresponding view in <code>app/cells/item_list/</code> (which is named after the state). In our case, it will find <code>app/cells/item_list/display.html.haml</code>.</p>
<p>Note that you could also have erb views, or builder, or whatever you prefer.</p>

<div class="wp_syntax"><div class="code"><pre>%h3 Things
&nbsp;
%ul
  - @items.each do |item|
    %li
      = item.text</pre></div></div>

<p><code class="file">app/cells/item_list/display.html.haml</code></p>
<p>Widget views can access instance variables from its widget instance, just as you know that from ordinary controllers.</p>
<p><a href="http://apotomo.de/wp-content/uploads/2010/03/simple_list.jpg"><img src="http://apotomo.de/wp-content/uploads/2010/03/simple_list-150x150.jpg" alt="simple_list" title="simple_list" width="150" height="150" class="alignleft size-thumbnail wp-image-458" /></a>When browsing back to the page, we see the list of all Todo items!<br />
Wow! You just learned how to implement encapsulated, reuseable components, and we&#8217;re at tag <code>0.3</code>, yet.</p>
<p><i>5</i>I was drunk yesterday. Today, I&#8217;m tired. I will stop here. Did it take you 12 minutes? No? Do you have questions already? Feel free to use the comments below.</p>
<p>Well, <a href="http://apotomo.de/2010/03/what-about-pagination-part-2/">tomorrow</a> we&#8217;re gonna extend the list, so it is pageable. We&#8217;re gonna use will_paginate for that and on top of that we&#8217;ll get AJAX paging. Exciting!</p>
<p><a href="http://apotomo.de/2010/03/what-about-pagination-part-2/"><img src="http://apotomo.de/wp-content/uploads/2010/03/next_lesson_cool1.png" alt="Go to: AJAX Paging" title="Go to: AJAX Paging" width="187" height="70" class="aligncenter size-full wp-image-497" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/03/meet-the-statefulwidget-part-1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Your 12 Minutes of Apotomo - new weekly tutorial series launched!</title>
		<link>http://apotomo.de/2010/03/your-12-minutes-of-apotomo-new-weekly-tutorial-series-launched/</link>
		<comments>http://apotomo.de/2010/03/your-12-minutes-of-apotomo-new-weekly-tutorial-series-launched/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 23:00:55 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[Your 12 Minutes of Apotomo]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=408</guid>
		<description><![CDATA[I&#8217;m happy to announce a new tutorial series &#8220;Your 12 Minutes of Apotomo&#8221; which will appear at least once per week, stealing up to 12 minutes of your worthy time per lesson in exchange for learning Apotomo.

Apotomo is stateful widgets for Ruby and Rails that help you keep your code clean and enrich your user [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m happy to announce a new tutorial series <strong>&#8220;Your 12 Minutes of Apotomo&#8221;</strong> which will appear at least once per week, stealing up to 12 minutes of your worthy time per lesson in exchange for learning Apotomo.</p>
<p><a href="http://github.com/apotonick/dumdidoo/"><img src="http://apotomo.de/wp-content/uploads/2010/03/dumdidoo-150x150.jpg" alt="dumdidoo" title="dumdidoo" width="150" height="150" class="alignleft size-thumbnail wp-image-410" /></a></p>
<p>Apotomo is stateful widgets for Ruby and Rails that help you keep your code clean and enrich your user interfaces with components.</p>
<h3>And what do we do?</h3>
<p>We&#8217;re gonna develop a small Todo app <em>dumdidoo</em> with Rails that utilizes a bunch of nifty Web 2.0 features, including</p>
<ul>
<li><strong>dashboards</strong> with slots for widgets</li>
<li><strong>tab panel</strong> and more popular elements for a user-friendly GUI</li>
<li><strong>drag&#038;drop</strong> everywhere</li>
<li><strong>AJAX</strong> pagination, form processing and file uploads</li>
</ul>
<h3>Sounds cool, what else?</h3>
<p>This is not a JavaScript tutorial, it is all Ruby! From a software developer&#8217;s point-of-view you&#8217;re gonna learn about</p>
<ul>
<li><strong>UI design</strong> with widgets</li>
<li><strong>component-oriented</strong> architecture</li>
<li><strong>event triggering</strong> in the browser and in your Ruby code</li>
<li><strong>event mapping</strong> from JS to Ruby</li>
<li><strong>statefulness</strong> and persistence vs. traditional Rails controllers</li>
<li><strong>unit- and functional tests</strong> for components</li>
<li><strong>YUI controls integration</strong> in your Rails app with Apotomo&#8217;s YUI widgets</li>
</ul>
<h3>Can we go?</h3>
<p>The first lesson is right <a href="http://apotomo.de/2010/03/meet-the-statefulwidget-part-1/">here</a>.</p>
<h3>And, please&#8230;</h3>
<p>Open your mouth and <strong>post questions</strong> so we can discuss obscure things in the next lection. Also, don&#8217;t be shy and post &#8220;feature requests&#8221; (what you&#8217;d like the app to do) cause I need to know what <em>you</em> guys want.</p>
<p>We can implement proposed features in one of the following lessons.</p>
<p>Looking forward to the trip!</p>
<p><a href="http://apotomo.de/2010/03/meet-the-statefulwidget-part-1/"><img src="http://apotomo.de/wp-content/uploads/2010/03/next_lesson_cool1.png" alt="Go to: Basic widgets" title="Go to: Basic widgets" width="187" height="70" class="aligncenter size-full wp-image-497" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/03/your-12-minutes-of-apotomo-new-weekly-tutorial-series-launched/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Onfire brings bubbling events to your Ruby objects</title>
		<link>http://apotomo.de/2010/03/onfire-brings-bubbling-events-to-your-ruby-objects/</link>
		<comments>http://apotomo.de/2010/03/onfire-brings-bubbling-events-to-your-ruby-objects/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 14:31:47 +0000</pubDate>
		<dc:creator>nick</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://apotomo.de/?p=382</guid>
		<description><![CDATA[In the current refactoring of Apotomo I finally extracted the bubbling event library into a separate ruby gem Onfire.
Bubbling events is, in contrast to Ruby&#8217;s own Observable module, focused on observing events triggered by business objects, not watching the objects themselves.
In addition, a triggered event will bubble up the tree branch and subsequently inform all [...]]]></description>
			<content:encoded><![CDATA[<p>In the current refactoring of Apotomo I finally extracted the <strong>bubbling event</strong> library into a separate ruby gem <a href="http://github.com/apotonick/onfire">Onfire</a>.</p>
<p>Bubbling events is, in contrast to Ruby&#8217;s own <code>Observable</code> module, focused on <strong>observing <em>events</em> triggered by business objects</strong>, not watching the objects themselves.</p>
<p>In addition, a triggered event will <strong>bubble up</strong> the tree branch and subsequently inform all ancestors- you get automatic organic event filtering.</p>
<p>Complete instructions are on the <a href="http://github.com/apotonick/onfire">github page</a>.</p>
<p>Let&#8217;s see how it works.</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold; ">&gt;</span> require <span style="color: #363636; ">'rubygems'</span>
<span style="color: #000000; font-weight: bold; ">&gt;</span> require <span style="color: #363636; ">'onfire'</span>
<span style="color: #000000; font-weight: bold; ">&gt;</span> class Object
<span style="color: #000000; font-weight: bold; ">&gt;</span>   include Onfire
<span style="color: #000000; font-weight: bold; ">&gt;</span>   attr_accessor :parent
<span style="color: #000000; font-weight: bold; ">&gt;</span> end</pre></div></div>

<p>Bubbling events only make sense in a tree structure. That&#8217;s why Onfire has one requirement to the class it&#8217;s mixed into- <strong><code>#parent</code></strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">ruby = <span style="color: #000000; ">Object</span>.<span style="color: #CF28C6; ">new</span>
&nbsp;
ruby.<span style="color: #CF28C6; ">on</span> <span style="color: #4d4d4d; ">:syntaxError</span> <span style="color: #7A1775; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold; ">|</span>event<span style="color: #000000; font-weight: bold; ">|</span> <span style="color: #000000; ">puts</span> <span style="color: #363636; ">&quot;ruby: omg, a syntax error in #{event.source}&quot;</span> <span style="color: #7A1775; font-weight: bold;">end</span>
ruby.<span style="color: #CF28C6; ">fire</span> <span style="color: #4d4d4d; ">:syntaxError</span>
<span style="color: #7e7e7e; "># =&gt; ruby: omg, a syntax error in #&lt;Object:0xb75a087c&gt;</span></pre></div></div>

<p>That was simple, <code>Observable</code> can do this, too.</p>
<h3>Appending a method</h3>
<p>Now let&#8217;s add a method object to <code>ruby</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">method = <span style="color: #000000; ">Object</span>.<span style="color: #CF28C6; ">new</span>
method.<span style="color: #CF28C6; ">parent</span> = ruby
&nbsp;
method.<span style="color: #CF28C6; ">on</span> <span style="color: #4d4d4d; ">:syntaxError</span> <span style="color: #7A1775; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold; ">|</span>event<span style="color: #000000; font-weight: bold; ">|</span> <span style="color: #000000; ">puts</span> <span style="color: #363636; ">&quot;method: a syntax error in ME?&quot;</span> <span style="color: #7A1775; font-weight: bold;">end</span>
method.<span style="color: #CF28C6; ">fire</span> <span style="color: #4d4d4d; ">:syntaxError</span>
<span style="color: #7e7e7e; ">#=&gt; method: a syntax error in ME?</span>
<span style="color: #7e7e7e; ">#=&gt; ruby: omg, a syntax error in #&lt;Object:0xb756dd00&gt;</span></pre></div></div>

<p>The event bubbles. What about some variable in the method?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">variable = <span style="color: #000000; ">Object</span>.<span style="color: #CF28C6; ">new</span>
variable.<span style="color: #CF28C6; ">parent</span> = method
&nbsp;
variable.<span style="color: #CF28C6; ">on</span> <span style="color: #4d4d4d; ">:syntaxError</span> <span style="color: #7A1775; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold; ">|</span>event<span style="color: #000000; font-weight: bold; ">|</span> <span style="color: #000000; ">puts</span> <span style="color: #363636; ">&quot;variable: I am the problem.&quot;</span> <span style="color: #7A1775; font-weight: bold;">end</span>
variable.<span style="color: #CF28C6; ">fire</span> <span style="color: #4d4d4d; ">:syntaxError</span>
<span style="color: #7e7e7e; ">#=&gt; variable: I am the problem.</span>
<span style="color: #7e7e7e; ">#=&gt; method: a syntax error in ME?</span>
<span style="color: #7e7e7e; ">#=&gt; ruby: omg, a syntax error in #&lt;Object:0xb756dd00&gt;</span></pre></div></div>

<h3>Extending the tree model</h3>
<p>Maybe there&#8217;s another method attached to <code>ruby</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">method2 = <span style="color: #000000; ">Object</span>.<span style="color: #CF28C6; ">new</span>
method2.<span style="color: #CF28C6; ">parent</span> = ruby
&nbsp;
method2.<span style="color: #CF28C6; ">on</span> <span style="color: #4d4d4d; ">:syntaxError</span> <span style="color: #7A1775; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold; ">|</span>event<span style="color: #000000; font-weight: bold; ">|</span> <span style="color: #000000; ">puts</span> <span style="color: #363636; ">&quot;method2: Whoops, something here is wrong.&quot;</span> <span style="color: #7A1775; font-weight: bold;">end</span></pre></div></div>

<p>Now if <code>variable</code> fires, <code>method2</code> shouldn&#8217;t be informed. Remember, the variable is in <code>method</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">variable.<span style="color: #CF28C6; ">fire</span> <span style="color: #4d4d4d; ">:syntaxError</span>
<span style="color: #7e7e7e; ">#=&gt; variable: I am the problem.</span>
<span style="color: #7e7e7e; ">#=&gt; method: a syntax error in ME?</span>
<span style="color: #7e7e7e; ">#=&gt; ruby: omg, a syntax error in #&lt;Object:0xb756dd00&gt;</span></pre></div></div>

<p>Works. That&#8217;s <strong>organic event filtering</strong> resulting from ascending event propagation.<br />
The bubbling event from <code>variable</code> never hits <code>method2</code> on its way up.</p>
<h3>Stopping the event</h3>
<p>What if <code>method</code> wants to intercept syntax errors in its scope, so <code>ruby</code> won&#8217;t get informed any further about errors in <code>method</code>?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">method.<span style="color: #CF28C6; ">on</span> <span style="color: #4d4d4d; ">:syntaxError</span> <span style="color: #7A1775; font-weight: bold;">do</span> <span style="color: #000000; font-weight: bold; ">|</span>event<span style="color: #000000; font-weight: bold; ">|</span> 
  <span style="color: #000000; ">puts</span> <span style="color: #363636; ">&quot;method: that's private things&quot;</span>
  event.<span style="color: #CF28C6; ">stop</span>!
<span style="color: #7A1775; font-weight: bold;">end</span>
&nbsp;
variable.<span style="color: #CF28C6; ">fire</span> <span style="color: #4d4d4d; ">:syntaxError</span>
<span style="color: #7e7e7e; ">#=&gt; variable: I am the problem.</span>
<span style="color: #7e7e7e; ">#=&gt; method: a syntax error in ME?</span>
<span style="color: #7e7e7e; ">#=&gt; method: that's private things</span></pre></div></div>

<p>The event stops in method.</p>
<p>And what if <code>method2</code> contains errors?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">method2.<span style="color: #CF28C6; ">fire</span> <span style="color: #4d4d4d; ">:syntaxError</span>
<span style="color: #7e7e7e; ">#=&gt; method2: Whoops, something here is wrong.</span>
<span style="color: #7e7e7e; ">#=&gt; ruby: omg, a syntax error in #&lt;Object:0xb75348d4&gt;</span></pre></div></div>

<p>It still bubbles up to root. Yeah, makes sense, it&#8217;s a different tree branch.</p>
<h3>More!</h3>
<p>I told you, more examples, source filtering, bubbling, babbling, intercepting on the <a href="http://github.com/apotonick/onfire#readme">github</a>. Have fun and let me know how it works for you! </p>
]]></content:encoded>
			<wfw:commentRss>http://apotomo.de/2010/03/onfire-brings-bubbling-events-to-your-ruby-objects/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
