<?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>Erik A. Hanson &#187; Ruby on Rails</title>
	<atom:link href="http://www.eahanson.com/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eahanson.com</link>
	<description>My weblog</description>
	<lastBuildDate>Mon, 24 May 2010 04:49:46 +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>A Rake Task to Concatenate and Compress Javascript Files</title>
		<link>http://www.eahanson.com/2008/03/02/a-rake-task-to-concatenate-and-compress-javascript-files/</link>
		<comments>http://www.eahanson.com/2008/03/02/a-rake-task-to-concatenate-and-compress-javascript-files/#comments</comments>
		<pubDate>Mon, 03 Mar 2008 06:15:52 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[wshlst.com]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2008/03/02/a-rake-task-to-concatenate-and-compress-javascript-files/</guid>
		<description><![CDATA[A while ago, I wrote about a Ruby script to concatenate and compress Javascript files for my wshlst application. I&#8217;ve changed things around a bit since that article, so I thought it was time for an update. Background wshlst uses a lot of Javascript. When developing, I want to have all of my JS files [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I wrote about <a href="http://www.eahanson.com/2007/10/16/a-ruby-script-to-concatenate-and-compress-javascript-files/">a Ruby script to concatenate and compress Javascript files</a> for my <a href="http://www.eahanson.com/category/wshlstcom/">wshlst</a> application. I&#8217;ve changed things around a bit since that article, so I thought it was time for an update.</p>
<h3>Background</h3>
<p><a href="http://www.eahanson.com/category/wshlstcom/">wshlst</a> uses <a href="http://www.eahanson.com/2007/10/25/wshlstcom-a-webapp-with-a-pure-javascript-ui/">a lot of Javascript</a>. When developing, I want to have all of my JS files listed in my <code>index.html</code> page. But in production mode, I want a single JS file. So I wrote a script to read in my <code>index.html</code> file, find all the references to my Javascript files, and write out a new HTML file that contains a reference to a single JS file that was created by concatenating and compressing all the JS files.</p>
<p>As an example, have a look at what my <code>index.html</code> page looks like in <a href="http://eahanson.com/code/wshlst-index.txt">development mode</a> and in <a href="http://eahanson.com/code/wshlst-index-mini.txt">production mode</a>.</p>
<h3>Ruby Script &rarr; Rake</h3>
<p>When a server move prompted me to upgrade to Subversion and Capistrano, I decided to turn my Ruby script into a Rake task that I could trigger from my Capistrano deploy: <a href="http://www.eahanson.com/code/minify.rake">minify.rake</a></p>
<h3>Capistrano</h3>
<p>I added a line to my Capistrano <code>deploy.rb</code> file to run <code>rake minify</code> on the server after updating from Subversion:</p>
<pre>run "cd #{release_path} &#038;&#038; rake minify RAILS_ENV=#{rails_env}"</pre>
<h3>Apache</h3>
<p>I also added a few lines to my <code>httpd.conf</code> file to support this. First, since the Rake task combines all the JS files into a single JS file but Capistrano deploys the entire app, I wanted to restrict access to everything in the <code>javascripts</code> directory:</p>
<pre># Don't allow access to the javascripts directory (all JS should be in minified js file)
RewriteRule ^/javascripts/ - [F]</pre>
<p>Then, because the Rake task reads <code>index.html</code> and outputs a file called <code>index-mini.html</code>, I wanted to send all requests for <code>/</code> and <code>/index.html</code> to <code>/index-mini.html</code>:</p>
<pre>
# Rewrite index.html to index-mini.html
RewriteRule ^/index.html$ /index-mini.html [QSA]

# Rewrite index to check for static
RewriteRule ^/$ /index-mini.html [QSA]
</pre>
<h3>Why Not Use AssetPackager?</h3>
<p>For a normal Rails application, <a href="http://synthesis.sbecker.net/pages/asset_packager">AssetPackager</a> is probably the right solution to this problem. However, <a href="http://www.eahanson.com/category/wshlstcom/">wshlst</a> doesn&#8217;t use Rails to generate the UI (<a href="http://www.eahanson.com/2007/10/25/wshlstcom-a-webapp-with-a-pure-javascript-ui/">it&#8217;s all done in Javascript</a>), so AssetPackager wouldn&#8217;t work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2008/03/02/a-rake-task-to-concatenate-and-compress-javascript-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My time zone solution: RelativeDate.js and ServerTime.js</title>
		<link>http://www.eahanson.com/2007/10/31/my-time-zone-solution-relativedatejs-and-servertimejs/</link>
		<comments>http://www.eahanson.com/2007/10/31/my-time-zone-solution-relativedatejs-and-servertimejs/#comments</comments>
		<pubDate>Wed, 31 Oct 2007 21:29:05 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[My Software]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[wshlst.com]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2007/10/31/my-time-zone-solution-relativedatejs-and-servertimejs/</guid>
		<description><![CDATA[For my wshlst.com project, I needed to show when each item or comment was created or edited. My initial implementation was to just show the time and date of the change, but people who aren&#8217;t in the same time zone as my server didn&#8217;t like the fact that it showed a different time zone. I [...]]]></description>
			<content:encoded><![CDATA[<p>For my <a href="http://www.eahanson.com/category/wshlstcom/">wshlst.com</a> project, I needed to show when each item or comment was created or edited. My initial implementation was to just show the time and date of the change, but people who aren&#8217;t in the same time zone as my server didn&#8217;t like the fact that it showed a different time zone.</p>
<p>I contemplated having a per-user time zone setting, but that seemed complicated. So I decided to just show how old the item is. For that, I needed to write two things. I&#8217;m posting them here in case anyone else finds them useful.</p>
<h3>RelativeDate.js</h3>
<p>First, I needed a simple relative date formatter, so I wrote a simple one. Given two dates, it returns a string like </p>
<pre>
4 days ago
</pre>
<p>or </p>
<pre>
26 hours ago
</pre>
<p>Here&#8217;s the code and the <a href="http://jsunit.net">JsUnit</a> test:</p>
<p><a href="/code/RelativeDate/RelativeDate.js">RelativeDate.js</a><br />
<a href="/code/RelativeDate/RelativeDateTest.html.txt">RelativeDateTest.html</a></p>
<h3>ServerTime.js</h3>
<p>The timestamps of the items I&#8217;m displaying are stored in the database, so they are based on the clock of my database server. The relative dates are calculated in Javascript, so they are based on the clock of the user, which might be hours or just minutes off from the database server time. </p>
<p>The first thing my app does when it loads is checks to see if the user is logged in, so I decided to piggyback the current server time along with the response. Since the client and server talk using JSON, it&#8217;s simple to add another property to the response:</p>
<pre>
def current
  ...
  render_json({
    :success => true,
    :user => user,
    :servertime => dbtime()
  }.to_json)
end

...

def dbtime
  ActiveRecord::Base
    .connection
    .select_all("select unix_timestamp(now()) as now")[0]["now"]
    .to_i
end
</pre>
<p>I wrote a ServerTime class on the client side and I initialize it when the app receives the current user response from the server:</p>
<pre>
this.serverTime = new ServerTime(json.servertime);
</pre>
<p>Here&#8217;s the code (it&#8217;s very simple):</p>
<p><a href="/code/ServerTime.js">ServerTime.js</a></p>
<h3>Putting It Together&#8230;</h3>
<p>And now to display the relative time, my app only has to do this:</p>
<pre>
var updated = new Date(item.updated_at * 1000);
element.insert(new RelativeDate(updated, app.serverTime.get()).toString());
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/10/31/my-time-zone-solution-relativedatejs-and-servertimejs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing termios gem on Mac OS X</title>
		<link>http://www.eahanson.com/2007/02/03/installing-termios-gem-on-mac-os-x/</link>
		<comments>http://www.eahanson.com/2007/02/03/installing-termios-gem-on-mac-os-x/#comments</comments>
		<pubDate>Sun, 04 Feb 2007 03:13:54 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=13</guid>
		<description><![CDATA[When installing Capistrano, you should install the termios gem so that your password isn't echoed when you type it. Easier said than done.]]></description>
			<content:encoded><![CDATA[<p>When installing <a href="http://manuals.rubyonrails.com/read/chapter/97">Capistrano</a>, you should install the <strong>termios</strong> gem so that your password isn&#8217;t echoed when you type it.</p>
<h4>Easier said than done</h4>
<p>Here&#8217;s the error I got:</p>
<div class="code">$<span style="color: darkgreen">sudo gem install termios</span><br />
Attempting local installation of &#8216;termios&#8217;<br />
Local gem file not found: termios*.gem<br />
Attempting remote installation of &#8216;termios&#8217;<br />
Updating Gem source index for: http://gems.rubyforge.org<br />
Building native extensions.  This could take a while&#8230;<br />
can&#8217;t find header files for ruby.<br />
ERROR:  While executing gem &#8230; (RuntimeError)<br />
ERROR: Failed to build gem native extension.<br />
Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/termios-0.9.4 for inspection.<br />
ruby extconf.rb install termios\n
</div>
<p>It was looking for header files in <tt>/usr/lib/ruby/1.8/powerpc-darwin8.0</tt>, but they weren&#8217;t there; they were in <tt>/usr/lib/ruby/1.8/universal-darwin8.0</tt>.</p>
<h4>The Solution</h4>
<p>The solution was simple enough: create symlinks to the header files:</p>
<pre>$<span style="color: darkgreen">sudo ln -s ../universal-darwin8.0/*.h ./</span>
$<span style="color: darkgreen">sudo gem install termios</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/02/03/installing-termios-gem-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Join Tables with Attributes in Rails</title>
		<link>http://www.eahanson.com/2006/02/12/join-tables-with-attributes-in-rails/</link>
		<comments>http://www.eahanson.com/2006/02/12/join-tables-with-attributes-in-rails/#comments</comments>
		<pubDate>Mon, 13 Feb 2006 04:19:39 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=7</guid>
		<description><![CDATA[I had a little trouble today with join tables that contain attributes.]]></description>
			<content:encoded><![CDATA[<p>I had a little trouble today with join tables that contain attributes. </p>
<p><h4>Creating the join table</h4>
<p>Starting with tables <tt>foos</tt> and <tt>bars</tt>, I created a join table <tt>bars_foos</tt>. (Rails expects the join table&#8217;s name to be a concatenation of the two table names <i>in alphabetical order</i>). </p>
<p><h4>Reading attributes</h4>
<p>I added an attribute <tt>baz</tt> to <tt>bars_foos</tt>. I had originally tried to access it with:</p>
<pre class="wrong">myFoo.baz</pre>
<p>but that didn&#8217;t work. It turns out that I needed to force Rails to do the join before accessing the attribute, which makes sense when you think about it (but it took me a while before my little brain thought about it correctly):</p>
<pre class="right">myFoo.bars.baz</pre>
<p><h4>Adding attributes</h4>
<p>Use the well-documented <tt>push_with_attributes</tt> method:</p>
<pre>myFoo.bars.push_with_attributes(bar, :baz => &quot;fez&quot;)</pre>
<p><h4>Updating attributes</h4>
<p>Apparently you can&#8217;t. Luckily, in my app the attribute is immutable. Other people have suggested deleting and re-inserting the row which I imagine shouldn&#8217;t be a problem for most applications. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2006/02/12/join-tables-with-attributes-in-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Writing a Ruby Test Suite that RDT and Test::Unit Can Understand</title>
		<link>http://www.eahanson.com/2005/09/20/writing-a-ruby-test-suite-that-rdt-and-testunit-can-understand/</link>
		<comments>http://www.eahanson.com/2005/09/20/writing-a-ruby-test-suite-that-rdt-and-testunit-can-understand/#comments</comments>
		<pubDate>Tue, 20 Sep 2005 17:33:10 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=3</guid>
		<description><![CDATA[I'm working on a Ruby project using Eclipse and RDT. RDT provides a couple useful features, including support for running unit tests with Test::Unit. Unfortunately, it's pretty primitive at the moment. One big problem is that it can't run all your tests. So we wrote a test suite class that runs all tests and can be run from Test::Unit:.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a <a href="http://www.ruby-lang.org">Ruby</a> project using <a href="http://www.eclipse.org">Eclipse</a> and <a href="http://rubyeclipse.sourceforge.net/">RDT</a>. RDT provides a couple useful features, including support for running unit tests with Test::Unit. Unfortunately, it&#8217;s pretty primitive at the moment.</p>
<p>One big problem is that it can&#8217;t run all your tests. So we wrote a test suite class that runs all tests and can be run from Test::Unit:</p>
<pre>
require 'test/unit'

Dir.chdir(&quot;test&quot;)
@@test_files = Dir.glob(&quot;**/*_test.rb&quot;)
@@test_files.each {|x| require x} 

class TestSuite
  def self.suite
    suite = Test::Unit::TestSuite.new

    @@test_files.each do |filename|
      File.open(filename) do |file|
        file.each_line do |line|
          if match = (/class\s+(\w+Test)/).match(line)
           suite &lt;&lt; eval(match[1] + &quot;.suite&quot;)
          end
        end
      end
    end

    return suite

  end
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2005/09/20/writing-a-ruby-test-suite-that-rdt-and-testunit-can-understand/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing Ruby on Rails on Mac OS X 10.4 (Tiger)</title>
		<link>http://www.eahanson.com/2005/09/07/instaling-ruby-on-rails-on-mac-os-x-104-tiger/</link>
		<comments>http://www.eahanson.com/2005/09/07/instaling-ruby-on-rails-on-mac-os-x-104-tiger/#comments</comments>
		<pubDate>Wed, 07 Sep 2005 16:55:29 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=2</guid>
		<description><![CDATA[I had a hell of a time getting Ruby on Rails to work on Mac OS X 10.4 (Tiger). Here's what ended up working for me.]]></description>
			<content:encoded><![CDATA[<p>I had a hell of a time getting <a href="http://www.rubyonrails.org/">Ruby on Rails</a> to work on <a href="http://www.apple.com/macosx/">Mac OS X 10.4 (Tiger)</a>.</p>
<p>Here&#8217;s what ended up working for me. It&#8217;s based on the <a href="http://jamie.blogthing.com/2005/05/12/installing-rails-on-a-fresh-tiger/">instructions on Jamie&#8217;s Blogomatic</a>.</p>
<p>1. Install the XCode tools from the Tiger DVD.</p>
<p>2. Download and run the Ruby on Rails installer from: <a href="http://users.tpg.com.au/aarnold/Ruby%20on%20Rails.zip">http://users.tpg.com.au/aarnold/Ruby%20on%20Rails.zip</a>.</p>
<p>3. Download and run the MySQL installer (I used version 4.1.11): <a href="http://dev.mysql.com/downloads/index.html">http://dev.mysql.com/downloads/index.html</a>.</p>
<p>4. Start mysql:  <tt>bash$ <b>sudo /usr/local/mysql/bin/mysqld_safe</b></tt></p>
<p>5. Put mysql into the background: <tt><b>ctrl-z</b></tt> and then <tt>bash$ <b>bg</b></tt></p>
<p>6. Update gems: <tt>bash$ <b>sudo gem update</b></tt></p>
<p>7. Install the mysql gem <b>from the remote source</b>: <tt>bash$ <b>sudo gem install -r mysql -- --with-mysql-dir=/usr/local/mysql</b></tt></p>
<p>8. Compile and install the MySQL API module from here: <a href="http://www.tmtm.org/en/mysql/ruby/">http://www.tmtm.org/en/mysql/ruby/</a>.</p>
<p>9. All done! </p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2005/09/07/instaling-ruby-on-rails-on-mac-os-x-104-tiger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
