<?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>New Auburn Personal Computer Services LLC</title>
	<atom:link href="http://www.napcsweb.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.napcsweb.com/blog</link>
	<description>professional web development and consulting</description>
	<lastBuildDate>Fri, 27 Aug 2010 18:11:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Accessible Rails Applications &#8211; Let Cucumber help you test for Accessibility</title>
		<link>http://www.napcsweb.com/blog/2010/08/27/let_cucumber_help/</link>
		<comments>http://www.napcsweb.com/blog/2010/08/27/let_cucumber_help/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 18:07:03 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=207</guid>
		<description><![CDATA[Developers working with Rails are constantly looking for ways to use Cucumber to test their JavaScript. One thing I&#8217;ve come to love about Cucumber&#8217;s default setup is that it does not work with JavaScript at all, which actually helps me ensure that my applications work for uses without JavaScript enabled. if I write a Cucumber [...]]]></description>
			<content:encoded><![CDATA[<p>Developers working with Rails are constantly looking for ways to use Cucumber to test their JavaScript. One thing I&#8217;ve come to love about Cucumber&#8217;s default setup is that it does <em>not work with JavaScript</em> at all, which actually helps me ensure that my applications work for uses without JavaScript enabled.</p>
<p>if I write a Cucumber story that tests to see if a delete works, and I&#8217;ve used the default &#8220;link_to&#8221; method for deletes made popular by the Rails scaffolding, my cucumber feature will fail. I&#8217;ll be shown the Show page instead of the &#8220;successfully deleted&#8221; message.</p>
<p>Keep that in mind as you work through your applications &#8211; See what stories break when you don&#8217;t have JavaScript enabled and reconsider your implementations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2010/08/27/let_cucumber_help/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reordering Records with acts_as_list and Metaprogramming</title>
		<link>http://www.napcsweb.com/blog/2010/05/28/reordering-records-with-acts_as_list-and-metaprogramming/</link>
		<comments>http://www.napcsweb.com/blog/2010/05/28/reordering-records-with-acts_as_list-and-metaprogramming/#comments</comments>
		<pubDate>Fri, 28 May 2010 15:36:15 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Metaprogramming]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=196</guid>
		<description><![CDATA[Using acts_as_list, you can reorder items by adding a &#8220;position&#8221; column to your database and then easily sort records. class Project &#60; ActiveRecord::Base acts_as_list end &#160; class Task &#60; ActiveRecord::Base acts_as_list end The acts_as_list plugin also provides you methods to manipulate the position. @project.move_to_top @project.move_to_bottom @project.move_higher @project.move_lower In a current project, I have multiple items [...]]]></description>
			<content:encoded><![CDATA[<p>Using<a class="popup" href="http://github.com/rails/acts_as_list"> acts_as_list</a>, you can reorder items by adding a &#8220;position&#8221; column to your database and then easily sort records.</p>

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

<p>The acts_as_list plugin also provides you methods to manipulate the position.</p>
<ul>
<li>@project.move_to_top</li>
<li>@project.move_to_bottom</li>
<li>@project.move_higher</li>
<li>@project.move_lower</li>
</ul>
<p>In a current project, I have multiple items that need reordering, which means I&#8217;d be duplicating a lot of controller actions and other code across my controllers and views. I&#8217;ll show you how I used metaprogramming to significantly reduce that duplication.</p>
<h3>The Route To Reordering</h3>
<p>First, we need some routes. Add this to your <code>routes.rb</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>projects tasks<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>controller<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>move_higher move_lower move_to_top move_to_bottom<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>action<span style="color:#006600; font-weight:bold;">|</span>
      instance_eval <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOF
        map.<span style="color:#008000; font-style:italic;">#{action}_#{controller.singularize} &quot;#{controller}/:id/#{action}&quot;, {:controller =&gt; &quot;#{controller}&quot;, :action =&gt; &quot;#{action}&quot;}</span>
      EOF
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We use an array of controllers, then we have another array of the four actions, and we just make the named routes. This generates</p>
<pre>
    move_higher_project        /projects/:id/move_higher                            {:action=>"move_higher", :controller=>"projects"}
      move_lower_project       /projects/:id/move_lower                             {:action=>"move_lower", :controller=>"projects"}
     move_to_top_project       /projects/:id/move_to_top                            {:action=>"move_to_top", :controller=>"projects"}
  move_to_bottom_project       /projects/:id/move_to_bottom                         {:action=>"move_to_bottom", :controller=>"projects"}

        move_higher_task       /tasks/:id/move_higher                            {:action=>"move_higher", :controller=>"tasks"}
         move_lower_task       /tasks/:id/move_lower                             {:action=>"move_lower", :controller=>"tasks"}
        move_to_top_task       /tasks/:id/move_to_top                            {:action=>"move_to_top", :controller=>"tasks"}
     move_to_bottom_task       /tasks/:id/move_to_bottom                         {:action=>"move_to_bottom", :controller=>"tasks"}
</pre>
<p>Metaprogramming really cuts down on work when you use it correctly. This turns out to be a pretty nice way of generating route patterns that share common attributes.</p>
<h3>Keep The Control Centralized</h3>
<p>Now let&#8217;s step it up a notch. Each controller is going to need the four reordering methods. The logic will be identical except for the object they work on and the URL they redirect to when they&#8217;re finished.</p>
<p>Let&#8217;s create a module that adds controller methods to match these routes. We&#8217;ll add it into our controllers where we need it.</p>
<p>Create the file <code>lib/reorder_controller_actions.rb</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">module</span> ReorderControllerMethods
&nbsp;
      <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>move_higher move_lower move_to_top move_to_bottom<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>action<span style="color:#006600; font-weight:bold;">|</span>
        define_method action <span style="color:#9966CC; font-weight:bold;">do</span>
          klass = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">name</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;::&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">last</span>.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Controller&quot;</span>, <span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">downcase</span>.<span style="color:#9900CC;">singularize</span>.<span style="color:#9900CC;">camelize</span>.<span style="color:#9900CC;">constantize</span>
          item = klass.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          item.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>action<span style="color:#006600; font-weight:bold;">&#41;</span>
          flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;The #{klass.to_s} was reordered.&quot;</span>
          redirect_to <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;index&quot;</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>That module defines four controller actions and calls the corresponding method on the model, then does a redirect back to the index action. It&#8217;ll be the same everywhere I use it, so I also gain consistency with metaprogramming.</p>
<p>We need to add this to the bottom of <code>config/environment.rb</code> so that our controllers can make use of it.</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;">'reorder_controller_methods'</span></pre></div></div>

<p>Then, in each controller where we need this module, we mix it in with <code>include</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ProjectsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">include</span> ReorderControllerMethods
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> TasksController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">include</span> ReorderControllerMethods
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Stop and REST for a second</h3>
<p>Is it appropriate for me to do it this way? I&#8217;m going to argue that while technically the position of something could be sent to the update action, I want specific redirection and notification to occur when I change the order of elements. The logic was getting too nasty in the update action, so I split each thing apart, so I went with this approach instead.</p>
<h3>An Improved View</h3>
<p>Next, we need some buttons for our index pages with arrows on them so the user can trigger the ordering. How about a simple helper that can generate all four buttons for us?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> reorder_buttons<span style="color:#006600; font-weight:bold;">&#40;</span>object<span style="color:#006600; font-weight:bold;">&#41;</span>
    thing = object.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">name</span>.<span style="color:#9900CC;">camelize</span>.<span style="color:#9900CC;">downcase</span>
    result = <span style="color:#996600;">&quot;&quot;</span>
    <span style="color:#006600; font-weight:bold;">&#91;</span>
      <span style="color:#006600; font-weight:bold;">&#123;</span>:action <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;move_higher&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:label</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&amp;uarr;&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
      <span style="color:#006600; font-weight:bold;">&#123;</span>:action <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;move_lower&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:label</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&amp;darr;&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
      <span style="color:#006600; font-weight:bold;">&#123;</span>:action <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;move_to_top&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:label</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&amp;uarr;&amp;uarr;&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>,
      <span style="color:#006600; font-weight:bold;">&#123;</span>:action <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;move_to_bottom&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:label</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;&amp;darr;&amp;darr;&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>item<span style="color:#006600; font-weight:bold;">|</span>    
      result <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> button_to<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{item[:label]}&quot;</span>, send<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{item[:action]}_#{thing}_path&quot;</span>, object<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>
    result
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We use an array hold the buttons. We use hashes within the array to map the action to the label of the button. We then iterate and generate buttons for each one, concatenating to a string that we then return. </p>
<p>Then in our index views, we just need to call this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#006600; font-weight:bold;">&lt;%</span>=reorder_buttons <span style="color:#0066ff; font-weight:bold;">@project</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>That generates exactly what I need &#8211; four buttons, one for each reordering action.</p>
<h3>Wrapping Up</h3>
<p>This solution easily allowed me to add reordering to a lot of controllers in only a few minutes. Hopefully it will help you do the same. You could improve this by storing the methods in a constant so that you didn&#8217;t have to duplicate the array all the time, and I&#8217;m sure there are other improvements that I could make as well. I&#8217;d love to hear from you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2010/05/28/reordering-records-with-acts_as_list-and-metaprogramming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making &#8220;as_string&#8221; Attribute Readers for ActiveRecord</title>
		<link>http://www.napcsweb.com/blog/2010/03/15/making-as-strin-alternate-attribute-readers/</link>
		<comments>http://www.napcsweb.com/blog/2010/03/15/making-as-strin-alternate-attribute-readers/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 02:58:47 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Metaprogramming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=152</guid>
		<description><![CDATA[Occasionally, I need to transform boolean model attributes like &#8220;active&#8221; to display &#8220;active&#8221; or &#8220;inactive&#8221; instead of &#8220;true&#8221; or &#8220;false&#8221; when making reports or views. A lot of times this means writing some kind of helper method like this: def active_or_inactive&#40;object, true_message, false_message&#41; object.active ? true_message : false_message end and calling it like this: &#60;%= [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally, I need to transform boolean model attributes like &#8220;active&#8221; to display &#8220;active&#8221; or &#8220;inactive&#8221; instead of &#8220;true&#8221; or &#8220;false&#8221; when making reports or views. A lot of times this means writing some kind of helper 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> active_or_inactive<span style="color:#006600; font-weight:bold;">&#40;</span>object, true_message, false_message<span style="color:#006600; font-weight:bold;">&#41;</span>
  object.<span style="color:#9900CC;">active</span> ? true_message : false_message
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>and calling it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#006600; font-weight:bold;">&lt;%</span>= active_or_inactive<span style="color:#006600; font-weight:bold;">&#40;</span>@project, <span style="color:#996600;">&quot;Active&quot;</span>, <span style="color:#996600;">&quot;Inactive&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>That&#8217;s not a bad approach, and it helps keep the views slightly cleaner by keeping the logic out, but it ends up being more characters than simply using a ternary operator in the view. I&#8217;ve used a slightly different approach in some of my more recent projects and I thought I should share it with you.</p>
<h2>Move It To The Model</h2>
<p>That&#8217;s right, I&#8217;m advocating pushing that helper into the model itself. I can hear you now, yelling something about &#8220;this guy doesn&#8217;t know what he&#8217;s talking about! How dare he put display logic in his models!&#8221; But before you close your browser, allow me to explain.</p>
<p>It just so happens that I need this logic not only in my views, but in my text-based reports that I run outside of the web server. I could mix the module with the helpers in when I needed it, but there&#8217;s also something un-object-oriented that bugs me about helpers. They remind me of PHP a bit. I feel like I should be calling <code>object.active_as_string("Active", "Inactive")</code> instead. So that&#8217;s what I&#8217;m going to do.</p>
<p>First, a unit test, because we&#8217;re all good professionals that write tests first. I want to call a method called <code>active_as_string</code> which takes two parameters &#8211; the string to print when it&#8217;s true and the string to print when it&#8217;s<br />
false. Here are my tests:</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;">'test_helper'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ProjectTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveSupport::TestCase</span>
&nbsp;
  test <span style="color:#996600;">&quot;should display 'Active' if active&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> = Project.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_equal <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">active_as_string</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Active&quot;</span>, <span style="color:#996600;">&quot;Inactive&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;Active&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  test <span style="color:#996600;">&quot;should display 'Inactive' if not active&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> = Project.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_equal <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">active_as_string</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Active&quot;</span>, <span style="color:#996600;">&quot;Inactive&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;Inactive&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Tests help me design the method&#8217;s use up front. With two failing tests as my guide, I can now take my first stab at making the method work:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Project <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
   <span style="color:#9966CC; font-weight:bold;">def</span> active_as_string<span style="color:#006600; font-weight:bold;">&#40;</span>true_message, false_message<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">active</span> ? true_message : false_message
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>With that implemented, my tests pass. However, I also have a &#8220;closed&#8221; boolean I need to handle, and it would also be nice if I could display &#8220;No description&#8221; if a project&#8217;s description was blank. I could write my own <code>_as_string</code> methods like I&#8217;ve done already, but instead, I&#8217;ll do a little metaprogramming to generate what I need.</p>
<p>Let&#8217;s add four more test cases &#8211; to test the &#8220;closed&#8221; and the &#8220;description&#8221; fields.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  test <span style="color:#996600;">&quot;should display 'Closed' if closed&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> = Project.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:closed</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_equal <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">closed_as_string</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Closed&quot;</span>, <span style="color:#996600;">&quot;Open&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;Closed&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  test <span style="color:#996600;">&quot;should display 'Open ' if not closed&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> = Project.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:active</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_equal <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">closed_as_string</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Closed&quot;</span>, <span style="color:#996600;">&quot;Open&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;Open&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  test <span style="color:#996600;">&quot;should display 'No Description' if description is nil&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> = Project.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_equal <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">description_as_string</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;No Description&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;No Description&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  test <span style="color:#996600;">&quot;should display the description if it exists&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#CC0066; font-weight:bold;">p</span> = Project.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:description</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Hi there!&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert_equal <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">description_as_string</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;No Description&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">&quot;Hi there!&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, let&#8217;s build some methods!</p>
<h3>ActiveRecord::Base.columns</h3>
<p>Every ActiveRecord class has a class method called <code>columns</code> that returns a collection of column objects. The Column object describes each database column and lets you determine its type and its name. We can use that and <code>class_eval</code> to generate a whole bunch of methods at runtime.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Project <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">columns</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>column<span style="color:#006600; font-weight:bold;">|</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> column.<span style="color:#9900CC;">type</span> == <span style="color:#ff3333; font-weight:bold;">:boolean</span>
&nbsp;
      class_eval <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOF
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{column.name}_as_string(t,f)</span>
          value = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#008000; font-style:italic;">#{column.name}</span>
          value ? t : f
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      EOF
&nbsp;
    <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>In this example, we&#8217;re creating the <code>_as_string</code> method for each boolean column. It takes two parameters and is basically the same code we already used in our original method earlier. Notice how <code>class_eval</code> can do string interpolation using Ruby&#8217;s <code>#{}</code> syntax. That makes it easy to build up the method names.</p>
<p>We can use that same concept to do the same for any other methods &#8211; we&#8217;ll just cast them to strings and check to see if they are blank.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  class_eval <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOF
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{column.name}_as_string(default_value)</span>
     value = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#008000; font-style:italic;">#{column.name}.to_s</span>
     value.<span style="color:#9900CC;">blank</span>? ? default_value : value
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  EOF</pre></div></div>

<p>We throw that into the <code>else</code> block and our whole example looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">class</span> Project <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">columns</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>column<span style="color:#006600; font-weight:bold;">|</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> column.<span style="color:#9900CC;">type</span> == <span style="color:#ff3333; font-weight:bold;">:boolean</span>
&nbsp;
        class_eval <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOF
&nbsp;
          <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{column.name}_as_string(t,f)</span>
            value = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#008000; font-style:italic;">#{column.name}</span>
            value ? t : f
          <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        EOF
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">else</span>
&nbsp;
      class_eval <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOF
&nbsp;
          <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{column.name}_as_string(default_value)</span>
           value = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#008000; font-style:italic;">#{column.name}.to_s</span>
           value.<span style="color:#9900CC;">blank</span>? ? default_value : value
          <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        EOF
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>If you run your tests now, they all pass. But our work isn&#8217;t done &#8211; this isn&#8217;t very DRY. We may want to use this in another class too.</p>
<h3>Modules!</h3>
<p>Create a new module and mix the behavior into your models. Create the file <code>lib/active_record/as_string_reader_methods.rb</code> (create the <code>active_record</code>folder if it doesn&#8217;t exist already) and put this code in the file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecord
    <span style="color:#9966CC; font-weight:bold;">module</span> AsStringReaderMethods
     <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>base<span style="color:#006600; font-weight:bold;">&#41;</span>
       create_string_readers<span style="color:#006600; font-weight:bold;">&#40;</span>base<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> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">create_string_readers</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
      base.<span style="color:#9900CC;">columns</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>column<span style="color:#006600; font-weight:bold;">|</span>
&nbsp;
         <span style="color:#9966CC; font-weight:bold;">if</span> column.<span style="color:#9900CC;">type</span> == <span style="color:#ff3333; font-weight:bold;">:boolean</span>
&nbsp;
           class_eval <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOF
&nbsp;
             <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{column.name}_as_string(t,f)</span>
               value = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#008000; font-style:italic;">#{column.name}</span>
               value ? t : f
             <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
           EOF
         <span style="color:#9966CC; font-weight:bold;">else</span>
&nbsp;
           class_eval <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOF
&nbsp;
             <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#008000; font-style:italic;">#{column.name}_as_string(default_value)</span>
               value = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#008000; font-style:italic;">#{column.name}.to_s</span>
               value.<span style="color:#9900CC;">blank</span>? ? default_value : value
             <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
           EOF
         <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:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It&#8217;s mostly the same code we had before, but in this case we&#8217;re using the <code>self.included</code> method to trigger the method creation on the model that includes the module. </p>
<p>Now, remove the code from your Project mode and replace it with</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">include</span> AsStringReaderMethods</pre></div></div>

<p>Run your tests, and everything should pass.  You now have a module you can drop into your projects and you&#8217;ll have this functionality yourself.  Now it&#8217;s up to you to expand upon this, and use this pattern in your own work if you find it useful.</p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2010/03/15/making-as-strin-alternate-attribute-readers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Basic Authentication in Ruby on Rails &#8211; In Case You Forgot</title>
		<link>http://www.napcsweb.com/blog/2010/03/11/basic-authentication-in-ruby-on-rails-in-case-you-forgot/</link>
		<comments>http://www.napcsweb.com/blog/2010/03/11/basic-authentication-in-ruby-on-rails-in-case-you-forgot/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 05:58:25 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=140</guid>
		<description><![CDATA[There are so many authentication choices in Rails these days, but sometimes the simplest approach is the best approach. I&#8217;m working with a client right now building an application that has a mostly-public interface. Only a handful of people need to log in to the site, and they only need to modify content occasionally. It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>There are so many authentication choices in Rails these days, but sometimes the simplest approach is the best approach. I&#8217;m working with a client right now building an application that has a mostly-public interface. Only a handful of people need to log in to the site, and they only need to modify content occasionally. It&#8217;s not a complicated project at all, and while my first instinct was to reach for my <a href="http://www.github.com/napcs/authlogic_base" class="popup">starter project</a> that I usually use for these kinds of things, I thought again and realized the following:</p>
<ol>
<li>This project doesn&#8217;t need to let people sign up.</li>
<li>There&#8217;s no need to send password recovery instructions</li>
<li>Much of the data entry will be done with a mobile device connecting to the app&#8217;s REST-style XML API.</li>
</ol>
<p>Something like Authlogic, or even Restful Authentication seems like overkill for something this simple.  Many Rails developers are probably used to their solutions because many Rails projects are more complex than this. In the spirit of keeping things simple, I&#8217;m going to show you how to do authentication of users without any plugins, the way we used to do it in 2005. (For those of you that have been working with Rails as long as me, this will be a good refresher for you. When was the last time <b>you</b> hand-rolled your authentication solution?)</p>
<h2>Back To Basic (Authentication, that is)</h2>
<p>Since Rails 2.0, Basic Authentication has been an option, as Ryan Bates explains in <a class="popup" href="http://railscasts.com/episodes/82-http-basic-authentication">Railscast #82</a>. We&#8217;ll use that and a basic user model to authenticate our users.</p>
<p>First, generate a user mode with login and a hashed password fields. I&#8217;m not going to do any salting here, as it&#8217;s not necessary. If you want it, you should be able to add it easily.</p>
<pre>ruby script/generate model User login:string hashed_password:string</pre>
<p>Now we need to modify the User model to do the password encryption.</p>
<p>First, set up the validations and the attribute accessors for the password and password_confirmation fields.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:login</span>
  validates_confirmation_of <span style="color:#ff3333; font-weight:bold;">:password</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:password</span></pre></div></div>

<p>Next, be a good developer and write a unit test for hashing the password.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  test <span style="color:#996600;">&quot;should create a user with a hashed password&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    u = User.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:login</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;homer&quot;</span>,
                 <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;homer&quot;</span>,
                 <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;1234&quot;</span>,
                 <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;1234&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    u.<span style="color:#9900CC;">reload</span> <span style="color:#008000; font-style:italic;"># (make sure it saved!)            </span>
    assert_not_nil u.<span style="color:#9900CC;">hashed_password</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Prepare your test database</p>
<pre>rake db:test:clone</pre>
<p>and run your test</p>
<pre>rake test:units</pre>
<p>With the test in place, write the code to encrypt the password on save. Add the SHA1 digest library at the top of your class:</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;">'digest/sha1'</span></pre></div></div>

<p>Then add the before filter and an encryption method:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  before_save <span style="color:#ff3333; font-weight:bold;">:encrypt_password</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> encrypt_password
    <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password</span>.<span style="color:#9900CC;">blank</span>?
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">hashed_password</span> = <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><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span> 
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Run your test again and everything should pass.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rake test:units</pre></div></div>

<h3>Authenticating Users</h3>
<p>Now we need to write a class method that we can use to grab a user from the database by looking up their username and hashed password. We&#8217;ll use a simple pattern for this. First, let&#8217;s write a quick test:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  test <span style="color:#996600;">&quot;given a user named homer with a password of 1234, he should be authenticated with 'homer' and '1234' &quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    User.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:login</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;homer&quot;</span>,
                 <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;homer&quot;</span>,
                 <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;1234&quot;</span>,
                 <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;1234&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    assert User.<span style="color:#9900CC;">authenticated</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;homer&quot;</span>, <span style="color:#996600;">&quot;1234&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We create a user and then call User.authenticated?. If its return value evaluates to True, we&#8217;ve got a good set of credentials.  Add this class method to your User model to make your test pass:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" 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;">authenticated</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>login, password<span style="color:#006600; font-weight:bold;">&#41;</span>
    pwd = <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>password.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    User.<span style="color:#9900CC;">find_by_login_and_hashed_password</span><span style="color:#006600; font-weight:bold;">&#40;</span>login, pwd<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Notice that here, I&#8217;m actually returning the user object, rather than a boolean. If no user is found, <b>nil</b> is returned which evaluates to false. Remember, in Ruby, everything except nil and false evaluates to True.</p>
<p>Our entire user model looks like this:</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;">'digest/sha1'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:login</span>
  validates_confirmation_of <span style="color:#ff3333; font-weight:bold;">:password</span>
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:password</span>
&nbsp;
  before_save <span style="color:#ff3333; font-weight:bold;">:encrypt_password</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">authenticated</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>login, password<span style="color:#006600; font-weight:bold;">&#41;</span>
    pwd = <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>password.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    User.<span style="color:#9900CC;">find_by_login_and_hashed_password</span><span style="color:#006600; font-weight:bold;">&#40;</span>login, pwd<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> encrypt_password
    <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password</span>.<span style="color:#9900CC;">blank</span>?
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">hashed_password</span> = <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><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">password</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span> 
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h3>Creating the Filter</h3>
<p>Let&#8217;s create a simple Projects scaffold. We&#8217;ll use our authentication to protect this scaffolded interface.</p>
<pre>ruby script/generate scaffold Project name:string description:text completed:boolean</pre>
<p>Now, open <code>app/controllers/application_controller.rb</code> and this code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">def</span> authenticate_with_basic_auth
    authenticate_or_request_with_http_basic <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>username, password<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#0066ff; font-weight:bold;">@current_user</span> = User.<span style="color:#9900CC;">authenticated</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>username, password<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>This tiny bit of code will pop up a Basic Authentication credentials box and look the user up in our database using the supplied credentials. If we find our user, we put it in the @current_user instance variable. It&#8217;s common practice in Rails apps to have a current_user helper method, so we can add that to <code>application_controller.rb</code> as well.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  helper_method <span style="color:#ff3333; font-weight:bold;">:current_user</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> current_user
    <span style="color:#0066ff; font-weight:bold;">@current_user</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>With that, we simply need to invoke the filter.  Open your <code>projects_controller.rb</code> file and add this to the top:</p>

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

<p>And that&#8217;s it! You&#8217;ve protected the application. Create a user via the Rails runner, fire up <code>script/server</code> and test it out!</p>
<pre>
ruby script/runner 'User.create(:login => "homer", :password => "1234", :password_confirmation => "1234")
ruby script/server
</pre>
<p>You should also be able to use cURL to play with the XML REST-style API provided by the scaffold generator.</p>
<p>Get projects:</p>
<pre>curl http://localhost:3000/projects.xml -u homer:1234</pre>
<p>Create project via XML</p>
<pre>
curl http://localhost:3000/projects.xml \
-u homer:1234 \
-X POST \
-d "
<project><name>Test</name></project>" \
-H "Content-Type: text/xml"
</pre>
<h2>Simple is Good</h2>
<p>This simple solution is easy to write, easy to maintain, and easy to extend. It&#8217;s also something that anyone with any practical experience with Rails should be able to write in as much time as it would take to configure Authlogic. </p>
<p>So what&#8217;s left to do with this? First, SHA1 isn&#8217;t great encryption &#8211; it&#8217;s just hashing. BCrypt might be better, Adding a salt to this hash might be another good idea too. Some more tests would be great. So, go write them, make this fit your security needs, and have fun! As always, I&#8217;d love to hear your comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2010/03/11/basic-authentication-in-ruby-on-rails-in-case-you-forgot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails and SQL Server &#8211; &#8220;There is no text for object&#8221;</title>
		<link>http://www.napcsweb.com/blog/2010/02/23/rails-and-sql-server-no-text-for-object/</link>
		<comments>http://www.napcsweb.com/blog/2010/02/23/rails-and-sql-server-no-text-for-object/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 18:36:57 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=135</guid>
		<description><![CDATA[I recently moved a Rails application to a new SQL Server 2005 server on a recent project and everything seemed to go smoothly, but when I tried to fire up a connection to the database from my Rails application, I was greeted with ActiveRecord::StatementInvalid: DBI::DatabaseError: 42000 (15197) [FreeTDS][SQL Server]There is no text for object 'people'.: [...]]]></description>
			<content:encoded><![CDATA[<p>I recently moved a Rails application to a new SQL Server 2005 server on a recent project and everything seemed to go smoothly, but when I tried to fire up a connection to the database from my Rails application, I was greeted with</p>
<pre>
ActiveRecord::StatementInvalid: DBI::DatabaseError: 42000 (15197) [FreeTDS][SQL Server]There is no text for object 'people'.: EXEC sp_helptext people
</pre>
<p>The &#8220;people&#8221; table here is actually a view that gets used all over the place in multiple applications. The DBA had moved the databases from an older SQL Server 2000 database previously.</p>
<p>The solution was to ensure that the application&#8217;s user account had the &#8220;view definition&#8221; permission on the view in question as well as the &#8220;select&#8221; permission.  On the view, in the SQL Server Management Studio, right click and choose &#8220;Properties&#8221;. Then choose Permissions select your user account, and then select the &#8220;View definition&#8221; permission. Checking the box under the &#8220;Grant&#8221; column was enough for me to make it work.</p>
<p> Interestingly enough, the production server (which was upgraded months ago from SQL Server 2000 to 2005), does not have the permission set, but still works fine.</p>
<p>Hopefully someone else finds this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2010/02/23/rails-and-sql-server-no-text-for-object/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why Rails?</title>
		<link>http://www.napcsweb.com/blog/2010/02/16/why-rail/</link>
		<comments>http://www.napcsweb.com/blog/2010/02/16/why-rail/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 23:13:59 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=132</guid>
		<description><![CDATA[NAPCS was a proud sponsor of the first Chippewa Valley Ruby Camp, a day-long Ruby training camp where 23 students learned how to build and deploy their first Rails application. I taught two of the three sessions and had a great time helping other developers get their hands on what I believe to be the [...]]]></description>
			<content:encoded><![CDATA[<p>NAPCS was a proud sponsor of the first <a href="http://www.ecruby.org/cvrc2010.html">Chippewa Valley Ruby Camp</a>, a day-long Ruby training camp where 23 students learned how to build and deploy their first Rails application. I taught two of the three sessions and had a great time helping other developers get their hands on what I believe to be <strong>the best way to develop scalable, maintainable, and stable web applications</strong> today. That&#8217;s a pretty bold statement, but I believe in it, and it&#8217;s why NAPCS uses Rails on all new client projects. (In fact, every project since 2006 has been a Rails project.)</p>
<h3>Rails projects are quick to launch</h3>
<p>With Rails, we can build and launch a prototype application in an extremely short time. On average, we can have something simple in front of the client in less than a couple of days, which is much faster than our previous projects where we used ASP or PHP.  And that project isn&#8217;t usually a throwaway project; we can tweak it and move forward, from prototype to production.</p>
<h3>Rails applications are easily testable</h3>
<p>Professionals write tests that prove the code works as it should, and since testing is built right in to the Rails framework. testing is an easy natural part of the process. Testing has always been possible regardless of the language used, but with Rails, it&#8217;s so easy to produce well-tested code that you&#8217;d be foolish not to test. For my customers, that means much better products, and less support calls.</p>
<h3>It&#8217;s a standard framework</h3>
<p>I occasionally pick up projects from other developers, and while I can&#8217;t always ensure that the quality of the code will be good, I at least already know my way around the project because, in a Rails application, conventions dictate where things go. This means the learning curve is lower when we transition an application, and the customer doesn&#8217;t get billed extra time for me to figure out what&#8217;s going on.</p>
<h3>The community is incredible</h3>
<p>We rely heavily on open-source projects to get stuff done, and Rails has an <a href="http://railsbridge.org/">amazing community</a> that is always pushing the limits of what Rails applications can do. There is a new solution to a new problem almost every day, and that keeps us all on our toes. Plus, we&#8217;re very proud to be sponsoring the<a href="http://www.railsmentors.org"> Rails Mentors</a> project, which helps other developers get better  at Rails development. We&#8217;re always <a href="http://www.github.com/napcs/">giving back to open source</a>, too. </p>
<h3>It gets out of the way.</h3>
<p>This is the most important point of all; Rails lets me deliver features. Instead of spending hours wiring up database tables to web pages, I can do that in five minutes and spend more time focusing on user experience and new features. And since it isn&#8217;t difficult to build things incrementally,  I don&#8217;t get boxed in. I can make changes without feeling that I&#8217;ll lose days of work. It allows me to respond flexibly to new feature requests.</p>
<p>Rails gives us a competitive advantage. We cannot always compete on price alone, but we can provide better-quality solutions than others because we embrace an open, agile framework that lets us deliver stable, scalable, well-tested, and maintainable web applications. </p>
<h3>Want to learn how you can take advantage of Ruby on Rails?</h3>
<p>Contact us for information on customized training and mentoring services. We offer affordable hourly rates for remote mentoring, as well as custom training classes upon request. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2010/02/16/why-rail/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Moving from Prototype to JQuery</title>
		<link>http://www.napcsweb.com/blog/2009/11/15/moving-from-prototype-to-jquery/</link>
		<comments>http://www.napcsweb.com/blog/2009/11/15/moving-from-prototype-to-jquery/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 06:19:35 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[tips]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=111</guid>
		<description><![CDATA[On a recent project, I converted a ton of Javascript from Prototype to jQuery in order to take advantage of many of the nice UI elements available. As I did the conversion, I took down some notes that I wanted to share with you about the differences between the two libraries. In some cases, the [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent project, I converted a ton of Javascript from Prototype to jQuery in order to take advantage of many of the nice UI elements available. As I did the conversion, I took down some notes that I wanted to share with you about the differences between the two libraries.  In some cases, the differences are insignificant, and in a couple of others, the differences merely come down to a difference of opinion among the developers and supporters of the libraries.</p>
<p>Here are the notes:</p>
<h2>Getting Ready</h2>
<p>Before you can work with elements on the page, those elements must be loaded.</p>
<p>Prototype uses</p>
<pre>
document.observe("dom:loaded", function() {
  // your functions
})
</pre>
<p>jQuery uses this:</p>
<pre>
$(document).ready(function(){
  // your functions
})
</pre>
<h2>Finding things</h2>
<p>In Prototype, you use <strong>$(</strong>) or <strong>$$()</strong> to locate elements. With <strong>$()</strong> you use the ID, whereas with <strong>$$()</strong> you use a CSS selector.</p>
<pre>
  var header = $("header");  // a single element,
<div id="header"
  var popups = $$("a.popup"); // all elements like <a class="popup">
</pre>
<p>With JQuery, you do almost all of your element location using <strong>$()</strong>, which works very much like <strong>$$()</strong> in Prototype.</p>
<pre>
  var header = $("#header");  // a single element,
<div id="header"
  var popups = $("a.popup"); // all elements like <a class="popup">
</pre>
<h2>Binding events</h2>
<p>Prototype&#8217;s <strong>Element</strong> class has an <strong>Observe</strong> method. It&#8217;s very easy to use and easy on the eyes.</p>
<pre>
  $("header").observe("click", function(event){
    alert("Hi there");
  });
</pre>
<p>In jQuery, it&#8217;s nearly identical, except that click is a method on the JQuery object.</p>
<pre>
  $("#header").click(function(event){
    alert("Hi there");
  });
</pre>
<p>At first, the differences look marginal, but let&#8217;s look at a more complicated example:</p>
<p>In Prototype, to find all links on the page with the class &#8220;Popup&#8221; and make them open in a new window, you have to do this:</p>
<pre>
function new_window_links(){
  links = $$("a.popup");
  links.each(function(link){
    link.observe("click", function(event){
      window.open(link.href);
      event.stop();
    });
  });
}
</pre>
<p>The Prototype version makes us find all of the elements, loop over them, and then apply the observer to each one.</p>
<p>jQuery can hide the iteration from you, which results in somewhat cleaner code.</p>
<pre>
function new_window_links(){
  links = $("a.popup").click(function(event){
      window.open($(this).attr('href'));
      event.preventDefault();
  });
}
</pre>
<h2>Adding Classes</h2>
<p>Prototype:</p>
<pre>
   $(".foo").addClassName("ninja");
</pre>
<pre>
  $(".foo").addClass("ninja");
</pre>
<h2>Traversing the DOM</h2>
<p>Prototype:</p>
<pre>
  parent_of_foo = $("foo").up();
</pre>
<p>jQuery</p>
<pre>
  parent_of_foo = $("#foo").parent();
</pre>
<h2>Working with HTML attributes</h2>
<p>This one was the most difficult to get used to. In Prototype, many of the HTML attributes are available as methods on the Element class.</p>
<pre>
  window.open(link.href);
</pre>
<p>In jQuery, you use the <b>attr</b> method to get and set the attributes.</p>
<pre>
  window.open(link.attr("http");
</pre>
<p>This illustrates only a few of the differences between the libraries, but as you can see, the differences don&#8217;t realy amount to anything substantial. Both of these libraries greatly simplify cross-browser JavaScript development, so no matter which you choose, you&#8217;ll be in good shape.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2009/11/15/moving-from-prototype-to-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Chippewa Valley Code Camp 2009</title>
		<link>http://www.napcsweb.com/blog/2009/11/15/chippewa-valley-code-camp/</link>
		<comments>http://www.napcsweb.com/blog/2009/11/15/chippewa-valley-code-camp/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 15:05:26 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=126</guid>
		<description><![CDATA[I had the honor of presenting two talks this year at the second annual Chippewa Valley Code Camp. Code camps are free events where programmers of any skill level come together for a day of sessions and talks that are centered around code. I&#8217;ve only recently become involved with these events and am extremely happy [...]]]></description>
			<content:encoded><![CDATA[<p>I had the honor of presenting two talks this year at the second annual Chippewa Valley Code Camp.  Code camps are free events where programmers of any skill level come together for a day of sessions and talks that are centered around code. I&#8217;ve only recently become involved with these events and am extremely happy I have. The opportunity to learn from so many experienced developers is incredible. </p>
<p>We held the event at UW-Stout this year, and had a great turnout. I gave two talks, the first on Ruby, and the second on Cucumber. I had wonderful audiences both times, and met some great people that I hope I get to work with in the future.  Here are the slides for my talks. </p>
<p>See the end of the slide decks for links to the demo source code.</p>
<div style="width:425px;text-align:left" id="__ss_2500173"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/napcs/intro-to-ruby-2500173" title="Intro to Ruby">Intro to Ruby</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=tccc7ruby-091114114030-phpapp01&#038;stripped_title=intro-to-ruby-2500173" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=tccc7ruby-091114114030-phpapp01&#038;stripped_title=intro-to-ruby-2500173" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/napcs">Brian Hogan</a>.</div>
</div>
<div style="width:425px;text-align:left" id="__ss_2503410"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/napcs/testing-your-sites-in-english-with-cucumber" title="Testing Your Sites In English with Cucumber">Testing Your Sites In English with Cucumber</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=cvcccucumber-091115011413-phpapp02&#038;stripped_title=testing-your-sites-in-english-with-cucumber" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=cvcccucumber-091115011413-phpapp02&#038;stripped_title=testing-your-sites-in-english-with-cucumber" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/napcs">Brian Hogan</a>.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2009/11/15/chippewa-valley-code-camp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8220;Introduction to Ruby&#8221; talk from Twin Cities Code Camp 7</title>
		<link>http://www.napcsweb.com/blog/2009/10/25/introduction-to-ruby-talk-from-twin-cities-code-camp-7/</link>
		<comments>http://www.napcsweb.com/blog/2009/10/25/introduction-to-ruby-talk-from-twin-cities-code-camp-7/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 03:32:42 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Projects]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=122</guid>
		<description><![CDATA[On Saturday, I had the honor of giving a talk on the Ruby programming language to an exceptional audience. I talked about the simple syntax and powerful libraries available, and then showed how we can use Ruby to maintain static web sites, build web applications, and test web sites. During the talk, I showed how [...]]]></description>
			<content:encoded><![CDATA[<p>On Saturday, I had the honor of giving a talk on the Ruby programming language to an exceptional audience. I talked about the simple syntax and powerful libraries available, and then showed how we can use Ruby to maintain static web sites, build web applications, and test web sites.</p>
<p>During the talk, I showed how to use <a href="http://www.sinatrarb.com">Sinatra</a> to create a very simple wiki. I wrote two versions. During the talk, I used SQLite3 and ActiveRecord, but I wrote a version that uses MongoDB. You can grab the <a href="http://www.github.com/napcs/sinatriki">source</a> for those at <a href="http://www.github.com/">Github</a>.</p>
<p>Here are the slides from my talk. You can view the notes too by viewing the slides on Slideshare.</p>
<div style="width:425px;text-align:left" id="__ss_2343598"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/napcs/intro-to-ruby-twin-cities-code-camp-7" title="Intro to Ruby - Twin Cities Code Camp 7">Intro to Ruby &#8211; Twin Cities Code Camp 7</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=tccc7ruby-091025153443-phpapp01&#038;stripped_title=intro-to-ruby-twin-cities-code-camp-7" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=tccc7ruby-091025153443-phpapp01&#038;stripped_title=intro-to-ruby-twin-cities-code-camp-7" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/napcs">Brian Hogan</a>.</div>
</div>
<p>Participating at code camps is something I really enjoy. It gives me a chance to talk about what I love, but it also gives me a chance to learn from other speakers. These events are usually free, and an awful lot of fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2009/10/25/introduction-to-ruby-talk-from-twin-cities-code-camp-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mongodb and Macports</title>
		<link>http://www.napcsweb.com/blog/2009/10/20/mongodb-and-macports/</link>
		<comments>http://www.napcsweb.com/blog/2009/10/20/mongodb-and-macports/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 21:21:39 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[tips]]></category>
		<category><![CDATA[mongodb ruby mac]]></category>

		<guid isPermaLink="false">http://www.napcsweb.com/blog/?p=119</guid>
		<description><![CDATA[I just hit a bump in the road with installing MongoDB via Macports 1.8.1. sudo port install boost pcre++ spidermonkey sudo port install mongodb When I ran mongodb I was greeted with dyld: Library not loaded: /opt/local/lib/libnspr4.dylib Referenced from: /opt/local/lib/nspr/libplds4.dylib Reason: image not found Trace/BPT trap To fix, I simply copied the missing file from [...]]]></description>
			<content:encoded><![CDATA[<p>I just hit a bump in the road with installing MongoDB via Macports 1.8.1. </p>
<pre>
sudo port install boost pcre++ spidermonkey
sudo port install mongodb
</pre>
<p>When I ran
<pre>mongodb</pre>
<p> I was greeted with</p>
<pre class="output">
dyld: Library not loaded: /opt/local/lib/libnspr4.dylib
  Referenced from: /opt/local/lib/nspr/libplds4.dylib
  Reason: image not found
Trace/BPT trap
</pre>
<p>To fix, I simply copied the missing file from its origin to where Mongodb was looking.</p>
<pre>
 sudo  cp /opt/local/lib/nspr/libnspr4.dylib /opt/local/lib
</pre>
<p>And all was right.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.napcsweb.com/blog/2009/10/20/mongodb-and-macports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
