<?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>jrmehle.com &#187; Rails</title>
	<atom:link href="http://jrmehle.com/archives/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://jrmehle.com</link>
	<description>Through the Eyes of a Dork</description>
	<lastBuildDate>Sun, 31 Jan 2010 19:30:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The Check-All Box in Prototype.js</title>
		<link>http://jrmehle.com/2010/01/08/the-check-all-box-in-prototype-js/</link>
		<comments>http://jrmehle.com/2010/01/08/the-check-all-box-in-prototype-js/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 03:48:48 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://jrmehle.com/?p=693</guid>
		<description><![CDATA[While working on a recent project at my job, I ran into a situation where I needed a form with a vertical column of checkboxes. At the top of them, I wanted to place a checkbox that when checked, would toggle the checkboxes below it.
I happen to be using a Rails app that has Prototype.js. [...]]]></description>
			<content:encoded><![CDATA[<p>While working on a recent project at my job, I ran into a situation where I needed a form with a vertical column of checkboxes. At the top of them, I wanted to place a checkbox that when checked, would toggle the checkboxes below it.</p>
<div class="wp-caption aligncenter" style="width: 168px"><img title="Checkboxes" src="http://farm5.static.flickr.com/4069/4257781489_cdcda341ab_o.jpg" alt="Checkboxes" width="158" height="171" /><p class="wp-caption-text">Checkboxes</p></div>
<p>I happen to be using a Rails app that has Prototype.js. The javascript to hook into the top-most checkbox and make it do our bidding looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Event.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span>window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</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: #003366; font-weight: bold;">var</span> select_all_box <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'select_all_box'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>select_all_box<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> checkboxes <span style="color: #339933;">=</span> select_all_box.<span style="color: #660066;">up</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getInputs</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'checkbox'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    checkboxes.<span style="color: #660066;">shift</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// throw out select all box</span>
&nbsp;
    select_all.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</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>
      checkboxes.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        e.<span style="color: #660066;">checked</span> <span style="color: #339933;">=</span> select_all.<span style="color: #660066;">checked</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>As you can see from the outer-most <strong>observe</strong> call, I&#8217;ve written this to be unobtrusive. With the page finished loading, we look for the checkbox we want to enhance. The HTML for the above screenshot is just a simple <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">&lt;table&gt;</span> contained within a <span style="font-family: Consolas, Monaco, 'Courier New', Courier, monospace; line-height: 18px; font-size: 12px; white-space: pre;">&lt;form&gt;</span>.  From there, grab all of the checkboxes in the form and then throw away the first one. Then create an event listener for the checkbox that iterates over the rest of the checkboxes matching them to the current state of the check-all box. Every time you check or uncheck the box at the top, all of the checkboxes below will follow.</p>
]]></content:encoded>
			<wfw:commentRss>http://jrmehle.com/2010/01/08/the-check-all-box-in-prototype-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Safari Top Sites + Passenger in Development Problem</title>
		<link>http://jrmehle.com/2009/11/18/the-safari-top-sites-passenger-in-development-problem/</link>
		<comments>http://jrmehle.com/2009/11/18/the-safari-top-sites-passenger-in-development-problem/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 20:32:55 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Browsers]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://jrmehle.com/?p=668</guid>
		<description><![CDATA[If you do more than a minimal amount of work on a number of Ruby web projects, you&#8217;ve more than likely sat down to figure out how to use Phusion Passenger in development. It makes switching between several projects a snap. Ruby and Rails developers have also been known to disproportionately favor Apple machines for [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption aligncenter" style="width: 560px"><a href="http://www.flickr.com/photos/jaredvolkl/4115031545"><img class="   " src="http://farm3.static.flickr.com/2648/4115031545_ae1c95638f.jpg" alt="" width="550" height="330" /></a><p class="wp-caption-text">Safari&#39;s Top Sites</p></div>
<p>If you do more than a minimal amount of work on a number of Ruby web projects, you&#8217;ve more than likely sat down to figure out how to use Phusion Passenger in development. It makes switching between several projects a snap. Ruby and Rails developers have also been known to disproportionately favor Apple machines for their development workhorses. Continue reading if you fit the mold.With the release of Safari 4 came the nifty &#8220;wall of screens&#8221; feature known as Top Sites. This feature takes your most visited sites and displays them in a nice grid. It&#8217;s more pretty than useful, but I&#8217;d be lying if I didn&#8217;t use it at least once a day.</p>
<p>Lately, the combination of the previously described tools have been causing me pain. One of the things developers tend to do when developing sites is refresh them; A LOT! So typically there are at least a couple of my projects that wind up as Top Sites in Safari. The problem with this is Safari likes to go update those pretty looking screenshots every time you view the Top Sites screen (and randomly at other times). Because I have this set to display when a new tabs open, this is every time I open a blank tab. In other words, very often.</p>
<p>If you haven&#8217;t put two and two together by now, what happens when I open a tab is Passenger spawns processes to handle the requests Safari makes (just so it can update it&#8217;s screenshots). This ends up consuming several hundred megs of RAM and spikes the CPU load. If you happen to have a memory leak in your application, be prepared to launch Activity Monitor in a hurry to kill the ruby process that is spiraling out of control.</p>
<div class="wp-caption aligncenter" style="width: 166px"><a href="http://www.flickr.com/photos/jaredvolkl/4115031609"><img title="Delete/Pin Button" src="http://farm3.static.flickr.com/2673/4115031609_87b2c57c7e_o.gif" alt="" width="156" height="105" /></a><p class="wp-caption-text">Delete/Pin Button</p></div>
<p>As a solution, you can easily hit the edit button and blacklist the worst offenders, but you have to do so for each unique URL. There is no way to say don&#8217;t ever list pages from &#8220;localproject.dev&#8221; or even &#8220;*.dev&#8221;. There is also a way to temporarily stop Safari from trying to update the images, &#8216;Command + .&#8217;</p>
]]></content:encoded>
			<wfw:commentRss>http://jrmehle.com/2009/11/18/the-safari-top-sites-passenger-in-development-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Switching From restful_authentication to authlogic</title>
		<link>http://jrmehle.com/2009/09/02/switching-from-restful_authentication-to-authlogic/</link>
		<comments>http://jrmehle.com/2009/09/02/switching-from-restful_authentication-to-authlogic/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:59:54 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://jrmehle.com/?p=647</guid>
		<description><![CDATA[Dear RA,
We&#8217;ve had some good times. You were my first and only authentication plugin. I&#8217;ll always remember you, but it&#8217;s time to move on. There&#8217;s this hot, young plugin on the block named AL that I&#8217;ve fallen for. So long. *tear*
Rick Olsen&#8217;s restful_authentication is out and Ben Johnson&#8217;s authlogic is in as the hot Ruby [...]]]></description>
			<content:encoded><![CDATA[<p><em>Dear RA,</em></p>
<p><em>We&#8217;ve had some good times. You were my first and only authentication plugin. I&#8217;ll always remember you, but it&#8217;s time to move on. There&#8217;s this hot, young plugin on the block named AL that I&#8217;ve fallen for. So long. *tear*</em></p>
<p>Rick Olsen&#8217;s <strong>restful_authentication</strong> is out and Ben Johnson&#8217;s <strong>authlogic</strong> is in as the hot Ruby (and/or Rails) authentication module these days. No disrespect to Rick and restful_authentication. It&#8217;s got sound security principles behind it, but dealing with the implementation can be a headache. If you&#8217;re unfamiliar with restful_authentication, the first thing you would do after installing the plugin is run some generator scripts. These scripts create a bunch of code in your Rails project. Where that becomes a problem is if you have to modify the code and then later want to run the generator again.</p>
<p>Authlogic takes the stance that you should never have to run a generator. It also has a lot of auto-configuration (based on the fields found in your user model). Sessions in authlogic make use of the familiar, standard RESTful controller. The gem itself is also very well organized with things being broken down into modules. The only downside I&#8217;ve comes across is I can&#8217;t find a page to show me all of the configuration options at once; hardly something to pick on.</p>
<p>Instead of trying to write the be-all end-all guide, this will be more of a case study.  The nature of using a configurable generator, as restful_authentication provides, is that it would be tough to cover all cases. In addition, my app had some closely related components, such as password resetting, that needed to also be changed.</p>
<p>The first thing I did was install authlogic as a gem and pull it into my application. If you&#8217;ve spent any time with Rails 2.2 or higher, then this should be old hat for you by now.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">sudo gem install authlogic</pre></div></div>

<p>Then add the following to your config/environment.rb file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'authlogic'</span></pre></div></div>

<p>In your user model, add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">acts_as_authentic <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span>
  c.<span style="color:#9900CC;">transition_from_restful_authentication</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>With authlogic in place, you can remove restful_authentication. The plugin directory can be removed from vendor/plugins. You will also need to remove the lib/authenticated_system.rb and lib/authenticated_test_helper.rb files as well as the includes for them. These should be in application_controller.rb and test_helper.rb respectively if you followed restful_authentication&#8217;s instructions.</p>
<p>From here, a number of things still need to happen and it doesn&#8217;t much matter the order. There need to be some database changes to the users table. A controller for sessions needs to be added or an existing one needs to be modified. The test suite will be severely broken with a large number of tests failing or generating errors. There is also a lot of code to clean up in the user model and controller. As mentioned earlier, there may also be some related pieces which need clean up or conversion such as password resets.</p>
<p>What I ended up doing was half-following <a href="http://github.com/binarylogic/authlogic_example/tree/master">Ben Johnson&#8217;s guide</a> for setting up Authlogic and adapting it to the existing code. In this process I stripped out all of the generated code from the user model and controller, generated a transition migration, and copied a few views for the users_controller.rb from the sample application.</p>
<p>The sessions_controller from restful_authentication could be modified, however, I found it easier to start from scratch. I copied the controller and views from the authlogic setup guide and made some small changes to the views to adapt them to my layout. You will also need the following model at minimum:</p>

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

<p>Because you inherit from Authlogic::Session::Base, there is no actual code in the session model. Some people like to work with the session variables through a model, you can add this code if you&#8217;d like but the default should get you all you need to begin.</p>
<p>A popular feature to add to any authentication system is a method to request a forgotten password. Ben also provides <a href="http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic/">a guide</a> for how to handle this with authlogic. As with the sessions, password resets were a concise and contained component. It was easier to wipe out what was existing and start fresh with the aforementioned guide.</p>
<p>Authlogic follows Rails&#8217; convention over configuration idea and will pick up functionality based on the fields that exist in your model. Of course this means changes to the database and in Rails, that means a migration. Below is what mine looks like:
</pre>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> TransitionToAuthlogic <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    change_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:crypted_password</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>, <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">128</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&quot;</span>
    change_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:salt</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>, <span style="color:#ff3333; font-weight:bold;">:limit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">128</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&quot;</span>
&nbsp;
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:salt</span>, <span style="color:#ff3333; font-weight:bold;">:password_salt</span>
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:activation_code</span>, <span style="color:#ff3333; font-weight:bold;">:old_activation_code</span>
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:password_reset_code</span>, <span style="color:#ff3333; font-weight:bold;">:old_password_reset_code</span>
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:remember_token</span>, <span style="color:#ff3333; font-weight:bold;">:old_remember_token</span>
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:remember_token_expires_at</span>, <span style="color:#ff3333; font-weight:bold;">:old_remember_token_expires_at</span>
&nbsp;
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:login_count</span>, <span style="color:#ff3333; font-weight:bold;">:integer</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:failed_login_count</span>, <span style="color:#ff3333; font-weight:bold;">:integer</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:last_request_at</span>, <span style="color:#ff3333; font-weight:bold;">:datetime</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:current_login_at</span>, <span style="color:#ff3333; font-weight:bold;">:datetime</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:last_login_at</span>, <span style="color:#ff3333; font-weight:bold;">:datetime</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:current_login_ip</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:last_login_ip</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
&nbsp;
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:persistence_token</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:single_access_token</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:perishable_token</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
    add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:active</span>, <span style="color:#ff3333; font-weight:bold;">:boolean</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># set active users to active.</span>
    User.<span style="color:#9900CC;">update_all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;active = 1&quot;</span>, <span style="color:#996600;">&quot;state = 'active'&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    add_index <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:perishable_token</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:active</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:perishable_token</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:single_access_token</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:persistence_token</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:last_login_ip</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:current_login_ip</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:last_login_at</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:current_login_at</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:last_request_at</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:failed_login_count</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:login_count</span>
&nbsp;
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:password_salt</span>, <span style="color:#ff3333; font-weight:bold;">:salt</span>
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:old_activation_code</span>, <span style="color:#ff3333; font-weight:bold;">:activation_code</span>
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:old_password_reset_code</span>, <span style="color:#ff3333; font-weight:bold;">:password_reset_code</span>
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:old_remember_token</span>, <span style="color:#ff3333; font-weight:bold;">:remember_token</span>
    rename_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:old_remember_token_expires_at</span>, <span style="color:#ff3333; font-weight:bold;">:remember_token_expires_at</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Most important are the two change_column calls at the top. Authlogic needs 128 characters to store the crypted_password and salt fields whereas restful_authentication only provides 40. It doesn't really matter if they are still 128 characters when the migration is run down so I didn't bother changing them back. Besides, you'd never do that in production anyway.</p>
<p>As mentioned previously, there is an active column being added to the user model. Restful_authentication has an option to enable user states. This is done to allow tracking of activation. Authlogic doesn't come with anything such as this built in. It's simple enough to add and someone has written <a href="http://www.claytonlz.com/index.php/2009/07/authlogic-account-activation-tutorial/">a tutorial</a> already. The migration handles carrying the active status over from restful_authentication with a single update_all call.</p>
<p>Notice the rename_columns; the idea is that those columns are essentially flagged for later removal. This is a somewhat cautious approach. It would be possible to handle the removal of these columns in a later transaction.</p>
<p>The final piece to the puzzle is making your test suite happy again. If you are at all serious about testing, you will now have a number of failing tests after making such widespread changes to your application. In my application, much of the functional testing involved logging in user with a specific role. To do this in authlogic is simple.</p>
<p>First, add support for authlogic in tests by requiring the authlogic test module in test_helper.rb.</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;">'authlogic/test_case'</span></pre></div></div>

<p>Then, add the following to any functional or integration test you need to log in to test:</p>

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

<p>Finally, you can initiate the actual login by calling UserSession.create and passing in your user object like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">assert UserSession.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span>users<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:admin</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Notice we can even assert that the result of this login is true so we know it actually happens. The example happens to pass in a user from a fixture, but you could just as easily use a factory to generate your user object. Authlogic doesn't care where the user comes from.</p>
<p>Assuming all of your tests pass and you've done enough manual testing to be satisfied, you can go ahead and declare your switch from restful_authentication to authlogic complete. Changing over to a new authentication module can be a scary thing, especially when you are changing encryption methods for your passwords. It has the potential to lock out every single user in your system if you get something wrong. Remember, TATFT. A solid test suite and thorough manual testing will give you confidence when it comes to launching this into production with your application. If you do your due diligence, your transition will be smooth.</p>
<p>I created this post based on the work I did for <a href="http://decoymusic.com">DecoyMusic.com</a>. It is now happily chugging along on Authlogic. Now that the transition is done, it's time to start using <a href="http://www.binarylogic.com/2009/02/25/the-future-of-authlogic-add-ons/">authlogic's plugin architecture</a> to add <a href="http://github.com/binarylogic/authlogic_openid/tree/master">OpenID</a> and <a href="http://github.com/kalasjocke/authlogic_facebook_connect">Facebook Connect</a> support.</p>
]]></content:encoded>
			<wfw:commentRss>http://jrmehle.com/2009/09/02/switching-from-restful_authentication-to-authlogic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Be a Douche</title>
		<link>http://jrmehle.com/2009/06/02/dont-be-a-douche/</link>
		<comments>http://jrmehle.com/2009/06/02/dont-be-a-douche/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 16:58:29 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://jrmehle.com/?p=638</guid>
		<description><![CDATA[If you&#8217;ve got a great idea for the next big web framework, the first thing you should do is not write a douche-y blog post bashing other web frameworks. Here&#8217;s some ideas for things you could do instead:

You could make a website promoting the advantages of your framework over other existing frameworks.
You could improve the [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve got a great idea for the next big web framework, the first thing you should do is not write <a href="http://bit.ly/UNvU7">a douche-y blog post bashing other web frameworks</a>. Here&#8217;s some ideas for things you could do instead:</p>
<ul>
<li>You could make a website promoting the advantages of your framework over other existing frameworks.</li>
<li>You could improve the framework itself.</li>
<li>You could document the framework so that others can easily begin using it.</li>
</ul>
<p>The point is by attacking others, you don&#8217;t accomplish anything other than branding yourself as a grade A asshole. It&#8217;s fine if programming in C and doing all that memory allocation, dealing with pointers, and calling functions validates your skill as a programmer. I happen to like Ruby because I don&#8217;t have to do those things. What&#8217;s great is that we can co-exist. You can have your fast framework written in C and serve thousands of pages a second. I&#8217;ll stick to my &#8217;slow&#8217; framework written in Ruby. Chances are the people visiting the sites will never know the difference anyway.</p>
<p>Drop the ego. Realize that there can be more than one approach to solving a problem and yours isn&#8217;t always going to be the best one.</p>
]]></content:encoded>
			<wfw:commentRss>http://jrmehle.com/2009/06/02/dont-be-a-douche/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Setting Email and Name with ActionMailer</title>
		<link>http://jrmehle.com/2009/05/09/setting-email-and-name-with-actionmailer/</link>
		<comments>http://jrmehle.com/2009/05/09/setting-email-and-name-with-actionmailer/#comments</comments>
		<pubDate>Sat, 09 May 2009 14:38:33 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://jrmehle.com/?p=625</guid>
		<description><![CDATA[It seems that the Rails core team is so busy building awesomeness into Rails that they don&#8217;t have much time to improve the documentation. This is why docrails was started. The new Rails Guides are immensely helpful, and quite the improvement over the old wiki. However, you&#8217;ll still find a few surprises if you dig [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that the Rails core team is so busy building awesomeness into Rails that they don&#8217;t have much time to improve the documentation. This is why <a href="http://wiki.github.com/lifo/docrails">docrails</a> was started. The new <a href="http://guides.rubyonrails.org">Rails Guides</a> are immensely helpful, and quite the improvement over the old wiki. However, you&#8217;ll still find a few surprises if you dig around in other people&#8217;s code like this handy feature I&#8217;ve found in ActionMailer.</p>
<p>ActionMailer let&#8217;s you specify the from address on emails sent out, but did you know you could also set a nice name in addition to that? You might have a basic mailer method like this.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> my_email<span style="color:#006600; font-weight:bold;">&#40;</span>to_email, to_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    from <span style="color:#996600;">'myname@example.com'</span>
    recipients to_email
    subject <span style="color:#996600;">&quot;Hello There #{to_name}&quot;</span>
    body<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:to_email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> to_email, <span style="color:#ff3333; font-weight:bold;">:to_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> to_name<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We pass in an email address to send to and the person&#8217;s name. The from name is simply hardcoded as a string. If that email showed up in your inbox, it would probably say it&#8217;s from <strong>myname</strong>. Depending on the email client, it may just show the address it came from. That&#8217;s great, but it doesn&#8217;t look very nice.</p>
<p>Enter the undocumented syntax I&#8217;ve stumbled on.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> my_email<span style="color:#006600; font-weight:bold;">&#40;</span>to_email, to_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    from <span style="color:#996600;">'My Pretty Name &lt;myname @example.com&gt;'</span>
    recipients to_email
    subject <span style="color:#996600;">&quot;Hello There #{to_name}&quot;</span>
    body<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:to_email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> to_email, <span style="color:#ff3333; font-weight:bold;">:to_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> to_name<span style="color:#006600; font-weight:bold;">&#41;</span>
  end<span style="color:#006600; font-weight:bold;">&lt;/</span>myname<span style="color:#006600; font-weight:bold;">&gt;</span></pre></div></div>

<p>All you need to do is add the name you want to appear in the from field before your email address, then add a space, and finally wrap the email with angle brackets. That&#8217;s it! Now when your email arrives in someone&#8217;s inbox, it will show that it&#8217;s from <strong>My Pretty Name</strong>.</p>
<p>The syntax leaves a bit to be desired in my opinion. It doesn&#8217;t feel very Rails-y or even like the rest of ActionMailer. When specifying the recipients in ActionMailer for example, you can pass in either a single email address as a string or an array of string email addresses. The syntax should be similar with the from name and address. Here&#8217;s my proposed syntax.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> my_email<span style="color:#006600; font-weight:bold;">&#40;</span>to_email, to_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    from <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'My Pretty Name'</span>, <span style="color:#996600;">'myname@example.com'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    recipients to_email
    subject <span style="color:#996600;">&quot;Hello There #{to_name}&quot;</span>
    body<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:to_email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> to_email, <span style="color:#ff3333; font-weight:bold;">:to_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> to_name<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Seems like a logical addition, eh? Pass in the from name as the first item in an array and the email as the second. Using a hash instead would also be acceptable. Maybe I&#8217;ll dig into Rails and submit my first ever patch.</p>
]]></content:encoded>
			<wfw:commentRss>http://jrmehle.com/2009/05/09/setting-email-and-name-with-actionmailer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
