<?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>Russell Quinn &#187; Ruby on Rails</title>
	<atom:link href="http://www.russellquinn.com/blog/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.russellquinn.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 01 Jul 2010 08:30:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Customising form controls in Rails</title>
		<link>http://www.russellquinn.com/blog/2008/02/03/customing-form-controls-in-rails/</link>
		<comments>http://www.russellquinn.com/blog/2008/02/03/customing-form-controls-in-rails/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 22:23:55 +0000</pubDate>
		<dc:creator>Russell Quinn</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.russellquinn.com/?p=85</guid>
		<description><![CDATA[Here is a simple way to implement custom form controls while still using Rails&#8217; standard helpers. For this example, we&#8217;ll implement a two-state graphical alternative to a check-box (usually implemented by the operating system and uncustomisable via CSS), which represents a standard boolean field in your database model.
Normally we&#8217;d do something like in our view:

 [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple way to implement custom form controls while still using Rails&#8217; standard helpers. For this example, we&#8217;ll implement a two-state graphical alternative to a check-box (usually implemented by the operating system and uncustomisable via CSS), which represents a standard boolean field in your database model.</p>
<p>Normally we&#8217;d do something like in our view:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">    <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">form_for</span> <span style="color:#0066ff; font-weight:bold;">@model</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:create</span> <span style="color:#006600; font-weight:bold;">&#125;</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> <span style="color:#006600; font-weight:bold;">%&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#5A0A0A; font-weight:bold;">check_box</span> <span style="color:#ff3333; font-weight:bold;">:someBool</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;OK&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>But we&#8217;d like to use something a little like this to represent our field instead:</p>
<p><img src="http://www.aslong.co.uk/blog/wp-content/uploads/signupitemactive.gif" alt="Active" /><br />
<img src="http://www.aslong.co.uk/blog/wp-content/uploads/signupiteminactive.gif" alt="Inactive" /></p>
<p>Firstly the images are added to our Rails view via the helpers, with the addition of assigning each one an ID and an inline style to hide the one opposing the required default state:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">    <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">image_tag</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;signUpItemActive.gif&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:size</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;13x18&quot;</span>,
        <span style="color:#ff3333; font-weight:bold;">:alt</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Active&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;checkActive&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">image_tag</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;signUpItemInActive.gif&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:size</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;13x18&quot;</span>,
        <span style="color:#ff3333; font-weight:bold;">:alt</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Inactive&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;checkInactive&quot;</span>,
        <span style="color:#ff3333; font-weight:bold;">:style</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;display:none;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>These are then both wrapped in a single anchor tag, which is given an <em>onclick</em> handler to a JavaScript function.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">    &lt;a href=&quot;#&quot; onclick=&quot;toggleCheckBox(); return false;&quot;&gt;
         <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">image_tag</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;signUpItemActive.gif&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:size</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;13x18&quot;</span>,
            <span style="color:#ff3333; font-weight:bold;">:alt</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Active&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;checkActive&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
         <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">image_tag</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;signUpItemInActive.gif&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:size</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;13x18&quot;</span>,
            <span style="color:#ff3333; font-weight:bold;">:alt</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Inactive&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;checkInactive&quot;</span>,
            <span style="color:#ff3333; font-weight:bold;">:style</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;display:none;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    &lt;/a&gt;</pre></div></div>

<p>The next step is to modify our standard Rails form to assign the check-box an ID, and hide it with some more inline CSS styling. This means it won&#8217;t be visible, but will continue to send its value when the form is submitted:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#5A0A0A; font-weight:bold;">check_box</span> <span style="color:#ff3333; font-weight:bold;">:someBool</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;hiddenCheckBox&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:style</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;display:none;&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>The JavaScript function can then be defined in Application.js (this could of course be modified to take the IDs as parameters):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">function</span> toggleCheckBox<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        elementActive <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checkActive'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        elementInactive <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checkInactive'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        elementHidden <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hiddenCheckBox'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elementActive <span style="color: #339933;">&amp;&amp;</span> elementInactive <span style="color: #339933;">&amp;&amp;</span> elementHidden<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> state <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>elementActive.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            elementHidden.<span style="color: #660066;">checked</span> <span style="color: #339933;">=</span> state<span style="color: #339933;">;</span>
            elementActive.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> state <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;inline&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
            elementInactive.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> state <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;inline&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>There we go! Clicking the dummy check-box graphic will toggle the images and set the value in the hidden check-box at the same time. There is only one remaining step and that is to set the default state of the images to match the existing value of the boolean field. Without this the graphics and the form are likely to get out of sync. Even if the form is for creating a new record, the page will be shown again in the event of a validation error.</p>
<p>Another JavaScript function can take care of this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> setCheckBox<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        elementActive <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checkActive'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        elementInactive <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checkInactive'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        elementHidden <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hiddenCheckBox'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elementActive <span style="color: #339933;">&amp;&amp;</span> elementInactive <span style="color: #339933;">&amp;&amp;</span> elementHidden<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> state <span style="color: #339933;">=</span> elementHidden.<span style="color: #660066;">checked</span><span style="color: #339933;">;</span>
            elementActive.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> state <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;inline&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
            elementInactive.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> state <span style="color: #339933;">?</span> <span style="color: #3366CC;">&quot;inline&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then call this from the page&#8217;s body <em>onLoad</em> event:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">    <span style="color:#006600; font-weight:bold;">&lt;</span>body onload=<span style="color:#996600;">&quot;setCheckBox();&quot;</span><span style="color:#006600; font-weight:bold;">&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.russellquinn.com/blog/2008/02/03/customing-form-controls-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Rails applications</title>
		<link>http://www.russellquinn.com/blog/2008/01/30/multiple-rails-applications/</link>
		<comments>http://www.russellquinn.com/blog/2008/01/30/multiple-rails-applications/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 00:37:02 +0000</pubDate>
		<dc:creator>Russell Quinn</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.russellquinn.com/?p=82</guid>
		<description><![CDATA[We are midway through a large project at the moment. It&#8217;s our biggest Ruby on Rails project for a client to date and consists of a front end and an administration area based on our DATA CARTON technology. Early on we faced several organisational conflicts between these two opposing forces.
Our DATA CARTON framework was adverse [...]]]></description>
			<content:encoded><![CDATA[<p>We are midway through a large project at the moment. It&#8217;s our biggest Ruby on Rails project for a client to date and consists of a front end and an administration area based on our <a href="http://www.datacarton.com">DATA CARTON</a> technology. Early on we faced several organisational conflicts between these two opposing forces.</p>
<p>Our DATA CARTON framework was adverse to settling in directly with its public facing companion, our models (while obviously the same) were actually required to be configured in subtly different ways, and with multiple developers and designers working together, we decided to split them into two applications both pointing at the same database.</p>
<p>While these logical units promised simpler security, cleaner directory structures and more streamlined development, we hadn&#8217;t tried this before but decided to just forge ahead anyway. These are the problems we encountered and how we solved them:</p>
<p><strong>1. Logins</strong></p>
<p>Our front end is community-based and all users of the system from newsletter subscribers to administrators share a <em>users</em> table. The first thing we did after pointing our database.yml file at the same database was to try logging in to both applications with the test user we had created. After only a 50% success rate, we traced the problem to our password encryption method that uses a mixture of the user&#8217;s password choice, a salt and an application specific key string. Different keys are going to result in different hashes and incompatible authentication routines in the applications. A quick switch to using identical strings and we were successfully creating users and logging-in in all possible combinations.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">encrypt</span><span style="color:#006600; font-weight:bold;">&#40;</span>pass, salt<span style="color:#006600; font-weight:bold;">&#41;</span>
        finalString = pass <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'somekey'</span> <span style="color:#006600; font-weight:bold;">+</span> salt
        <span style="color:#6666ff; font-weight:bold;">Digest::SHA1</span>.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span>finalString<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><strong>2. Sessions</strong></p>
<p>Our next thought was &#8217;sharing sessions would be nice&#8217;. Linking administrators directly to the backend from their frontend toolbar without having to login again, passing secret messages back and forth, that sort of thing. Rails offers cookie-only sessions or database based ones. Using the more data-sensitive one is a simple matter of uncommenting these lines from the application&#8217;s environment.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">    config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">session_store</span> = <span style="color:#ff3333; font-weight:bold;">:active_record_store</span></pre></div></div>

<p>Then in the same file (environment.rb) you&#8217;ll find your session security key and secret:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">    config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#5A0A0A; font-weight:bold;">session</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#ff3333; font-weight:bold;">:session_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'some_secret_key'</span>,
        <span style="color:#ff3333; font-weight:bold;">:secret</span>      <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'some_secret_hash'</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Rails generates both of these automatically when creating your application. They are sent with every non-GET request (i.e. PUT, POST, DELETE) to verify your session and protect against cross-site forgery (there&#8217;s actually a few fiddly issues with getting in-place edits working while using this method, but I&#8217;ll save that for another post).</p>
<p>The important thing here is to again make sure all keys and secrets are consistent across both applications. So pick one and copy it across. Once you&#8217;ve synchronised your environment.rb session settings, you&#8217;ll need to uncomment and duplicate your session secret from the top of controllers\application.rb too. Now you&#8217;re sharing sessions!</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">	protect_from_forgery <span style="color:#ff3333; font-weight:bold;">:secret</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'some_secret_hash'</span></pre></div></div>

<p><strong>3. Subdomains</strong></p>
<p>We quickly realised that our session sharing was not going to be as smooth as first anticipated, as we typically run our client&#8217;s backends on an <em>admin</em> subdomain (i.e. http://admin.abcdefg.com) and as cookies don&#8217;t take kindly to being requested by subdomains that haven&#8217;t sent them, we still didn&#8217;t have our single login functionality.</p>
<p>After much brainstorming and hunting around, we eventually found this genius configuration option that (notice the all important &#8216;.&#8217; prefix) makes the cookie available to all subdomains on a domain. Finally we had truly shared sessions.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">    <span style="color:#6666ff; font-weight:bold;">ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS</span>.
        <span style="color:#5A0A0A; font-weight:bold;">update</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#ff3333; font-weight:bold;">:session_domain</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'.abcdefg.com'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><strong>4. Migrations / Models / Helpers / Deployment</strong></p>
<p>The remaining issues are ones we are still facing and gradually solving. These tend to be less of a technical matter and more of an organisational one. We&#8217;re quickly establishing rules for how we order the migration files, share some models while keeping the security sensitive ones separate, bundling all of our helpers up into libraries and unifying deployment. We&#8217;re not quite there yet, but I&#8217;ll be sure to share our assumptions and solutions soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.russellquinn.com/blog/2008/01/30/multiple-rails-applications/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
