<?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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LRBlog &#187; Development</title>
	<atom:link href="http://blog.lrdesign.com/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.lrdesign.com</link>
	<description>Logical Reality Design: Web Design and Software Development</description>
	<lastBuildDate>Tue, 07 Jun 2011 21:43:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>RSpec 2.0 and before/after hooks</title>
		<link>http://blog.lrdesign.com/2011/06/rspec-2-0-and-beforeafter-hooks/</link>
		<comments>http://blog.lrdesign.com/2011/06/rspec-2-0-and-beforeafter-hooks/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 21:42:58 +0000</pubDate>
		<dc:creator>Judson</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=242</guid>
		<description><![CDATA[As of RSpec 2, the configuration interface for RSpec changed dramatically.  What used to look like: Spec::Runner.configure do &#124;config&#124; config.prepend_before&#40;:each, :type =&#38;gt; :controller&#41; do ... end end Now looks more like: RSpec::configure do &#124;config&#124; config.before&#40;:each, :line =&#38;gt; 153&#41; do end end One significant and interesting change is the way that before hooks are processed.  Specifically, [...]]]></description>
			<content:encoded><![CDATA[<p>As of RSpec 2, the configuration interface for RSpec changed dramatically.  What used to look like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Spec::Runner</span>.<span style="color:#9900CC;">configure</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
config.<span style="color:#9900CC;">prepend_before</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:controller</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
...
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now looks more like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">RSpec::configure <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
  config.<span style="color:#9900CC;">before</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span>, <span style="color:#ff3333; font-weight:bold;">:line</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006666;">153</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>One significant and interesting change is the way that before hooks are processed.  Specifically, the #before, #after, and #around methods are now part of the Hooks module, which is included in both ExampleGroup and in Configuration, so you call configure.before in exactly the same way as you do within a describe block.  Normally, you pass :each or :all, which sets the scope under which the hook will be called, but Hooks inspects the arguments for filtering metadata regardless of where you call it - I don't know that you'd <em>want</em> filter within an ExampleGroup, but you could...</p>
<p>Unfortunately, as cool as the metadata filtering capabilities are they aren't, as far as I can tell, very well documented.  The process of extracting the metadata lives in it's own <code>:nodoc:</code> limbo, and the attachment of metadata to a particular example is scattered throughout the RSpec code.  This, then, is an attempt to pick that apart.</p>
<h2>Extracting Filters</h2>
<p>When you call Hooks#before, for example (#after and #around work fundamentally the same way), the args are examined and two things are extracted:</p>
<p>A scope, which is the :each, :all, or :suite specification.</p>
<p>A metadata filter hash.  Normally, you call <code>#before(:each, {:hash =&gt; [:of, :metadata]})</code>, but you can instead do something like <code>before(:all, :symbol)</code> which will result in a metadata filter like <code>{:symbol =&gt; true}</code></p>
<p>Again, probably if you need to add metadata inside of a describe block, you are Doing Something Wrong, but maybe there's a good reason.  The extreme (excessive?) flexibility of RSpec metadata and filtering does open up a lot of interesting possibilities.</p>
<h2>Filter Matching</h2>
<p>The metadata filter is used to decide if the hook should be run for a particular example block that it might apply to.  As such, it's a remarkably powerful filtering system, although there's a lot of assumptions about it's format that you need to bear in mind.</p>
<p>The actual mechanics of the metadata filtering happen in <code>RSpec::Core::Metadata#apply?</code> and <code>#apply_condition</code> - there's a long chain of delegation and extra-meta-programming that leads there.</p>
<p>The upshot is that your metadata filter will be compared to the metadata on the example key/value pair by pair, like this:</p>
<ul>
<li>A regular expression in the filter will match against the appropriate value for the example.</li>
<li>If you pass :line_number =&gt; 17, Rspec will check to see if the example includes line 17, much like running rspec filename_spec.rb:17</li>
<li>Any other Fixnum will be compared with == to the value in the metadata</li>
<li>Anything else gets compared with == to the value in the metadata, after both values have been converted to a string.</li>
<li>A proc like <code>{|value| ... }</code> will get the value of the key, and can return true for a match.</li>
</ul>
<p>Filters can nest Hashes, which will be compared to nested Hashes in the metadata.  In other words, if you want to be able to match for metadata like</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#123;</span>...,  :example_group =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span>..., <span style="color:#ff3333; font-weight:bold;">:full_description</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;A very long winded example of the group&quot;</span>, ...<span style="color:#006600; font-weight:bold;">&#125;</span>, ...<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>You can do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:example_group =<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&#123;</span>:full_description =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">/</span>long winded<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<div>RSpec attaches some metadata to examples and groups, but you can also explicitly add metadata to groups and examples as they're defined.  One useful example of that is</div>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">it <span style="color:#996600;">&quot;should do something useful, someday&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:pending</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;Not this day, though&quot;</span></pre></div></div>

<p>Which is much faster than using the pending method call inside the block, and can be applied to a describe block to make the whole thing pending - especially handy when you have a before block inside that is causing problems.</p>
<p>In the same token, the example given in RSpec 2 documentation and announcement posts has been doing something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">it <span style="color:#996600;">&quot;should not be taking this looooong&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:slow</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#0000FF; font-weight:bold;">true</span></pre></div></div>

<p>Since metadata can also be used to filter examples, you could use this to pull out the examples that take forever from your all-the-time specs, and run them only before a push, for instance.</p>
<h2>What Metadata Does RSpec Give Us?</h2>
<p>Probably the best way to figure that out is this very pragmatic approach.</p>
<h3>A Useful Trick</h3>
<p>Very useful for experimenting with metadata is that the proc form of the metadata has a special case: if the proc takes two arguments, the whole metadata hash will get passed into the proc, so you can inspect it at leisure.  The snippet looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'pp'</span>
before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span>, <span style="color:#ff3333; font-weight:bold;">:bogus</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#CC0066; font-weight:bold;">proc</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>val, all<span style="color:#006600; font-weight:bold;">|</span> pp all<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<div>From a Rails controller spec:</div>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#123;</span>
<span style="color:#ff3333; font-weight:bold;">:execution_result</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006600; font-weight:bold;">&#123;</span>:started_at=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;Tue Jun 07 <span style="color:#006666;">14</span>:<span style="color:#006666;">13</span>:<span style="color:#006666;">46</span> <span style="color:#006600; font-weight:bold;">-</span>0700 <span style="color:#006666;">2011</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
<span style="color:#ff3333; font-weight:bold;">:type</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;:controller,
<span style="color:#ff3333; font-weight:bold;">:full_description</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;">&quot;UserSessionsController should be authorized&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:description</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;">&quot;should be authorized&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:example_group</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;
  <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:full_description</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;">&quot;UserSessionsController&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:file_path</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;spec/controllers/user_sessions_controller_spec.rb&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:describes</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;UserSessionsController,
  <span style="color:#ff3333; font-weight:bold;">:description</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#996600;">&quot;UserSessionsController&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:block</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#008000; font-style:italic;">#</span>
,
  <span style="color:#ff3333; font-weight:bold;">:line_number</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt;<span style="color:#006666;">3</span>,
  :<span style="color:#CC0066; font-weight:bold;">caller</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span> ... <span style="color:#9900CC;">the</span> whole backtrace of the group ... <span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span>,
:<span style="color:#CC0066; font-weight:bold;">caller</span>=<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span> ... <span style="color:#9900CC;">the</span> backtrace of the example ...<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>One of the cool-but-problematic things about metadata in RSpec is that it get's added and updated all over the codebase, and constantly over the lifecycle of an example run and extensions (like Rspec-Rails) add their own fields and values, so it's very hard to have formal documentation for what you can match.  Also, somewhat troubling, is that none of these fields are an explicit part of the RSpec API, and so might change with very little notice.  It seems like the best way to manage working with the metadata is with the above pragmatic approach.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2011/06/rspec-2-0-and-beforeafter-hooks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Extending form_for in Rails 3 with your own methods</title>
		<link>http://blog.lrdesign.com/2011/04/extending-form_for-in-rails-3-with-your-own-methods/</link>
		<comments>http://blog.lrdesign.com/2011/04/extending-form_for-in-rails-3-with-your-own-methods/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 19:58:12 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[adding methods]]></category>
		<category><![CDATA[extending]]></category>
		<category><![CDATA[FormBuilder]]></category>
		<category><![CDATA[form_for]]></category>
		<category><![CDATA[Gems]]></category>
		<category><![CDATA[Rails 3]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=272</guid>
		<description><![CDATA[The code that handles <code>form_for</code> in Rails 3 is rather dense and incomprehensible and takes a while to pore through.   Here's the short version to understanding it so you can add your own methods to <code>FormBuilder</code> properly.   Since we dug through it, hopefully this will save others some time. ]]></description>
			<content:encoded><![CDATA[<p>At LRDesign, we have a bunch of internal tools to make laying out Rails views more consistent.  I recently upgraded and improved some of ours for Rails 3, and published them as a gem.  (The published / open source ones are available at <a href="https://github.com/LRDesign/lrd_view_tools">https://github.com/LRDesign/lrd_view_tools</a>, if you're interested).  One of the handy techniques we figured out (poring through the Rails code) is how to correctly add a method to <code>FormBuilder</code> so that you can properly use it inside a <code>form_for</code>  block.</p>
<h2>An example method added to forms:</h2>
<p>Since I nearly always want<code> &lt;input&gt;</code> and <code>&lt;label&gt;</code> tags at the same time, I created a <code>labeled_input</code> method that lets me say this (in HAML):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">= form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@book<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
    = f.<span style="color:#9900CC;">labeled_input</span> <span style="color:#ff3333; font-weight:bold;">:title</span>
    = f.<span style="color:#9900CC;">labeled_input</span> <span style="color:#ff3333; font-weight:bold;">:author</span>
    = f.<span style="color:#9900CC;">labeled_input</span> <span style="color:#ff3333; font-weight:bold;">:price</span></pre></div></div>

<p>to get:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;/books/new&quot;&gt;
  &lt;div class=&quot;labeled_input&quot;&gt;
    &lt;label for=&quot;book_title&quot;&gt;Title:&lt;/label&gt;&lt;input id=&quot;book_title&quot; name=&quot;book[title]&quot; type=&quot;text&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;labeled_input&quot;&gt;
    &lt;label for=&quot;book_author&quot;&gt;Author:&lt;/label&gt;&lt;input id=&quot;book_author&quot; name=&quot;book[author]&quot; type=&quot;text&quot; /&gt;
  &lt;/div&gt;
  &lt;div class=&quot;labeled_input&quot;&gt;
    &lt;label for=&quot;book_price&quot;&gt;Price:&lt;/label&gt;
    &lt;input id=&quot;book_price&quot; name=&quot;book[price]&quot; type=&quot;text&quot; /&gt;
  &lt;/div&gt;
&lt;/form&gt;</pre></div></div>

<p>Combined with some default CSS code in our application template that aligns the <code>&lt;label&gt;</code>s and <code>&lt;input&gt;</code>s in columns, this saves us a couple of hours setting up clean-looking forms on every new project, while significantly shortening and prettifying our view templates.   (Markup Haiku, just like HAML intended.)</p>
<h2>Implementing the extension in Rails 3</h2>
<p>The code that handles <code>form_for</code> in Rails 3 is rather dense and incomprehensible and takes a while to pore through.   Here's the short version to understanding it so you can add your own methods to <code>FormBuilder</code> properly.   Since we dug through it, hopefully this will save others some time.    The only Rails file you care about for this purpose is actionpack-3.0.x/lib/action_view/helpers/form_helper.rb.</p>
<ul>
<li><code>module ActionView::Helpers::FormHelper</code> defines a bunch of helpers, like <code>label</code>, <code>text_field</code>, etc. that define helpers you use <em>outside of a <code>form_for</code></em>.  For example, <code>text_field(@user, :title)</code> calls this version of the helper.</li>
<li><code>class ActionView::Helpers::FormBuilder</code> is what's used to define the helpers you run <em>inside</em> a <em>form_for</em>.   It works automatically via metaprogramming ... when loaded, it finds each helper in <code>FormHelper</code> (except for a few) and defines a similarly named method in <code>FormBuilder</code>.    <code>form_for(@user) { |f| f.text_field(:title)</code> calls this version of the helper, which basically just calls the FormHelper version but passes the FormBuilder's @object_name as an additional first argument.   In version 3.0.7, this metaprogramming happens on lines 1131-1141 of form_helper.rb.</li>
<li>As a result, if you were to write a new helper in <code>ActionView::Helpers::FormHelper</code> that uses the same argument structure as the pre-built ones, you'd automatically get both kinds of helper.    However, if you're writing your own plugin or gem and injecting new helpers, this won't happen because by the time you inject your method FormBuilder will have already done its metaprogramming (it happens when the file is loaded).</li>
<li>The solution to this is that your gem needs to do the second half - defining the <code>FormBuilder</code> version of the helper - itself.  I'll put an example below.</li>
<li>Most of the helper methods work by instantiating InstanceTag, a local one-size-fits-all class to emit a form tag, and then calling the appropriate method for the kind of tag that's wanted, like <code>to_text_field_tag</code>.  It's very confusing why the Rails team decided to do one class for InstanceTag and a bunch of different methods, rather than make subclasses of InstanceTag for each kind of tag they want; an odd OOP decision, but that's what we've got.</li>
<li>InstanceTag itself has only one line:  it includes InstanceTagMethods, a model that defines all the methods for the class, and which isn't used elsewhere.</li>
</ul>
<p>So to implement a <code>FormBuilder</code> method yourself that you can use inside a form_for, the best way is to inject your method inside <code>FormHelper</code>, and then call that from a method you inject into <code>FormBuilder</code>.   This gives you both versions of the method, in the same structure that Rails defines them.   You could do this either in a helper file directly in your application, or in a gem (like we have) so you can reuse your form helpers in more than one projects.   </p>
<h2>An example implementation.</h2>
<p>Here's a simplified construction of the <code>labeled_input</code> method we use at LRD.  This one just emits a label and a text field and wraps them in a &lt;div&gt;.</p>
<h3>Start by defining the helper:</h3>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> LRD
  <span style="color:#9966CC; font-weight:bold;">module</span> FormHelper
    <span style="color:#9966CC; font-weight:bold;">def</span> labeled_input<span style="color:#006600; font-weight:bold;">&#40;</span>object_name, method, options = <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;">&#41;</span>
      input = text_field<span style="color:#006600; font-weight:bold;">&#40;</span>object_name, method, options<span style="color:#006600; font-weight:bold;">&#41;</span>
      label = label<span style="color:#006600; font-weight:bold;">&#40;</span>object_name, method, options<span style="color:#006600; font-weight:bold;">&#41;</span>
      content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:div</span>, <span style="color:#006600; font-weight:bold;">&#40;</span>label<span style="color:#006600; font-weight:bold;">+</span>input<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> :<span style="color:#9966CC; font-weight:bold;">class</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'labeled_input'</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#6666ff; font-weight:bold;">ActionView::Helpers::FormHelper</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#9966CC; font-weight:bold;">include</span>, <span style="color:#6666ff; font-weight:bold;">LRD::FormHelper</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This will successfully define <code>labeled_input</code> that you can use outside of a form_for.   </p>
<h3>Now add the FormBuilder version:</h3>
<p>To get it working inside of a form_for, you need to add a similar method to <code>ActionView::Helpers::FormBuilder</code>.   As mentioned above, Rails does this automatically for its own <code>FormHelper</code> methods using a metaprogramming approach.  But since that has already happened by the time your code can inject into <code>FormHelper</code>, you have to do it yourself.   The solution we used is to make our own <code>FormBuilder</code> module that manually defines the <code>labeled_input</code> method in the same format that <code>FormBuilder</code> would have done, and then auto-include that into <code>FormBuilder</code> when our own <code>FormHelper</code> module gets included.  Add this stuff to the above code block:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Inside LRD::FormHelper, add this method:</span>
<span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>arg<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#6666ff; font-weight:bold;">ActionView::Helpers::FormBuilder</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#9966CC; font-weight:bold;">include</span>, <span style="color:#6666ff; font-weight:bold;">LRD::FormBuilder</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> <span style="color:#6666ff; font-weight:bold;">LRD::FormBuilder</span>
  <span style="color:#008000; font-style:italic;"># ActionPack's metaprogramming would have done this for us, if FormHelper#labeled_input </span>
  <span style="color:#008000; font-style:italic;"># had been defined  at load.   Instead we define it ourselves here.</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> labeled_input<span style="color:#006600; font-weight:bold;">&#40;</span>method, options = <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;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@template</span>.<span style="color:#9900CC;">labeled_input</span><span style="color:#006600; font-weight:bold;">&#40;</span>@object_name, method, objectify_options<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</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 practice, our <code>labeled_input</code> method is much more complex; it handles other input types, can add instructional comments/notes to the field, and can accept a block if you want to put something other than an <code>&lt;input&gt;</code> where the text field normally goes.   This guide should get you started to writing your own <code>form_for</code> methods quickly, but if you want to see how to do more complex things, check out <a href="https://github.com/LRDesign/lrd_view_tools/blob/master/lib/app/helpers/lrd_form_helper.rb">the full version</a>.</p>
<h2>Adding more input types or other tags.</h2>
<p>If you wanted to add an entire different tag or input type (as opposed to combining different ones, the way <code>labeled_input</code> does), you would probably start by building a module that you inserted into <code>InstanceTag</code> or <code>InstanceTagMethods</code>.  It should define a method like <code>MyInstanceTagModule#to_some_funky_tag()</code> in parallel <code>with to_input_field_tag()</code>.</p>
<h2>Testing it with rSpec 2</h2>
<p>Another challenge we faced was writing specs for <code>labeled_input's</code> behavior.  It's a bit of a trick because we needed to instantiate <code>ActionView</code> and render some templates to check the output, but rspec-rails is written with the assumption that you will be loading an entire rails project and all the rails gems. If you want to spec just a view helper, you need to load a bunch of rspec-rails's files one by one, and then manually include <code>RSpec::Rails::ViewExampleGroup</code> into RSpec's configuration.   We may write a separate post on this process in the future, but in the meantime, take a look at <a href="https://github.com/LRDesign/lrd_view_tools/blob/master/spec/spec_helper.rb">lrd_view_tools' spec_helper file</a> and <a href="https://github.com/LRDesign/lrd_view_tools/blob/master/spec/labelled_input_spec.rb">example spec for labeled_input</a> to get the sense of it.</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2011/04/extending-form_for-in-rails-3-with-your-own-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On the road to faster specs</title>
		<link>http://blog.lrdesign.com/2011/01/on-the-road-to-faster-specs/</link>
		<comments>http://blog.lrdesign.com/2011/01/on-the-road-to-faster-specs/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 23:39:41 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=234</guid>
		<description><![CDATA[ Just drop this blob of code at the bottom of your spec_helper.rb - I saw a 43% speed increase in one project's spec suite. ]]></description>
			<content:encoded><![CDATA[<p>Running large spec or test suites can be a bane of Rails developers.   I've certainly stopped using autotest since half of our projects started exceeding 5 minutes of spec runtime.   After seeing (three times!) presentations by <a href="http://twitter.com/#!/tmm1">Aman Gupta</a>, I had spent some time with perftools trying to figure out what the heck was making my specs take so long.   It's not just that more specs take more time to run: if you clock individual specs you will see identical examples run slower in a larger project.  I'd seen that rspec runs can spend upwards of 60% of their time in the garbage collector, but not pursued it further than that.</p>
<p>A couple days ago, Jamis at the 37Signals blog took this idea further, dug into ActiveSupport::TestCase, and generated <a href="http://37signals.com/svn/posts/2742-the-road-to-faster-tests">this wonderful blog post</a> that explains his findings and how to get a 40% or more speedup in Test::Unit.  His solution involves  reducing the frequency of garbage collection and forcing ActiveSupport::TestCase to destroy instance variables it doesn't need anymore).</p>
<p>It's great, but if you do exactly what he says it won't quite work in RSpec - and RSpec users should get to enjoy this new development, too!  While RSpec makes use of ActiveSupport::TestCase, it has a different set of internal instance variables, and Jamis' code will end up erasing the variables that store your actual examples.   If you drop in Jamis' code to spec_helper.rb you'll see this error:</p>
<pre>
vendor/rails/activesupport/lib/active_support/whiny_nil.rb:52:in `method_missing':
 undefined method `description' for nil:NilClass (NoMethodError)
</pre>
<p>All that's needed to make RSpec happy is a little tweak to Jamis' code that protects a different set of instance variables from being unset.   Just drop this blob of code at the bottom of your spec_helper.rb - I saw a 43% speed increase in one project's spec suite.  (Note that if you are still using fixtures, you might need to add @loaded_fixtures and/or @fixture_cache to @@reserved_ivars;  at LRD we long since abandoned fixtures in favor of factories, so I haven't tested this on spec suites with fixtures).</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
  setup <span style="color:#ff3333; font-weight:bold;">:begin_gc_deferment</span>
  teardown <span style="color:#ff3333; font-weight:bold;">:reconsider_gc_deferment</span>
  teardown <span style="color:#ff3333; font-weight:bold;">:scrub_instance_variables</span>
&nbsp;
  @@reserved_ivars = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>@_implementation <span style="color:#0066ff; font-weight:bold;">@_result</span> <span style="color:#0066ff; font-weight:bold;">@_proxy</span>  <span style="color:#0066ff; font-weight:bold;">@_assigns_hash_proxy</span> <span style="color:#0066ff; font-weight:bold;">@_backtrace</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  DEFERRED_GC_THRESHOLD = <span style="color:#006600; font-weight:bold;">&#40;</span>ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'DEFER_GC'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">1.0</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_f</span>
&nbsp;
  @@last_gc_run = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> begin_gc_deferment
    <span style="color:#CC00FF; font-weight:bold;">GC</span>.<span style="color:#9900CC;">disable</span> <span style="color:#9966CC; font-weight:bold;">if</span> DEFERRED_GC_THRESHOLD <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> reconsider_gc_deferment
    <span style="color:#9966CC; font-weight:bold;">if</span> DEFERRED_GC_THRESHOLD <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span> <span style="color:#006600; font-weight:bold;">-</span> @@last_gc_run <span style="color:#006600; font-weight:bold;">&gt;</span>= DEFERRED_GC_THRESHOLD
      <span style="color:#CC00FF; font-weight:bold;">GC</span>.<span style="color:#9900CC;">enable</span>
      <span style="color:#CC00FF; font-weight:bold;">GC</span>.<span style="color:#9900CC;">start</span>
      <span style="color:#CC00FF; font-weight:bold;">GC</span>.<span style="color:#9900CC;">disable</span>
&nbsp;
      @@last_gc_run = <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> scrub_instance_variables
    <span style="color:#006600; font-weight:bold;">&#40;</span>instance_variables <span style="color:#006600; font-weight:bold;">-</span> @@reserved_ivars<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>ivar<span style="color:#006600; font-weight:bold;">|</span>
      instance_variable_set<span style="color:#006600; font-weight:bold;">&#40;</span>ivar, <span style="color:#0000FF; font-weight:bold;">nil</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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>(Most of this code is Jamis', and I'm not taking credit for his fantastic work.)</p>
<p>RSpec already does a much better job of handling instance variables than Test::Unit, so the scrubbing didn't produce a big speedup for me (only about 5%).   But the GC deferment did indeed give me a 43% speed improvement in the spec suite for my biggest project; run time dropped from 7m38s to 4m23s ... what a difference!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2011/01/on-the-road-to-faster-specs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NinjaScript: Javascript so unobtrusive, you&#8217;ll never it see coming</title>
		<link>http://blog.lrdesign.com/2010/10/ninjascript-javascript-so-unobtrusive-youll-never-it-see-coming/</link>
		<comments>http://blog.lrdesign.com/2010/10/ninjascript-javascript-so-unobtrusive-youll-never-it-see-coming/#comments</comments>
		<pubDate>Wed, 13 Oct 2010 23:19:24 +0000</pubDate>
		<dc:creator>Judson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[HTML, CSS, and Web Standards]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[User Interface Design]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=208</guid>
		<description><![CDATA[Unobtrusive Javascript is one of the coming movements in web design for a reason. ... On the other hand, one hears a lot about "Unobtrusive Is Hard" and how a graceful degrade takes twice as long, etc etc...

Therefore: NinjaScript. Unobtrusive Javascript in a tidy package so that you can get on with your day.]]></description>
			<content:encoded><![CDATA[<p><img alt="NinjaScript Logo" src="http://lrdesign.com/images/logos/ninja_script.png" title="NinjaScript Logo" class="alignleft" width="100" height="100" style="margin-right: 2em"; />We're happy to announce NinjaScript: a jQuery plugin for unobtrusive scripting.<br />
NinjaScript provides:</p>
<ul>
<li>CSS-like language for web page behavior</li>
<li>Define rich behaviors that include both event handlers and transformations.</li>
<li>Durable behaviors that survive DOM alteration, with performance comparable to jQuery's live() method.</li>
<li>Handy built-in behaviors for AJAX.</li>
</ul>
<h2>Motivations</h2>
<p>Unobtrusive Javascript is one of the coming movements in web design for a reason.  Separation of concerns is generally a good thing, and the idea of separating behavior from semantics is pretty obvious once you think about it.  If nothing else, it makes it much easier to think about how you structure your site. Just build it out as if this were still 1998 and you couldn't trust a browser to open an alert box, much less submit AJAX, then come back and mark everything up.</p>
<p>On the other hand, one hears a lot about "Unobtrusive Is Hard" and how a graceful degrade takes twice as long, etc etc.  At the same time, software exists to encapsulate skills.  Could be you'll be seeing a Rails plugin from LRD soon to convert the big Rails helpers into degrading versions.</p>
<p>Therefore: NinjaScript.  Unobtrusive Javascript in a tidy package so that you can get on with your day.</p>
<h2>What's it look like?</h2>
<p>Here's a very simple example.  Suppose you have an existing<br />
<form id="coolness"> that POSTs and reloads the page, and you would like it to submit to the same URL, but via AJAX.</p>
<p>If you have NinjaScript loaded, this all you need:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">behavior</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'#coolness'</span><span style="color: #339933;">:</span> $.<span style="color: #660066;">ninja</span>.<span style="color: #660066;">submits_as_ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>In addition to the pre-defined behaviors like submits_as_ajax(), you can build your own rich behaviors, specifying both transforms (alterations to an element that will be applied as soon as the element appears in the doms) and event handlers at the same time:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">behavior</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'.date_entry'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> transform<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> $<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">datepicker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">'#work_unit_select_all'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> click<span style="color: #339933;">:</span> selectAllWorkUnits <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  <span style="color: #3366CC;">'#timeclock form.edit_work_unit'</span><span style="color: #339933;">:</span>    $.<span style="color: #660066;">ninja</span>.<span style="color: #660066;">ajax_submission</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    busy_element<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#timeclock'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
  <span style="color: #3366CC;">'#messages .flash'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    transform<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">delay</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10000</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">600</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>$<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">'#timeclock input#work_unit_hours'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    click<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evnt<span style="color: #339933;">,</span> elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>hours_format<span style="color: #009900;">&#40;</span>task_elapsed<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That's direct from an LRD project.</p>
<p>Basically, it's meant to look like a stylesheet - as much as possible within jQuery.  This snippet:</p>
<ul>
<li> adds a datapicker to the date entry fields</li>
<li>binds a click handler (defined elsewhere) to allow for selecting all work units</li>
<li>makes a form into an AJAX submitter - complete with busy overlay</li>
<li>makes the #messages list decay - items live for 10 seconds and then go away</li>
<li>sets up automatic calculation of hours for certain fields</li>
</ul>
<p>Pretty simple, but powerful.</p>
<p>What you're seeing there are CSS-style selectors (strictly speaking: jQuery selectors) used to pick elements, and behaviors applied to the elements. $.ninja.ajax_submission() is a prepackaged behavior, which are pretty easy to write.</p>
<p>The ad hoc behavior applied to '#messages .flash' defines a transformation.  Transformations are basically the code you'd throw into a document.ready block, pre-sorted to go to their respective elements, with the added bonus that they'll survive later modifications of the DOM.</p>
<p>Behaviors can also define event handlers by adding an events clause, with the events they respond to as keys.  In other words:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">behavior</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">'.fun'</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    events<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      click<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #339933;">,</span> elem<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> $<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">sing_and_dance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
      mouseover<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #339933;">,</span> elem<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> $<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">shiver_in_anticipation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
      <span style="color: #006600; font-style: italic;">//yes: mouseover.</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>What the app needs to do</h2>
<p>To start, pretend that there is no AJAX.  Build everything with full round trip HTTP. </p>
<p>Next, come back and make sure that your app responds to requires for javascript with scripts to do whatever you need them to do.  Replace elements, usually.</p>
<p>Finally, add behaviors to your pages with NinjaScript.  For AJAX, you don't need to change the HTML at all.  All straightforward forms and GET links can be converted into to AJAX forms just by specifying the submits_as_ajax() behavior as shown in the top example above.   Since you wrote them without AJAX originally, they will continue to work and degrade gracefully without AJAX.</p>
<h2>How it works</h2>
<p>The short answer is: rebinding.  Event delegation is well and good, and if that's all you need, you probably can look elsewhere.  My advice is to stick around, though.  You get plenty of goodness from NinjaScript, without too much pain.</p>
<p>There are some problems with bubbly delegation though.  Event delegation doesn't solve the problem of modifying elements.  You can't delegate watermarking.  And you can't (easily) store data on an element-by-element basis while you're delegating.</p>
<p>NinjaScript builds and applies behavior objects to all the elements selected in the $.behavior block.  When they're applied, behaviors modify their host with their transform function (adding tooltips, changing no-input forms into links, pulling input labels in as watermarks, etc.) and apply event handlers directly to the element. They also mark the element as having been enriched with behavior, so that we don't try to re-apply behavior.</p>
<p>Now, when the DOM is modified, the collection of behaviors is told to reapply all the behavior objects.  Any elements already enriched get skipped, since we know they were already enriched.  One nifty consequence is that elements that weren't around for the initial application get found this time and get behaviors applied.</p>
<p>How do we know the DOM was modified?  Believe it or not, there are events that a lot of browsers generate when the DOM is changed, and we listen for those.  Plus, when a NinjaScript behavior does an AJAX call, it assumes that the resulting javascript execution changed the page, and it fires it's own event based on that.</p>
<h2>Consequences of adopting NinjaScript</h2>
<p>Most noticeably, NinjaScript event handlers are a little different from normal event handlers.  We assume that events shouldn't bubble and shouldn't fire their normal behavior - you can save a little code not worrying about suppressing those.  NinjaScript handlers also are called in the context of the behavior object they're attached to.  This means that "this" is not the element receiving the event; "this" is the behavior object that is unique to the element.  You can stash information about the behavior in there during the transform step, or maintain state for the element between events.  The event handler receives not only the event record (with the original target, etc.) but also the element it's attached to as arguments.  All in all, changing a standard event handler over to a NinjaScript handler isn't terribly difficult.</p>
<p>You should also be aware that NinjaScript really does need to know when the DOM changes.  Everywhere but IE, you should be okay without doing anything - any DOMNodeInserted or DOMSubtreeModified that reaches the root node should trigger rebinding.  To be safe, call $.ninja.tools.fire_mutation_event() and everything should be fine.</p>
<p>There exists a (small) set of utility functions at $.ninja.tools - right now there's only:<br />
$.ninja.tools.perform_ajax_submission(form_or_anchor) - Submits form data over AJAX, evals the response and triggers a rebind.</p>
<h2>Directions for the Future</h2>
<p>NinjaScript really wants more stock behaviors.  Already on the TODO list are:</p>
<ul>
<li>make_watermark</li>
<li>Editable table rows - it'd be nice to be able to have AJAX checkboxes and draggable order</li>
<li>Fading messages - complete with a backlog and roll back - "Wait, what was that?"</li>
<li>Undoable edits - there's likely a lot of backend support this needs.</li>
</ul>
<p>Behaviors should be mergable.  At the moment, the application of two behaviors to the same element is undefined, in that their order isn't predictable.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2010/10/ninjascript-javascript-so-unobtrusive-youll-never-it-see-coming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convection: self-hosted secure file exchange in Rails</title>
		<link>http://blog.lrdesign.com/2010/06/convection-self-hosted-secure-file-exchange-in-rails/</link>
		<comments>http://blog.lrdesign.com/2010/06/convection-self-hosted-secure-file-exchange-in-rails/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 02:54:03 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
				<category><![CDATA[File Exchange]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[client management]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=160</guid>
		<description><![CDATA[Need to swap files with clients or collaborators, but don't want to (or can't) trust those files to Amazon or sendbigfiles.com?  Want fine-grained control over which users can see which files?   Try Convection.]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.lrdesign.com/wp-content/uploads/2010/06/convection.png"><img src="http://blog.lrdesign.com/wp-content/uploads/2010/06/convection.png" alt="" title="convection" width="100" height="100" class="alignleft size-full wp-image-166" style="margin-right: 1em;" /></a>Introducing <a href="http://github.com/LRDesign/Convection">Convection</a>, an open-source (MIT License) project of Logical Reality Design.    Need to swap files with clients or collaborators, but don't want to (or can't) trust those files to Amazon or sendbigfiles.com?  Want fine-grained control over which users can see which files?   Try Convection.</p>
<p>Lots of file exchange services exist, for example SendBigFiles.com etc.  However, all of these services are hosted on someone else's hardware, and most of them share files by transferring URLs -- usually via email -- without good access control or authorization schemes. </p>
<p>We built Convection because a client needed to transfer files with other companies, but they needed to host the system themselves because the contracts they hold with their own clients require them not to store data on services that they don't control.   The specifications Convection was built around were:</p>
<ol>
<li>Hosted on our own server.</li>
<li>Downloads require a login, and files cannot be shared by email.</li>
<li>Users must log in to download files or see available files.</li>
<li>User accounts can be grouped, groups can be managed.</li>
<li>Files can be shared with an entire group.</li>
<li>Files uploaded by users default to minimal permission - visible only to the uploader and to admins.</li>
<li>All communications over SSL. (we made this optional)</li>
</ol>
<h2>Installing and hosting Convection</h2>
<p>To run Convection, you will need a webserver capable of running a Ruby on Rails application, and a database.   Setting such a thing up is beyond the scope of this post.   If you have a Dreamhost account, you can set up a Rails-capable domain with a couple of clicks in their web panel.  In addition to the server, you will need to set up a database (we have only tested MySQL, but Convection should work with any SQL database for which Rails/ActiveRecord has a supported adapter, including PostgreSQL and Oracle), and initialize the database with these two commands:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">&gt;</span> rake db:migrate <span style="color: #007800;">RAILS_ENV</span>=production
  <span style="color: #000000; font-weight: bold;">&gt;</span> rake db:seed <span style="color: #007800;">RAILS_ENV</span>=production</pre></div></div>

<p>This will generate the tables necessary for Convection to run, and create a pair of initial demo users "admin" and "user", both with password "foobar".   </p>
<p>If you are setting up a server yourself, there are plenty of guides to deploying Rails on the web.   Much of our own <a href="http://blog.lrdesign.com/2010/02/howto-setting-up-cruisecontrol-rb-on-slicehost/">guide to deploying CruiseControl.rb</a> can be used to set up any Rails application on Slicehost or any other Ubuntu Linux hosting provider.  </p>
<p>Let me know if you're trying to deploy Convection and having trouble: if we know people are using it we may put effort into making it easier to deploy and install, and write a more thorough guide.</p>
<p>A few other links that may help you with deploying a Rails application, depending on your environment:</p>
<ol>
<li><a href="http://wdvl.com/Authoring/ror/Passenger/saurabh_bhatia04142010.html">Using Phusion Passenger to Deploy a Rails Application on Apache</a></li>
<li><a href="http://pragprog.com/titles/fr_deploy/deploying-rails-applications">Deploying Rails Applications (book)</a></li>
</ol>
<p>If you Google around you may find plenty of other links relevant to your particular environment.</p>
<h2>Configuring Convection</h2>
<p>If you log into your running Convection application as an administrator (initial user "admin", password "foobar"), an Admin Tools utility will appear in the right hand column.   From here, you can access tools for creating users, and groups, and the general site configuration.</p>
<p>In general site config, you can set your site name and logo, set whether or not the site requires SSL access (Note: your server must already support SSL!) outgoing email and email notification preferences, add Google analytics, and an assortment of other site configuration operations that are mostly self-explanatory. </p>
<h2>Upload progress bar: experimental feature.</h2>
<p>If your site hosts large uploads that take a while to transfer, you can try our experimental tools to provide an upload progress bar to the user.   This tool will only work if your site is served by Apache, and requires installing and configuring an optional module for Apache.</p>
<p>To enable this tool, follow the instructions in the README file and associated links, and turn on the progress bar setting in site preferences.</p>
<h2>Helping us improve Convection</h2>
<p>Convection is currently in version 1.1.4 and has been in production in two places (that we know of) for about five months as of June 8, 2010.   </p>
<p>Please let us know if you are using Convection and enjoy it (or don't).   Feel free to request features or alterations, but Convection is open source, so also please consider contributing if you have ideas!  </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2010/06/convection-self-hosted-secure-file-exchange-in-rails/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Transactional Testing for Multiple Databases in ActiveRecord</title>
		<link>http://blog.lrdesign.com/2010/03/transactional-testing-for-multiple-databases-in-activerecord/</link>
		<comments>http://blog.lrdesign.com/2010/03/transactional-testing-for-multiple-databases-in-activerecord/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 06:36:27 +0000</pubDate>
		<dc:creator>Judson</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=130</guid>
		<description><![CDATA[We've been working on an app that needs to stand astride two databases - one local DB for the app itself, and another with restrictive policies about modifications that is nonetheless authoritative on many subjects. There's a fair amount of tricky interaction between the two, and testing has been a delightful challenge. We're using the [...]]]></description>
			<content:encoded><![CDATA[<p>We've been working on an app that needs to stand astride two databases - one local DB for the app itself, and another with restrictive policies about modifications that is nonetheless authoritative on many subjects.  There's a fair amount of tricky interaction between the two, and testing has been a delightful challenge.</p>
<p>We're using the use_db plugin, and all it takes to make testing transactions happen around multiple DBs is:</p>
<p><code>In: spec/spec_helper.rb</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'override_test_callbacks'</span></pre></div></div>

<p>My concern comes from the fact that this is a direct and unfiltered monkeypatch on ActiveRecord::TestFixtures.  So it relies on <code>use_transactional_fixtures</code> (which could certainly be used without using actual fixures, granted), and if the test transaction code moves within Rails, that's another integration to worry about.  Or if we add a spec that doesn't wind up making ActiveRecord::TestFixtures load... Or if we decide to use something other than use_db...</p>
<p>So instead I'm using:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Spec::Runner</span>.<span style="color:#9900CC;">configure</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
  config.<span style="color:#9900CC;">prepend_before</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>UseDbPlugin.<span style="color:#9900CC;">all_use_dbs</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>db<span style="color:#006600; font-weight:bold;">|</span>
      db.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">increment_open_transactions</span>
      db.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">transaction_joinable</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
      db.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">begin_db_transaction</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  config.<span style="color:#9900CC;">append_after</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#006600; font-weight:bold;">&#40;</span>UseDbPlugin.<span style="color:#9900CC;">all_use_dbs</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">reverse</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>db<span style="color:#006600; font-weight:bold;">|</span>
      db.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">rollback_db_transaction</span>
      db.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">decrement_open_transactions</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>If we weren't already using transactional fixtures, I might pull out the <code lang="ruby">- [ActiveRecord::Base]</code>.  And if we were to change off of use_db, there's one place to change the transaction code.  Finally, there's much less dependence on the innards of ActiveRecord - only it's published API.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2010/03/transactional-testing-for-multiple-databases-in-activerecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Danger: ActiveRecord, param hashes, and symbol keys</title>
		<link>http://blog.lrdesign.com/2010/03/danger-activerecord-param-hashes-and-symbol-keys/</link>
		<comments>http://blog.lrdesign.com/2010/03/danger-activerecord-param-hashes-and-symbol-keys/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 22:20:17 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=109</guid>
		<description><![CDATA[Here's a little foible of ActiveRecord that cost me over an hour today. AR accepts both symbol keys and string keys when specifying attributes. Both of these are valid ways of mass assigning attributes to a Rails model: MyModel.new&#40;:field_1 =&#62; 'foo', :field_2 =&#62; 'bar'&#41; MyModel.new&#40;'field_1' =&#62; 'foo', 'field_2' =&#62; 'bar'&#41; It's convenient, often, to not [...]]]></description>
			<content:encoded><![CDATA[<p>Here's a little foible of ActiveRecord that cost me over an hour today.   AR accepts both symbol keys and string keys when specifying attributes.   Both of these are valid ways of mass assigning attributes to a Rails model:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">MyModel.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:field_1</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foo'</span>, <span style="color:#ff3333; font-weight:bold;">:field_2</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bar'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
MyModel.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'field_1'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foo'</span>, <span style="color:#996600;">'field_2'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bar'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>It's convenient, often, to not have to worry about whether your keys are symbols are strings since they get converted around a bit when you pass parameters.   The downside of this, however, is that it will happily accept BOTH without complaining, and will quietly default to the symbol key regardless of the order you specify them in:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> model = MyModel.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:field_1</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foo'</span>, <span style="color:#996600;">'field_1'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bar'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#0000FF; font-weight:bold;">nil</span>; 
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> mymodel.<span style="color:#9900CC;">field_1</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foo'</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> model = MyModel.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'field_1'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'foo'</span>, <span style="color:#ff3333; font-weight:bold;">:field_1</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bar'</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#0000FF; font-weight:bold;">nil</span>;
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> mymodel.<span style="color:#9900CC;">field_1</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'bar'</span></pre></div></div>

<h3>Okay, so that's kinda sloppy.  Bad ActiveRecord!  No Biscuit!</h3>
<p>This can cause <em>serious</em> confusion for the unwary.   When ActionController hands us a params hash, it always has String keys, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#CC0066; font-weight:bold;">eval</span> params
<span style="color:#006600; font-weight:bold;">=&gt;</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'article'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">'title'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Awesome blog post'</span>, <span style="color:#996600;">'body'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'I will make you smart'</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>But most of us, canonically, specify params and default AR values with symbols, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">   post <span style="color:#ff3333; font-weight:bold;">:article</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:title <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Awesome blog post'</span>, <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'I will make you smart'</span><span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>So we get used to thinking about them as symbols.</p>
<p>This means we can make mistakes like this one I made recently.   Consider this block of code for a shopping cart model that pre-fills some fields for an associated Payment by pulling the address from the user's profile, to save the user re-typing their address:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ShoppingCart <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_one <span style="color:#ff3333; font-weight:bold;">:payment</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> build_default_payment<span style="color:#006600; font-weight:bold;">&#40;</span>options = <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;">&#41;</span> 
    <span style="color:#008000; font-style:italic;">#prepopulate the billing address from the profile and merge</span>
    <span style="color:#008000; font-style:italic;">#with params passed into options</span>
    build_payment<span style="color:#006600; font-weight:bold;">&#40;</span>prepopulated_fields.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>    
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> prepopulated_fields
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>addr = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">person</span>.<span style="color:#9900CC;">address</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#ff3333; font-weight:bold;">:billing_address_1</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> addr.<span style="color:#9900CC;">line_1</span>,
        <span style="color:#ff3333; font-weight:bold;">:billing_address_2</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> addr.<span style="color:#9900CC;">line_2</span>,
        <span style="color:#ff3333; font-weight:bold;">:city</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> addr.<span style="color:#9900CC;">city</span>,
        <span style="color:#ff3333; font-weight:bold;">:state</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> addr.<span style="color:#9900CC;">state</span>,
        <span style="color:#ff3333; font-weight:bold;">:zip</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> addr.<span style="color:#9900CC;">zipcode</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Looks great, right?  And if the user's address has a nil field (like no city, or no line_1), it will get overwritten by the hash merge.</p>
<p>Except not.  I specified symbol keys in prepopulated_fields, but the hash getting passed to build_default_payment's 'options' argument has string keys, because it's coming from params.  So the merge doesn't overwrite the value for :line_1, it simply adds a new key 'line_1'.  So, if a user has a profile address but hadn't entered a line_1 (just city and state), and then manually entered line_1 in the payment form to submit, the Payment build during the create action was getting this hash:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">build_payment<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>
   <span style="color:#ff3333; font-weight:bold;">:line_1</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>,
   <span style="color:#ff3333; font-weight:bold;">:city</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Pasadena'</span>,
   <span style="color:#ff3333; font-weight:bold;">:state</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">'CA'</span>,
   <span style="color:#ff3333; font-weight:bold;">:zipcode</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'91106'</span>
   <span style="color:#996600;">'line_1'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'100 Main St.'</span>.
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>ActiveRecord was respecting the :line_1 => nil from the profile, and not the 'line_1' => '100 Main St.' from params.  This meant that the user couldn't make payment!  The payment had validates_inclusion_of line_1, and even though it was typed into the form it was getting ignored because of the nil from his profile address.   Very frustrating for a user to manually type in a billing address and get back "Address Line 1 can't be blank." on every submit!</p>
<p>Nasty ... this one took a while to figure out.   Beware of this little foible of ActiveRecord!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2010/03/danger-activerecord-param-hashes-and-symbol-keys/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>HOWTO: Setting up CruiseControl.rb on Slicehost</title>
		<link>http://blog.lrdesign.com/2010/02/howto-setting-up-cruisecontrol-rb-on-slicehost/</link>
		<comments>http://blog.lrdesign.com/2010/02/howto-setting-up-cruisecontrol-rb-on-slicehost/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 00:47:46 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Guides]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=90</guid>
		<description><![CDATA[Continuous Integration is a key tool for collborative development, and CruiseControl.rb is the tool of choice for many Ruby and Rails teams, including us at Logical Reality. Unfortunately, setting up CC.rb for a team can be a relatively frustrating experience: this guide (the first of a series of HOWTOs by LRD) will walk you through [...]]]></description>
			<content:encoded><![CDATA[<p>Continuous Integration is a key tool for collborative development, and CruiseControl.rb is the tool of choice for many Ruby and Rails teams, including us at Logical Reality.</p>
<p>Unfortunately, setting up CC.rb for a team can be a relatively frustrating experience: this guide (the first of a series of HOWTOs by LRD) will walk you through every step of setting up a team instance of CruiseControl.rb on a low-cost server from <a href="http://slicehost.com">Slicehost</a>.</p>
<h2> Step 1: Lease a Ubuntu Slicehost account</h2>
<p>I recommend a 384 slice or a 512 slice, as 256MB or RAM is pretty light for anything involving a Rails application.   Our CI server runs on a 512 slice; if you are running it on a smaller slice please let us know how it performs.</p>
<p>I used Ubuntu 9.10 (Karmic) for this post.</p>
<h2>Step 2: Create a working user</h2>
<p>Slicehost configures slices with an active root account - definitely a Ubuntu no-no - and no user account. Ick!  Let's start by creating a user account with sudo access to do everything from.   Log in as root using the information Slicehost sends you, run this (replace 'usename' with whatever name you like) and fill in the information it asks for:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> <span style="color: #666666; font-style: italic;"># adduser username</span></pre></div></div>

<p>Then edit /etc/sudoers and add this line to the bottom of the file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">username    <span style="color: #007800;">ALL</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span>ALL<span style="color: #7a0874; font-weight: bold;">&#41;</span> ALL</pre></div></div>

<p>Log out, and log back in as the user you've now configured, to make sure it work.</p>
<h2>Step 3: Installing packages and gems</h2>
<p>Reset your timezone:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> dpkg-reconfigure tzdata</pre></div></div>

<p>Install a whole bunch of packages you'll want for running Rails applications and hosting CruiseControl:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #c20cb9; font-weight: bold;">locate</span> emacs git-core ruby build-essential \
libopenssl-ruby ruby1.8-dev irb  apache2 apache2-mpm-prefork \
apache2-prefork-dev sqlite3 rubygems mysql-server mysql-client</pre></div></div>

<p>Go grab a cup of coffee while those install.  The mysql install will ask you to set a root password.   Do so, and write it down for later use.  When all the installs are done, come back and install the ruby gems you'll be needing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> sqlite3-ruby passenger mysql metric_fu reek roodi</pre></div></div>

<h2>Step 4: Assorted server configuration</h2>
<p>Add this line to the bottom of your ~/.profile to put your gems in your path:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$PATH</span>:/var/lib/gems/1.8/bin/&quot;</span></pre></div></div>

<p>And source it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. ~<span style="color: #000000; font-weight: bold;">/</span>.profile</pre></div></div>

<p>Some assorted config: set up the passenger module for Apache, set your hostname, and make /etc/hosts readable.   (For some bizarre reason, /etc/hosts was only readable by root on my slice, and that has a tendency to break things down the road).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>passenger-install-apache2-module
<span style="color: #c20cb9; font-weight: bold;">sudo</span> emacs <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">hostname</span>  <span style="color: #666666; font-style: italic;"># set it to &quot;your.hostname.com&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">hostname</span> <span style="color: #660033;">-F</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">hostname</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> a+r <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>hosts</pre></div></div>

<h2>Step 5:  Configure Passenger and Apache</h2>
<p>We'll run CruiseControl.rb with Apache and Passenger.   Start by enabling the Passenger module.   The command below will walk you through a super-easy configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.8</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>passenger-install-apache2-module</pre></div></div>

<p>When the command completes, it will give you three lines to paste into your apache config, they should look pretty much like these below.   Put these lines at the top of /etc/apache2/apache2.conf.  I included the hostname I set in the previous step as ServerName.</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">LoadModule</span> passenger_module /var/lib/gems/<span style="color: #ff0000;">1.8</span>/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
PassengerRoot /var/lib/gems/<span style="color: #ff0000;">1.8</span>/gems/passenger-2.2.8
PassengerRuby /usr/bin/ruby1.8   
&nbsp;
<span style="color: #00007f;">ServerName</span> your.hostname.com</pre></div></div>

<p>To set up the application itself, edit /etc/apache2/sites-available/default to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:<span style="color: #ff0000;">80</span>&gt;
        <span style="color: #00007f;">ServerAdmin</span> administrator@your-email-domain.com
        <span style="color: #00007f;">DocumentRoot</span> /u/apps/cruisecontrol/public
        RailsEnv production
        RailsBaseURI /
        <span style="color: #00007f;">ServerName</span> &lt;IP Address from Slicehost&gt;
        <span style="color: #00007f;">ServerAlias</span> your.hostname.com
        <span style="color: #00007f;">SetEnv</span> PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/var/lib/gems/<span style="color: #ff0000;">1.8</span>/bin/
&nbsp;
        <span style="color: #00007f;">ErrorLog</span> /var/log/apache2/error.log
&nbsp;
        <span style="color: #adadad; font-style: italic;"># Possible values include: debug, info, notice, warn, error, crit,</span>
        <span style="color: #adadad; font-style: italic;"># alert, emerg.</span>
        <span style="color: #00007f;">LogLevel</span> warn
&nbsp;
        <span style="color: #00007f;">CustomLog</span> /var/log/apache2/access.log combined
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</pre></div></div>

<p>Make a home for the app.  (I use /u/apps/<appname> as a convention for apps in apache. Use whatever you like, but make sure your DocumentRoot in your config file above matches.)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>u<span style="color: #000000; font-weight: bold;">/</span>apps</pre></div></div>

<h2>Step 6: Download and install CruiseControl.rb</h2>
<p>Download cruisecontrol.rb from RubyForge  (Check for the current version first; it was 1.4.0 when I installed), and give ownership to the web user www-data:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>u<span style="color: #000000; font-weight: bold;">/</span>apps
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>rubyforge.org<span style="color: #000000; font-weight: bold;">/</span>frs<span style="color: #000000; font-weight: bold;">/</span>download.php<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">59598</span><span style="color: #000000; font-weight: bold;">/</span>cruisecontrol-1.4.0.tgz
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxf</span> cruisecontrol-1.4.0.tgz     
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> cruisecontrol-1.4.0 cruisecontrol   
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chown</span> <span style="color: #660033;">-R</span> :www-data cruisecontrol</pre></div></div>

<p>Give environment.rb to the web user; this prevents an  Errno::EACCES accessing environment.rb from Passenger (see discussion at <a href="http://forum.slicehost.com/comments.php?DiscussionID=3668">this forum post</a>).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chown</span> www-data:www-data config<span style="color: #000000; font-weight: bold;">/</span>environment.rb</pre></div></div>

<p>Turn off the built-in htaccess, it will break Passenger:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> public<span style="color: #000000; font-weight: bold;">/</span>.htaccess public<span style="color: #000000; font-weight: bold;">/</span>.htaccess-disabled
<span style="color: #7a0874; font-weight: bold;">cd</span> config  
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> site_config.rb_example site_config.rb</pre></div></div>

<h2>Step 7: Setting up the user environment</h2>
<p>CruiseControl.rb prefers, by default, to put project builds in the running user's ~/.cruise directory.   This is unfortunate because the standard user for running Apache, www-data, doesn't have a user directory!  There are ways to override this, but I've found that they cause significant problems down the line.    </p>
<p>An example of the problem is letting CC.rb check out your source code.   If you authenticate access to GitHub or another code repository with SSH, CC.rb &#8212; running as www-data &#8212; won't be able access your repo since www-data doesn't have a ~/.ssh directory to put the keys in!   </p>
<p>After much hacking, I came to the unhappy conclusion that the best solution is simply to let CruiseControl.rb have its way and give user www-data a home directory.  Boo, hiss, but here we go:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 stop   
<span style="color: #c20cb9; font-weight: bold;">sudo</span> usermod <span style="color: #660033;">-d</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>www-data www-data      
<span style="color: #c20cb9; font-weight: bold;">sudo</span> usermod <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">bash</span> www-data
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 start</pre></div></div>

<p> If you give www-data standard config files as well, then you can set the PATH so that user www-data can find your gems, and you can set up ssh keys so that CruiseControl.rb can securely check out projects from GitHub or whatever source code repository you're using:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-r</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>skel <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>www-data 
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chown</span> www-data:www-data <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>www-data
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">su</span> www-data               
<span style="color: #7a0874; font-weight: bold;">cd</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh  
<span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>.ssh
<span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa
<span style="color: #c20cb9; font-weight: bold;">cat</span> id_rsa.pub</pre></div></div>

<p>Add this line to the bottom of ~/www-data/.profile:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$PATH</span>:/var/lib/gems/1.8/bin/&quot;</span></pre></div></div>

<p>Re-start Apache:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p>At this point, you should be able to load CruiseControl.rb in a web browser at the IP address given to you by Slicehost, or at the domain name if you've set up DNS and it's resolving.  Congratulations, you have CC.rb up and running!  One last thing to configure.</p>
<p>Running CruiseControl.rb will have created a configuration directory . ~www-data/.cruise.   You'll want to edit ~www-data/.cruise/site_config.rb to set two options.  Uncomment and set appropriate values for this line:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> Configuration.<span style="color:#9900CC;">email_from</span> = <span style="color:#996600;">'cruisecontrolrb@mydomain.com'</span>
 Configuration.<span style="color:#9900CC;">dashboard_url</span> = <span style="color:#996600;">'http://my.cruisecontrolrb.host/'</span></pre></div></div>

<p>Okay, it's time to get a project installed!</p>
<h2>Step 8: Setting up your first project</h2>
<p>I'll use Logical Reality's open-source project, Convection, as an example project for CruiseControl.rb.  This works best if you run it as user 'www-data'.</p>
<p>The command for adding a new project is really simple:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>u<span style="color: #000000; font-weight: bold;">/</span>apps<span style="color: #000000; font-weight: bold;">/</span>cruisecontrol 
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">su</span> www-data
.<span style="color: #000000; font-weight: bold;">/</span>cruise add Convection <span style="color: #660033;">-r</span> <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>LRDesign<span style="color: #000000; font-weight: bold;">/</span>Convection.git <span style="color: #660033;">-s</span> <span style="color: #c20cb9; font-weight: bold;">git</span></pre></div></div>

<p>This will set up the build in ~www-data/.cruise/projects/Convection.</p>
<p>Create a test database for the application.   For Convection, I'm going to use mysql, and prefix my database name with 'ci' for Continuous Integration.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqladmin <span style="color: #660033;">-u</span> root <span style="color: #660033;">-p</span> create ci_convection</pre></div></div>

<p>We don't want to put a functioning database.yml in our GitHub repository, but at the same time we want CruiseControl.rb to be able to build and test the app without help from the user.   For all our Rails projects, we use a custom rake task that generates a database.yml from command-line arguments, then rebuilds the database, run the specs, and generate output with metric_fu.   For an example of how to do this, look at our <a href="http://github.com/LRDesign/Convection/blob/master/lib/tasks/integration.rake">integration.rake</a> and <a href="http://github.com/LRDesign/Convection/blob/edge/config/database.yml.ci.erb">ERB database.yml template</a> from Convection.</p>
<p>To configure CruiseControl.rb  to run Convection this, we need to add that task to the configuration file for this project.  Edit ~www-data/.cruise/projects/Convection/cruise_config.rb so that it looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
Project.<span style="color:#9900CC;">configure</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>project<span style="color:#006600; font-weight:bold;">|</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Send email notifications about broken and fixed builds to email1@your.site, email2@your.site (default: send to nobody)</span>
  project.<span style="color:#9900CC;">email_notifier</span>.<span style="color:#9900CC;">emails</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'sysadmin@lrdesign.com'</span>, <span style="color:#996600;">'judson@lrdesign.com'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Set email 'from' field</span>
  project.<span style="color:#9900CC;">email_notifier</span>.<span style="color:#9900CC;">from</span> = <span style="color:#996600;">'sysadmin@lrdesign.com'</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Build the project by invoking rake task 'custom'</span>
  <span style="color:#008000; font-style:italic;"># project.rake_task = 'custom'</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Build the project by invoking shell script &quot;build_my_app.sh&quot;. Keep in mind that when the script is invoked,</span>
  <span style="color:#008000; font-style:italic;"># current working directory is &lt;em&gt;[cruise&amp;nbsp;data]&lt;/em&gt;/projects/your_project/work, so if you do not keep build_my_app.sh</span>
  <span style="color:#008000; font-style:italic;"># in version control, it should be '../build_my_app.sh' instead</span>
  <span style="color:#008000; font-style:italic;"># project.build_command = 'build_my_app.sh'</span>
  project.<span style="color:#9900CC;">build_command</span> = <span style="color:#996600;">'rake ci:run[localhost,root,&lt;YOUR_MYSQL_ROOT_PASSWORD&gt;,ci_convection] --trace RAILS_ENV=test'</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Ping Subversion for new revisions every 5 minutes (default: 30 seconds)</span>
  <span style="color:#008000; font-style:italic;"># project.scheduler.polling_interval = 5.minutes</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h2>Step 9: There is no step nine!</h2>
<p>Okay, so it's not the simplest thing in the world to set up.  But if you've done everything above correctly, you should have a running server your team can use for continuous integration.  If you've included metric_fu in your build task, you should get both test output and a wealth of useful code metrics.  </p>
<p>Did this sequence work for you?   Did I omit a step or misspell a command?   Let me know in comments, and I'll update/correct the post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2010/02/howto-setting-up-cruisecontrol-rb-on-slicehost/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>RailsTutorial.org launched</title>
		<link>http://blog.lrdesign.com/2009/12/railstutorial-org-launched/</link>
		<comments>http://blog.lrdesign.com/2009/12/railstutorial-org-launched/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 18:56:28 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=83</guid>
		<description><![CDATA[The new Ruby on Rails Tutorial book and website by Michael Hartl has launched at RailsTutorial.org.   Hartl is the author of RailsSpace and cofounder of the Insoshi Ruby on Rails social networking platform. Logical Reality did the logo and layout design work for Rails Tutorial. &#160;]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-84" title="rails-tutorial-logo-2" src="http://blog.lrdesign.com/wp-content/uploads/2009/12/rails-tutorial-logo-2-300x251.png" alt="rails-tutorial-logo-2" width="300" height="251" />The new <a href="http://www.railstutorial.org/">Ruby on Rails Tutorial book</a> and website by Michael Hartl has launched at <a href="http://railstutorial.org/">RailsTutorial.org</a>.   Hartl is the author of RailsSpace and cofounder of the Insoshi Ruby on Rails social networking platform.</p>
<p>Logical Reality did the logo and layout design work for Rails Tutorial.</p>
<div style="clear: both;">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2009/12/railstutorial-org-launched/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using link_to (or other helper methods) in a controller</title>
		<link>http://blog.lrdesign.com/2009/05/using-link_to-or-other-helper-methods-in-a-controller/</link>
		<comments>http://blog.lrdesign.com/2009/05/using-link_to-or-other-helper-methods-in-a-controller/#comments</comments>
		<pubDate>Wed, 06 May 2009 23:40:10 +0000</pubDate>
		<dc:creator>Evan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[controllers]]></category>
		<category><![CDATA[helper methods]]></category>
		<category><![CDATA[link_to]]></category>

		<guid isPermaLink="false">http://blog.lrdesign.com/?p=57</guid>
		<description><![CDATA[This one was a big aggravator to me lately. I have one controller that needs to call link_to and url_for, which are normally helper methods you'd call from a view. However, in this case during certain modifications to a record, I actually need to append user-visible HTML links to a block of HTML stored in [...]]]></description>
			<content:encoded><![CDATA[<p>This one was a big aggravator to me lately.   I have one controller that needs to call link_to and url_for, which are normally helper methods you'd call from a view.   However, in this case during certain modifications to a record, I actually need to append user-visible HTML links to a block of HTML stored in that object, or possibly another one.</p>
<p>Specifically, I needed to put annotations in the description of a work order object that said, for example "this work order was escalated from <a href="/tasks/293">Problem Report 293</a>.   This was done in a create action that redirected at the end and never rendered a view, so I really did need to generate that link in the controller.   And for consistency with the rest of the application, I wanted to generate the link with link_to(@task).</p>
<p>Now, ActionView::Helpers::UrlHelper is not loaded in a Rails controller, even if you've put helper :all in application.rb (application_controller.rb in newer versions).   So, when I tried to use link_to in the controller, I got an error:</p>
<p><code>NoMethodError: undefined method `link_to' for #<br />
/Users/evan/Development/Ruby/eclipticdb/app/helpers/tasks_helper.rb:64:in `task_link'<br />
/Users/evan/Development/Ruby/eclipticdb/app/controllers/tasks_controller.rb:103:in `escalate'<br />
... etc ...<br />
</code></p>
<h2>The first fix - but with a problem</h2>
<p>A year ago, I fixed this just by adding <code>include ActionView::Helpers::UrlHelper</code> at the top of that controller.  This worked great ... for a while.</p>
<p>Lately, I've been rewriting this application into a RESTful style - it had previously been a controller/action style application.  In the process, I started linking things with resource paths and polymorphic paths ... a lot of <code>link_to @task</code> and <code>edit_polymorphic_path(@task)</code> sorts of bits.   And these started breaking.   I began seeing this mysterious error:</p>
<h4>Error:</h4>
<p><code>You have a nil object when you didn't expect it!<br />
The error occurred while evaluating nil.url_for</p>
<p>... some code here that calls a link_to ...</p>
<p>Trace of template inclusion: /tasks/_task_panel.html.erb, /tasks/_task_tabbed_panel.html.erb, /tasks/index.html.erb</p>
<p>RAILS_ROOT: /Users/evan/Development/Ruby/eclipticdb<br />
Application Trace | Framework Trace | Full Trace</p>
<p>vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:71:in `send'<br />
vendor/rails/actionpack/lib/action_view/helpers/url_helper.rb:71:in `url_for'</code></p>
<p>This one was a real bitch to debug, I have to say.   The line in question that was failing in url_helper.rb said this: <code>url = @controller.send(:url_for, options)</code>.   Clearly, @controller was nil ... which was very bizarre, because I never interact with that instance variable anywhere.</p>
<p>I thrashed around trying to find the cause of this error for quite some time.  Eventually I realized that the link_to method was only failing when called from a view in TasksController, and not from any other controller.  And then I realized that TasksController was the one where, a year ago, I'd put <code>include ActionView::Helpers::UrlHelper</code> at the top.   Somehow, including that helper in the controller was nullifying <code>@controller</code> when those helper method we called from within the view.   I removed the include and my polymorphic and resource links all started working again.</p>
<h2>Now back to the original problem!</h2>
<p>Of course, that then left me back with the problem I'd had a year ago ... needing to use link_to from within the controller and having no way to do it.   After a fair bit of googling around I found <a href="http://www.neeraj.name/blog/articles/740-using-helpers-in-controllers">this post</a> from Neeraj, which had an interesting approach -- but a commenter had suggested a much easier solution:</p>
<p>[sourcecode language='ror']self.class.helpers.link_to[/sourcecode]</p>
<p>I'm not certain where one would find this in the docs, but it does seem to have solved my problem for now.  Onward and upward!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lrdesign.com/2009/05/using-link_to-or-other-helper-methods-in-a-controller/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

