<?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"
	>

<channel>
	<title>Erik A. Hanson</title>
	<atom:link href="http://www.eahanson.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eahanson.com</link>
	<description>My weblog</description>
	<pubDate>Tue, 24 Jun 2008 05:12:52 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>New Test Runner for JsUnit</title>
		<link>http://www.eahanson.com/2008/05/18/new-test-runner-for-jsunit/</link>
		<comments>http://www.eahanson.com/2008/05/18/new-test-runner-for-jsunit/#comments</comments>
		<pubDate>Mon, 19 May 2008 02:01:45 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[JsUnit]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2008/05/18/new-test-runner-for-jsunit/</guid>
		<description><![CDATA[Christian Williams created a new test runner for JsUnit. More info here.
]]></description>
			<content:encoded><![CDATA[<p>Christian Williams created a new test runner for JsUnit. <a href="http://pivots.pivotallabs.com/users/kelly/blog/articles/450-standup-5-7-2008">More info here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2008/05/18/new-test-runner-for-jsunit/feed/</wfw:commentRss>
		</item>
		<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 listed in [...]]]></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>
		</item>
		<item>
		<title>Firing mouse events in tests</title>
		<link>http://www.eahanson.com/2008/01/01/firing-mouse-events-in-tests/</link>
		<comments>http://www.eahanson.com/2008/01/01/firing-mouse-events-in-tests/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 04:17:48 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[JsUnit]]></category>

		<category><![CDATA[Unit Testing]]></category>

		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2008/01/01/firing-mouse-events-in-tests/</guid>
		<description><![CDATA[The Bad News
Sending mouse events such as click and mouseover in JsUnit tests can be really hard.
More Bad News
Prototype doesn&#8217;t make it any easier. Sam Stephenson says:

We would very much like to support it in the future.  It&#8217;s fairly complicated to implement native event firing across all supported browsers, so in 1.6.0, fire works [...]]]></description>
			<content:encoded><![CDATA[<h4>The Bad News</h4>
<p>Sending mouse events such as <code>click</code> and <code>mouseover</code> in <a href="http://jsunit.net">JsUnit</a> tests can be really hard.</p>
<h4>More Bad News</h4>
<p><a href="http://prototypejs.org">Prototype</a> doesn&#8217;t make it any easier. Sam Stephenson <a href="http://groups.google.com/group/prototype-core/browse_thread/thread/9fec287978138250">says</a>:</p>
<blockquote><p>
We would very much like to support it in the future.  It&#8217;s fairly complicated to implement native event firing across all supported browsers, so in 1.6.0, fire works with custom events only.
</p></blockquote>
<h4>YUI To The Rescue</h4>
<p><a href="http://developer.yahoo.com/yui/yuitest/#useractions">YAHOO.util.UserActions</a> can simulate some user actions. Unfortunately, calls to YUI can look a bit clunky in a Prototype-heavy codebase:</p>
<pre>
var element = new Element("div").insert("Hi");
var offset = element.cumulativeOffset();
YAHOO.util.UserAction.click(element, { shiftKey: true });
</pre>
<h4>YUI + Prototype FTW</h4>
<p>A little mixin magic:</p>
<pre>
Element.addMethods({
  simulateClick: YAHOO.util.UserAction.click.bind(YAHOO.util.UserAction),
  simulateDblClick: YAHOO.util.UserAction.dblclick.bind(YAHOO.util.UserAction),
  simulateMousedown: YAHOO.util.UserAction.mousedown.bind(YAHOO.util.UserAction),
  simulateMouseup: YAHOO.util.UserAction.mouseup.bind(YAHOO.util.UserAction),
  simulateMouseover: YAHOO.util.UserAction.mouseover.bind(YAHOO.util.UserAction),
  simulateMouseout: YAHOO.util.UserAction.mouseout.bind(YAHOO.util.UserAction),
  simulateMousemove: YAHOO.util.UserAction.mousemove.bind(YAHOO.util.UserAction)
});
</pre>
<p>and now our test code looks nicer:</p>
<pre>
var element = new Element("div").insert("Hi");
var offset = element.cumulativeOffset();
myElement.simulateClick({ shiftKey: true });
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2008/01/01/firing-mouse-events-in-tests/feed/</wfw:commentRss>
		</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 contemplated [...]]]></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>
		</item>
		<item>
		<title>wshlst.com: A Webapp With A Pure-Javascript UI</title>
		<link>http://www.eahanson.com/2007/10/25/wshlstcom-a-webapp-with-a-pure-javascript-ui/</link>
		<comments>http://www.eahanson.com/2007/10/25/wshlstcom-a-webapp-with-a-pure-javascript-ui/#comments</comments>
		<pubDate>Thu, 25 Oct 2007 22:51:08 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[My Software]]></category>

		<category><![CDATA[Web development]]></category>

		<category><![CDATA[wshlst.com]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2007/10/25/wshlstcom-a-webapp-with-a-pure-javascript-ui/</guid>
		<description><![CDATA[I created a little wish list website called wshlst.com for my family to use. (Read more about it here.) I worked on it a little bit before Christmas last year, and a little bit again this year.
It&#8217;s been a lot of fun to write, and one reason is that the entire UI is written in [...]]]></description>
			<content:encoded><![CDATA[<p>I created a little wish list website called <a href="http://www.wshlst.com/">wshlst.com</a> for my family to use. (<a href="http://www.eahanson.com/2007/10/24/wshlstcom/">Read more about it here</a>.) I worked on it a little bit before Christmas last year, and a little bit again this year.</p>
<p>It&#8217;s been a lot of fun to write, and one reason is that the entire UI is written in Javascript. No HTML, no CSS. There&#8217;s no HTML generation on the server-side either; it&#8217;s all client-side Javascript. I&#8217;d really like to write more apps like this.</p>
<h3>The Server</h3>
<p>There is of course a server-side component. I decided to write the server in Rails because I had just finished working on a Rails project at <a href="http://www.pivotallabs.com/">Pivotal Labs</a> and had Rails on the brain. Plus, my web host, <a href="http://www.dreamhost.com/r.cgi?29771">DreamHost</a>, supports Rails and doesn&#8217;t support Java, which is the language I&#8217;ve written all my other web apps in.</p>
<p>DreamHost&#8217;s support of Rails is a bit limited though: they kill any long-running processes, which means that a response from your Rails app could take 10 seconds if no other requests have been made lately. It&#8217;s good enough for a little project like Wshlst with only a few users and for initial development, but not much beyond that. (If I were to do it over again, I&#8217;d probably just use Ruby CGI, or maybe even PHP. Or use a VPS from <a href="http://www.rimuhosting.com/">Rimu</a>, but that costs money.)</p>
<p>Because all of the UI is in Javascript, the Rails side of things doesn&#8217;t have to generate any HTML. Instead, it takes standard Rails requests and returns <a href="http://json.org/">JSON</a> using <a href="http://json.rubyforge.org/">JSON for Ruby</a>. For example, the client sends the following AJAX request:</p>
<pre>
GET http://wshlst.com/user/list
</pre>
<p>and the server uses ActiveRecord magic to find all the users that are &#8220;friends&#8221; with the current user and uses JSON for Ruby to convert that list to JSON, which it returns back to the client like this:</p>
<pre>
[{name: "Andy Acorn", id: 12, email: "andy@example.com"},
{name: "Betty Beets", id: 13, email: "betty@example.com"},
{name: "Chad Carrot", id: 14, email: "chad@example.com"},
{name: "Donna Donut", id: 15, email: "donna@example.com"}]
</pre>
<p>which Prototype automatically converts to Javascript objects.</p>
<p>The server is of course also responsible for sending static files, but those are all handled by DreamHost&#8217;s Apache servers.</p>
<h3>The Client</h3>
<p>Here&#8217;s where it gets interesting. The app consists of an HTML file that does little more than include the Javascript files. (In development mode, there are multiple Javascript files, but when I deploy to production, there&#8217;s just one file. <a href="http://www.eahanson.com/2007/10/16/a-ruby-script-to-concatenate-and-compress-javascript-files/">Read this blog post on concatenating and compressing Javascript files with Ruby</a>.)</p>
<p>The app relies heavily on <a href="http://prototypejs.org/">Prototype</a>, and also uses <a href="http://www.berniecode.com/writing/animator.html">Animator.js</a> for a few visual effects. Everything else I wrote from scratch. I like to write a lot of code from scratch when I&#8217;m working on a personal project so I can get a good understanding of what&#8217;s really going on and be able to better evaluate pre-written libraries when I&#8217;m programming for money.</p>
<p>The app&#8217;s main class is called Application and acts as a controller. There are many view classes which are combined to form the app&#8217;s UI. For example, the &#8220;Change Password&#8221; box is implemented by a ChangePasswordForm class and consists of a ModalBox with a FormPanel inside. The FormPanel contains instances of Buttons and Fields. Writing a Field class of course takes more time than putting <tt>&lt;input type=&#8221;text&#8221;&gt;</tt> in some template file somewhere, but it allows for easy reuse and the ability to add a feature to all fields in just one place. (I hope to post some of the code from Wshlst on this blog in the future.)</p>
<p>Responding to user actions is just a matter of one Javascript method directly calling another method. No parsing of parameters, no layers of actions and filters, no guessing client state on the server side. It&#8217;s very liberating. </p>
<h3>Lessons Learned</h3>
<p><b>Test early and often in lots of browsers.</b> This is obviously important when doing any web development, but when it&#8217;s 100% Javascript, there&#8217;s even more chance of incompatibilities. My <a href="http://jsunit.net/">JsUnit</a> tests caught some issues (<tt>Date.now</tt> turns out to be Firefox-only), but most issues I had were visual.</p>
<p><b>MVC is good.</b> Again, I already knew this, but I didn&#8217;t do nearly a good enough job with it in this app, probably because it was the first time I wrote an entire UI in Javascript. Next time, I&#8217;ll have multiple controllers and keep app logic out of the views as much as possible.</p>
<p><b>Events are probably the way to go.</b> Wshlst suffers from some tight coupling of objects because they directly notify each other of actions. Next time, I&#8217;ll try having all my objects send custom events to and receive custom events from some global event source (perhaps <tt>document.body</tt>), like I did back in the days when I wrote Mac software.</p>
<p><b>UI is hard.</b> I&#8217;m glad I did all the UI myself, but next time I&#8217;d like to build on the work of others. I&#8217;m still looking for a good Javascript widget library. Some of the widget libraries I&#8217;ve seen are trying to make web apps look application-y, and I&#8217;d prefer my web app to look web-y. Also, some libraries don&#8217;t have great support for calling everything from Javascript. I think I&#8217;ll look into <a href="http://dojotoolkit.org/">Dojo</a> again now that version 1.0 has been released, and more importantly, now that there&#8217;s more documentation.</p>
<h3>Try It Out</h3>
<p>There&#8217;s no way yet to sign up for Wshlst (if there&#8217;s enough interest I&#8217;ll do it), but the front page of the site lists some demo users you can use to play around: <a href="http://www.wshlst.com/">www.wshlst.com</a>.</p>
<p><i><a href="http://www.eahanson.com/category/wshlstcom/">Read more articles about wshlst.com</a></i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/10/25/wshlstcom-a-webapp-with-a-pure-javascript-ui/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introducing wshlst.com</title>
		<link>http://www.eahanson.com/2007/10/24/wshlstcom/</link>
		<comments>http://www.eahanson.com/2007/10/24/wshlstcom/#comments</comments>
		<pubDate>Wed, 24 Oct 2007 19:06:57 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[My Software]]></category>

		<category><![CDATA[wshlst.com]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2007/10/24/wshlstcom/</guid>
		<description><![CDATA[wshlst.com is a wish list website I created for my family to use. 
It differs from other wish list sites in a few ways: it&#8217;s group-oriented, it allows people to add items to other peoples&#8217; wish lists (secretly, if desired), and it allows people to discuss wish list items (again, secretly, if desired). The result [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.wshlst.com/">wshlst.com</a> is a wish list website I created for my family to use. </p>
<p>It differs from other wish list sites in a few ways: it&#8217;s group-oriented, it allows people to add items to other peoples&#8217; wish lists (secretly, if desired), and it allows people to discuss wish list items (again, secretly, if desired). The result is a site that lets a group of people plot and scheme behind each others&#8217; backs to make gift buying easier.</p>
<h3>Site Tour</h3>
<p><img src="/images/wshlst/overview.jpg" width="400" height="220" class="articleImage"></p>
<p>The site&#8217;s main page is divided into three panels. The first is called &#8220;People&#8221; and lists all the people you share wish lists with. For me, it lists everyone in my family and everyone in my wife&#8217;s family. </p>
<p><img src="/images/wshlst/people.jpg" width="311" height="197" class="articleImage"></p>
<p>Clicking on a person loads their wish list in the next panel, the &#8220;Wish List&#8221; panel. As you&#8217;d expect, you can add and remove items from your wish list. But you can also add and remove items from other peoples&#8217; wish lists. By default, when you add an item to someone else&#8217;s list, it&#8217;s not visible to that person. Items can also be marked as &#8220;Claimed&#8221; so two people don&#8217;t end up buying the same gift. </p>
<p><img src="/images/wshlst/wishlist.jpg" width="313" height="289" class="articleImage"></p>
<p>Clicking on a wish list item loads comments about the item in the third panel, called &#8220;Discussion&#8221;. Comments in the discussion panel are also hidden from the item&#8217;s intended recipient by default. </p>
<p><img src="/images/wshlst/discussion.jpg" width="312" height="150" class="articleImage"></p>
<h3>Try It Out</h3>
<p>There&#8217;s no way to sign up to use the site (yet&mdash;if there&#8217;s interest from anyone outside my family, I might take the time to add the ability), but there are demo users you can use to try the site out. Details are on the front page of the site: <a href="http://www.wshlst.com/">www.wshlst.com</a>.</p>
<p><i><a href="http://www.eahanson.com/category/wshlstcom/">Read more posts about wshlst.com</a></i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/10/24/wshlstcom/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Ruby Script to Concatenate and Compress Javascript Files</title>
		<link>http://www.eahanson.com/2007/10/16/a-ruby-script-to-concatenate-and-compress-javascript-files/</link>
		<comments>http://www.eahanson.com/2007/10/16/a-ruby-script-to-concatenate-and-compress-javascript-files/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 06:07:38 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2007/10/16/a-ruby-script-to-concatenate-and-compress-javascript-files/</guid>
		<description><![CDATA[I&#8217;ve got an HTML page that includes a bunch of Javascript files, which makes development easy but which hurts performance in production.
The standard solution is to concatenate all the Javascript files into one big file and then compress it. I wanted it to be automated of course, and I wanted to do it at deploy-time, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got an HTML page that includes a bunch of Javascript files, which makes development easy but which <a href="http://yuiblog.com/blog/2006/11/28/performance-research-part-1/">hurts performance</a> in production.</p>
<p>The standard solution is to concatenate all the Javascript files into one big file and then compress it. I wanted it to be automated of course, and I wanted to do it at deploy-time, not at run-time. </p>
<p>So I wrote a little script that figures out what Javascript files are being included in the HTML page, compresses them with the <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>, concatenates them into a file named after the newly-compressed and -concatenated Javascript&#8217;s MD5 hash, and creates a copy of the original HTML page that references the new Javascript file instead of the old ones.</p>
<p>It&#8217;s pretty specific to my app&#8217;s needs, but with some minor tweaking, it should be useable on other projects. Check it out: <b><a href="http://www.eahanson.com/code/minify.rb">minify.rb</a></b></p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/10/16/a-ruby-script-to-concatenate-and-compress-javascript-files/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Big New Feature In JsUnit 2.2 alpha 25</title>
		<link>http://www.eahanson.com/2007/08/13/big-new-feature-in-jsunit-22-alpha-25/</link>
		<comments>http://www.eahanson.com/2007/08/13/big-new-feature-in-jsunit-22-alpha-25/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 08:21:33 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[JsUnit]]></category>

		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2007/08/13/big-new-feature-in-jsunit-22-alpha-25/</guid>
		<description><![CDATA[In order to run all test functions in a test page, JsUnit naturally needs to know what those test functions are. It's able to get a list of those functions from Firefox, Safari and Opera, but it's not able to get them from Internet Explorer. So in IE, JsUnit has to resort to searching the text of all scripts for strings that look like test function names. Unfortunately, it doesn't understand comments so commented-out test functions will end up running anyway. ]]></description>
			<content:encoded><![CDATA[<p>In order to run all test functions in a test page, <a href="http://www.jsunit.net/">JsUnit</a> naturally needs to know what those test functions are. It&#8217;s able to get a list of those functions from Firefox, Safari and Opera, but it&#8217;s not able to get them from Internet Explorer. </p>
<p>So in IE, JsUnit has to resort to searching the text of all scripts for strings that look like test function names. Unfortunately, it doesn&#8217;t understand comments so commented-out test functions will end up running anyway. </p>
<p>Recently, Dean McNamee and <a href="http://pupius.co.uk/">Dan Pupius</a> alerted <a href="http://edwardh.com/">Edward Hieatt</a> and me to an undocumented Internet Explorer Javascript function called <tt>RuntimeObject</tt> and suggested JsUnit might be able to use it to discover test functions. </p>
<p>I replaced the text searching with <tt>RuntimeObject</tt> and tagged it as version 2.2 alpha 25. (I also added an <tt>assertEqualsIgnoringOrder</tt> function while I was making changes.)</p>
<p>Browse the new version <a href="http://jsunit.svn.sourceforge.net/viewvc/jsunit/tags/v2_2alpha25/jsunit/">at SourceForge</a> or check it out like this:</p>
<div class="code">svn co https://jsunit.svn.sf.net/svnroot/jsunit/tags/v2_2alpha25/jsunit/ jsunit</div>
<p>Try it out and <a href="http://tech.groups.yahoo.com/group/jsunit/">let us know</a> if there are any problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/08/13/big-new-feature-in-jsunit-22-alpha-25/feed/</wfw:commentRss>
		</item>
		<item>
		<title>An AppleScript to Clean Up My Downloads Folder</title>
		<link>http://www.eahanson.com/2007/08/11/an-applescript-to-clean-up-my-downloads-folder/</link>
		<comments>http://www.eahanson.com/2007/08/11/an-applescript-to-clean-up-my-downloads-folder/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 18:55:02 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[AppleScript]]></category>

		<category><![CDATA[Mac OS X]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2007/08/11/an-applescript-to-clean-up-my-downloads-folder/</guid>
		<description><![CDATA[My downloads folder always gets so full after a while that I have a hard time finding things. I like to keep stuff around for a little while in case I need it again, and if I have the space, I like to keep things around for a long time, just in case. So I wrote a simple AppleScript to move old files to an "Old" subdirectory.]]></description>
			<content:encoded><![CDATA[<p>My downloads folder always gets so full after a while that I have a hard time finding things. I like to keep stuff around for a little while in case I need it again, and if I have the space, I like to keep things around for a long time, just in case.</p>
<p>So I wrote a simple AppleScript to move old files to an &#8220;Old&#8221; subdirectory. I thought I&#8217;d post it here in case anyone else needed something similar. It&#8217;s a simple script, but AppleScript is hard for me because I write scripts so rarely that I forget everything about it before I need to write another script.</p>
<p>(Many years ago, I actually got paid to write a few AppleScripts.)</p>
<pre>
set folderName to "Macintosh HD:Users:<em>your-user-name</em>:Temp:&#8221;

repeat with fileName in list folder folderName
  set tempFile to (folderName as string) &amp; contents of fileName
  set modificationDate to modification date of (info for alias tempFile)
  set age to ((current date) - modificationDate) / days

  if age &gt; 7 and fileName is not &#8221; Clean Up Temp Dir&#8221; then
    tell application &#8220;Finder&#8221;
      move alias tempFile to alias (folderName &amp; &#8221; Old&#8221;)
    end tell
  end if
end repeat
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/08/11/an-applescript-to-clean-up-my-downloads-folder/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Minor New Features In JsUnit 2.2alpha24</title>
		<link>http://www.eahanson.com/2007/08/09/minor-new-features-in-jsunit-22alpha24/</link>
		<comments>http://www.eahanson.com/2007/08/09/minor-new-features-in-jsunit-22alpha24/#comments</comments>
		<pubDate>Fri, 10 Aug 2007 01:46:36 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[JsUnit]]></category>

		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2007/08/09/minor-new-features-in-jsunit-22alpha24/</guid>
		<description><![CDATA[I added a few new features to JsUnit and tagged it as version 2.2 alpha 24: new log button, new URL parameters, and easier suite creation.]]></description>
			<content:encoded><![CDATA[<p>I added a few new features to <a href="http://jsunit.net">JsUnit</a> and tagged it as version 2.2 alpha 24. Browse the new version <a href="http://jsunit.svn.sourceforge.net/viewvc/jsunit/tags/v2_2alpha24/jsunit/">at SourceForge</a> or check it out like this:</p>
<div class="code">
  svn co https://jsunit.svn.sf.net/svnroot/jsunit/tags/v2_2alpha24/jsunit/ jsunit
</div>
<h3>Show Log</h3>
<p>There&#8217;s now a &#8220;Show Log&#8221; button that opens a window with a log of what JsUnit has been up to.</p>
<p><a href="http://www.eahanson.com/weblog/wp-content/uploads/2007/08/testrunner.jpg" title="New Log Button"><img src="http://www.eahanson.com/weblog/wp-content/uploads/2007/08/testrunner.thumbnail.jpg" alt="New Log Button" /></a></p>
<p>New log button (click image for larger view)</p>
<p><a href="http://www.eahanson.com/weblog/wp-content/uploads/2007/08/log.gif" title="New Log Window"><img src="http://www.eahanson.com/weblog/wp-content/uploads/2007/08/log.thumbnail.gif" alt="New Log Window" /></a></p>
<p>New log window (click image for larger view)</p>
<h3>New URL Parameters</h3>
<p>There are three new URL parameters:</p>
<ul>
<li><tt>suppressDialogs=true</tt> will suppress JsUnit&#8217;s dialogs in interactive mode</li>
<li><tt>setupPageTimeout=<em>n</em></tt> will change the timeout for the setupPage call</li>
<li><tt>pageLoadTimeout=<em>n</em></tt> will change the page load timeout</li>
</ul>
<h3>Easier Suite Creation</h3>
<p>The <tt>jsUnitTestSuite</tt> constructor now accepts test pages and subsuites as parameters. So instead of this:</p>
<pre>
function suite() {
  var suite = new jsUnitTestSuite();
  suite.addTestPage("/foo/barTest.html");
  suite.addTestPage("/foo/bazTest.html");
  return suite;
}</pre>
<p>you can do this:</p>
<pre>
function suite() {
  return new jsUnitTestSuite("/foo/barTest.html", "/foo/bazTest.html");
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/08/09/minor-new-features-in-jsunit-22alpha24/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Ruby Script To Generate a JsUnit Suite</title>
		<link>http://www.eahanson.com/2007/05/26/a-ruby-script-to-generate-a-jsunit-suite/</link>
		<comments>http://www.eahanson.com/2007/05/26/a-ruby-script-to-generate-a-jsunit-suite/#comments</comments>
		<pubDate>Sat, 26 May 2007 20:38:29 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Howto]]></category>

		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[JsUnit]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=16</guid>
		<description><![CDATA[I've got a bunch of JsUnit tests for a project I'm working on and I wanted an easy way to create a JsUnit suite that can run all the tests but also run any individual test page. Also, I wanted to be able to view any test page so I could debug the tests with Firebug. I also didn't want to run a web server all the time, so I wanted a script that would create a static HTML file.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got a bunch of JsUnit tests for a project I&#8217;m working on and I wanted an easy way to create a JsUnit suite that can run all the tests but also run any individual test page.</p>
<p>Also, I wanted to be able to view any test page so I could debug the tests with Firebug (as explained <a href="http://www.eahanson.com/weblog/?p=11">here</a>).</p>
<p>I also didn&#8217;t want to run a web server all the time, so I wanted a script that would create a static HTML file.</p>
<p>Here&#8217;s what I came up with. It&#8217;s not configurable, so you&#8217;ll have to modify it for your own needs.</p>
<p align="center"><strong><a href="/code/JsUnitSuiteBuilder.rb">JsUnitSuiteBuilder.rb</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/05/26/a-ruby-script-to-generate-a-jsunit-suite/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Element.stylize via Prototype.js</title>
		<link>http://www.eahanson.com/2007/05/25/elementstylize-via-prototypejs/</link>
		<comments>http://www.eahanson.com/2007/05/25/elementstylize-via-prototypejs/#comments</comments>
		<pubDate>Fri, 25 May 2007 20:13:18 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=15</guid>
		<description><![CDATA[I often want to add a bunch of styles to an element, so I wrote a simple stylize method.]]></description>
			<content:encoded><![CDATA[<p>I often want to add a bunch of styles to an element, so I wrote a <tt>stylize</tt> method so I can do something like this:</p>
<pre>
$("foo").stylize({
  color: red,
  background-color: green,
  text-decoration: underline
});</pre>
<p>Implementing <tt>stylize</tt> is easy with <a href="http://prototypejs.org/">Prototype</a>&#8217;s <a href="http://prototypejs.org/api/element/addMethods">Element.addMethods()</a> method:</p>
<pre>
var ElementExtensions = {
  stylize: function(element, styles) {
    for (var s in styles) {
      element.style[s] = styles[s];
    }
    return element;
  }
}
Element.addMethods(ElementExtensions);</pre>
<p><b>Update:</b> <a href="http://prototypejs.org/2007/8/15/prototype-1-6-0-release-candidate">Prototype 1.6</a> added a similar <tt>setStyle</tt> method which works similarly but also allows CSS string-type style declaration (e.g., <tt>myElement.setStyle(&#8221;color: blue; border: 3px&#8221;)</tt>). Still, the above is a nice example of <tt>Element.addMethods()</tt>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/05/25/elementstylize-via-prototypejs/feed/</wfw:commentRss>
		</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>
		</item>
		<item>
		<title>Free Website Monitoring</title>
		<link>http://www.eahanson.com/2007/01/14/free-website-monitoring/</link>
		<comments>http://www.eahanson.com/2007/01/14/free-website-monitoring/#comments</comments>
		<pubDate>Mon, 15 Jan 2007 02:31:17 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Services]]></category>

		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=12</guid>
		<description><![CDATA[I was looking around for a good website monitoring service or script, and I found Montastic which is a free website monitoring service.]]></description>
			<content:encoded><![CDATA[<p>I was looking around for a good website monitoring service or script, and I found <a href="http://www.montastic.com/">Montastic</a> which is a free website monitoring service.</p>
<p>There&#8217;s even a <a href="http://widgets.yahoo.com/gallery/view.php?widget=38681">Monstatic Widget</a> for <a href="http://widgets.yahoo.com/">Yahoo Widgets</a>.</p>
<p>I only wish it had <a href="http://www.jabber.org">Jabber</a> notification in addition to email notification.</p>
<p>I also read about <a href="http://nubyonrails.topfunky.com/articles/2006/03/29/surviving-rails-1-1-with-server-monitoring">dwatch</a> but I figured a service would be easier.</p>
<p><strong>Update:</strong> I got an email from Arun who works for <a href="http://www.site24x7.com">Site24&#215;7.com</a>. It seems to have more features than Montastic, such as being able to look for a particular keyword in the response (or not in the response), and nice looking reports. I&#8217;ll be trying them out alongside Montastic. (Unrelatedly, the makers of Site24&#215;7 also make <a href="http://www.zoho.com/">Zoho</a>, a suite of productivty apps like a word processor, spreadsheet, etc. Somehow I&#8217;d never heard of them.)</p>
<p><strong>Update 2</strong>: My pals at <a href="http://pivotallabs.com/">Pivotal Labs</a> have released <a href="http://monitwitter.com/">Monitwitter</a>, which uses Twitter to show you the status of your servers. Brilliant!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/01/14/free-website-monitoring/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Debugging a JsUnit Test With Firebug</title>
		<link>http://www.eahanson.com/2006/11/16/debugging-a-jsunit-test-with-firebug/</link>
		<comments>http://www.eahanson.com/2006/11/16/debugging-a-jsunit-test-with-firebug/#comments</comments>
		<pubDate>Thu, 16 Nov 2006 07:36:53 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Howto]]></category>

		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[JsUnit]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=11</guid>
		<description><![CDATA[Sometimes I find that I want to debug a JsUnit test because I'm not quite sure what's going on in the browser. FireBug has a nice debugger, but I couldn't get it to work with the JsUnit test runner. I could set breakpoints in JsUnit itself, but any breakpoints in my test would be ignored. And then it hit me: ditch the test runner.]]></description>
			<content:encoded><![CDATA[<p>Sometimes I find that I want to debug a <a href="http://www.jsunit.net/">JsUnit</a> test because I&#8217;m not quite sure what&#8217;s going on in the browser.</p>
<p><a href="http://getfirebug.com/">FireBug</a> has a nice debugger, but I couldn&#8217;t get it to work with the JsUnit test runner. I could set breakpoints in JsUnit itself, but any breakpoints in my test would be ignored.</p>
<p>And then it hit me: ditch the test runner. I opened my test page directly in Firefox, opened FireBug, clicked the Debugger tab, and chose my test from the &#8220;Scripts&#8221; menu. Here&#8217;s a simplified version:</p>
<pre>
function testUpdate() {
  var listPanel = new ListPanel();
  listPanel.update(["one", "two", "three"]);

  var children = $(&#8221;content&#8221;).childNodes;
  assertEquals(&#8221;one&#8221;, children[0]);
  assertEquals(&#8221;two&#8221;, children[1]);
  assertEquals(&#8221;three&#8221;, children[2]);
}</pre>
<p>I put a breakpoint at the first <tt>assertEquals</tt> and switched to the Console tab and typed <tt>testUpdate()</tt>. I saw the ListPanel getting drawn in the browser (something you don&#8217;t see in the JsUnit test runner) and in the Debugger tab I could inspect all my variables.</p>
<p>But even better, I could go back to the Console tab and start evaluating Javascript.</p>
<pre>
&gt;&gt;&gt; children.length
3</pre>
<p>Good.</p>
<pre>
&gt;&gt;&gt; children[0]
div</pre>
<p>Bad.</p>
<pre>
&gt;&gt;&gt; children[0].firstChild
[Text] &#8220;one&#8221;</pre>
<p>Better.</p>
<pre>
&gt;&gt;&gt; children[0].firstChild.nodeValue
&#8220;one&#8221;</pre>
<p>Perfect.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2006/11/16/debugging-a-jsunit-test-with-firebug/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Bold Prompt in Bash</title>
		<link>http://www.eahanson.com/2006/02/12/a-bold-prompt-in-bash/</link>
		<comments>http://www.eahanson.com/2006/02/12/a-bold-prompt-in-bash/#comments</comments>
		<pubDate>Mon, 13 Feb 2006 04:20:29 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Howto]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=8</guid>
		<description><![CDATA[I wanted my Bash prompt to be bold and red so I could find my one line of input amongst the million lines of output in my terminal. I'm no Unix nerd, so I don't really know offhand where to look for this info. After a bunch of Googling, I found something that works for me.]]></description>
			<content:encoded><![CDATA[<p>I wanted my Bash prompt to be bold and red so I could find my one line of input amongst the million lines of output in my terminal. I&#8217;m no Unix nerd, so I don&#8217;t really know offhand where to look for this info. After a bunch of Googling, I found something that works for me. The shell variable for the prompt is naturally called <tt>PS1</tt>. The original value was:</p>
<pre>PS1='\\h:\\w \\u\\$ '</pre>
<p>The code for starting a font and color change is <tt>\\e[</tt>. The code for bold red is <tt>1;31</tt>. The code for closing the starting code for a font and color tag is <tt>m</tt>. The closing code for a font and color change is <tt>\\e[m</tt>. Bash thinks that these color commands will increase the length of the prompt, which will cause odd line wrapping behavior. The solution is to wrap all the color codes in brackets (<tt>\\[</tt> and <tt>\\]</tt>).</p>
<p>
So, the entire prompt looks like this:</p>
<pre>PS1='\\[\\e[1;31m\\]\\h:\\w \\u\\$\\[\\e[m\\] &#8216;</pre>
<p>Eeech. Add that to your <tt>.bashrc</tt> file and you&#8217;re all set. If you don&#8217;t want the red, the prompt would be:</p>
<pre>PS1='\\[\\e[1m\\]\\h:\\w \\u\\$\\[\\e[m\\] &#8216;</pre>
<p>
More colors are listed at the bottom of <a href="http://networking.ringofsaturn.com/Unix/Bash-prompts.php">this page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2006/02/12/a-bold-prompt-in-bash/feed/</wfw:commentRss>
		</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>
		</item>
		<item>
		<title>Setting XSL Parameters from Javascript in AJAXSLT</title>
		<link>http://www.eahanson.com/2005/10/23/setting-xsl-parameters-from-javascript-in-ajaxslt/</link>
		<comments>http://www.eahanson.com/2005/10/23/setting-xsl-parameters-from-javascript-in-ajaxslt/#comments</comments>
		<pubDate>Sun, 23 Oct 2005 23:34:22 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Howto]]></category>

		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[XSL]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=6</guid>
		<description><![CDATA[Recently, I was trying to pass some info from my Javascript into my XSL stylesheet. Here's how I finally did it.]]></description>
			<content:encoded><![CDATA[<p>Google&#8217;s <a href="http://goog-ajaxslt.sourceforge.net/">AJAXSLT</a> library is very nice, but it is lacking in documentation and sometimes hard to figure out.</p>
<p>
Recently, I was trying to pass some info from my Javascript into my XSL stylesheet. Here&#8217;s how I finally did it:</p>
<p>
<i>(Quick note: in real life, I don&#8217;t name variables &quot;myThis&quot; and &quot;myThat&quot;; I&#8217;m just doing it here to make it clear what is my code and what is part of XSL, AJAXSLT, Javascript, etc.)</i></p>
<p><h4>The XSL</h4>
<pre>
&lt;xsl:stylesheet version=&quot;1.0&quot;
    xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:param name=&quot;myParam&quot; select=&quot;&apos;myDefault&apos;&quot;/&gt;
  &lt;xsl:template match=&quot;/&quot;&gt;
    myParam: &lt;xsl:value-of select=&quot;$myParam&quot;/&gt;
  &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p><h4>The Javascript</h4>
<pre>
var myContext = new ExprContext(myXml);
myContext.setVariable(&quot;myParam&quot;,
    <b>new StringValue(&quot;the value for myParam&quot;)</b>);
xsltProcessContext(myContext, myXsl, myRootElement);
</pre>
<h4>The Trick</h4>
<p>So the big trick, which I figured out after reading lots of the AJAXSLT code, was to set the parameter to a <tt>StringValue</tt>, rather than a regular Javascript String. <tt>StringValue</tt> is part of AJAXSLT. <b>Update:</b> Will, in the comments below, mentions that there are four total types: <tt>StringValue</tt>, <tt>NumberValue</tt>, <tt>BooleanValue</tt> and <tt>NodeSetValue</tt>.</p>
<h4>Troubleshooting</h4>
<p>Don&#8217;t forget to declare your parameter with <code>&lt;xsl:param.../&gt;</code> before using it with <code>&lt;xsl:value-of select=&quot;$...&quot;/&gt;</code>, otherwise Ajaxslt will blow up in the middle of rendering.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2005/10/23/setting-xsl-parameters-from-javascript-in-ajaxslt/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Mac OS X Web Browser for JavaScript Testing</title>
		<link>http://www.eahanson.com/2005/10/02/writing-a-mac-os-x-web-browser-for-javascript-testing/</link>
		<comments>http://www.eahanson.com/2005/10/02/writing-a-mac-os-x-web-browser-for-javascript-testing/#comments</comments>
		<pubDate>Sun, 02 Oct 2005 21:34:22 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[My Software]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=5</guid>
		<description><![CDATA[I was playing with a very JavaScript-heavy web page and wanted my automated tests to use a real web browser. I tried running them inside Safari, but it's a bit annoying to have my tests take control of my browser. Plus, there were caching issues. So I wrote a very simple web browser.]]></description>
			<content:encoded><![CDATA[<p>I was playing with a very JavaScript-heavy web page and wanted my automated tests to use a real web browser. I tried running them inside Safari, but it&#8217;s a bit annoying to have my tests take control of my browser. Plus, there were caching issues. So I wrote a very simple web browser with the following features:</p>
<ul>
<li>It uses WebKit so it works just like Safari</li>
<li>It doesn&#8217;t do any caching</li>
<li>It takes a URL command-line parameter and loads it</li>
<li>It quits immediately <b>[update: I added a noquit paramter in case you don't want it to quit immediately]</b></li>
</ul>
<p>It currently has the following problems:</p>
<ul>
<li>It&#8217;s uses AppKit, so it has a UI. I really wanted it to be a command-line-only app, but I haven&#8217;t yet figured out how to do that</li>
<li>Invoking it is painful: <tt>/Applications/wkget.app/Contents/MacOS/wkget -url http://www.apple.com</tt></li>
<li>It only tests WebKit; one day I might make a version that uses Firefox&#8217;s JavaScript engine</li>
</ul>
<p>I called it <b>wkget</b> and you can <a href="/sw/wkget.zip">download it here</a>. [<b>Update:</b> I had an incorrectly-built version up here for a while. It's fixed now.]</p>
<p>Here&#8217;s the code (sorry for the formatting; I had to wrap everything for this very narrow column):</p>
<pre>
@implementation WkgetController
- (void)awakeFromNib {
  [self gotoUrl:self];
  [webView setFrameLoadDelegate:self];
}

- (IBAction)gotoUrl:(id)sender {
  NSString *url = [[NSUserDefaults standardUserDefaults]
      stringForKey:@&#8221;url&#8221;];

  if (url == nil) {
    url = @&#8221;http://www.apple.com/&#8221;;
  }

  NSURLRequest *request = [NSURLRequest
      requestWithURL:[NSURL URLWithString:url]
      cachePolicy:NSURLRequestReloadIgnoringCacheData
      timeoutInterval:1.0];

  [[webView mainFrame] loadRequest:request];
}

- (void)webView:(WebView *)sender
  didFinishLoadForFrame:(WebFrame *)frame {
  if (![[NSUserDefaults standardUserDefaults]
      boolForKey:@&#8221;noquit&#8221;]) {
    [NSApp terminate:self];
  }
}
@end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2005/10/02/writing-a-mac-os-x-web-browser-for-javascript-testing/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How To Use Google&#8217;s Ajaxslt Library</title>
		<link>http://www.eahanson.com/2005/09/30/how-to-use-googles-ajaxslt-library/</link>
		<comments>http://www.eahanson.com/2005/09/30/how-to-use-googles-ajaxslt-library/#comments</comments>
		<pubDate>Fri, 30 Sep 2005 15:53:48 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
		
		<category><![CDATA[Howto]]></category>

		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[XSL]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/weblog/?p=4</guid>
		<description><![CDATA[Google has written an open-source library called Ajaxslt that implements XPath and XSLT in Javascript. It took a bit of playing to get it to work.]]></description>
			<content:encoded><![CDATA[<p><a href="http://search.yahoo.com/search?p=google&#038;sm=Yahoo%21+Search&#038;fr=FP-tab-web-t&#038;toggle=1&#038;cop=&#038;ei=UTF-8">Google</a> has written an open-source library called <a href="http://goog-ajaxslt.sourceforge.net/">Ajaxslt</a> that implements <a href="http://www.w3.org/TR/1999/REC-xpath-19991116">XPath</a> and <a href="http://www.w3.org/TR/1999/REC-xslt-19991116">XSLT</a> in Javascript.</p>
<p>It took a bit of playing to get it to work. Here&#8217;s some sample code. (I forgot to write it up when I did it, so I might have left something out.)</p>
<h3>Setup</h3>
<p>First, include the scripts. <b>Note:</b> make sure to have a closing <tt>&lt;/script&gt;</tt> tag rather than doing <tt>&lt;script &#8230; /&gt;</tt> because that silently fails on some browsers (yay!).</p>
<pre>
&lt;!-- AJAXSLT --&gt;
&lt;script src=&quot;/ajaxslt/misc.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/ajaxslt/dom.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/ajaxslt/xpath.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/ajaxslt/xslt.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;script src=&quot;/ajaxslt/xpathdebug.js&quot;
    type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
</pre>
<h3>Using Ajaxslt for XSL Transformation</h3>
<p>For XSL transformation, you&#8217;ll need three things: some XML, some XSL, and a node (or DocumentFragment presumably) to place the result in. One way to get the XML and XSL is to load them through an XmlHttpRequest. Ajaxslt also provides a couple of handy functions to convert between text and XML:</p>
<pre>
var myText = &quot;...&quot;;
var myXml = xmlParse(myText);
var myTextAgain = xmlText(myXml);
</pre>
<p>Once you&#8217;ve got the XML and XSL, you&#8217;re ready to do the transformation:</p>
<pre>
var myNode = document.getElementById(&quot;myId&quot;);
xsltProcessContext(new ExprContext(myXml), myXsl, myNode);
</pre>
<p>Your node should now contain the result of the transformation.</p>
<h3>Using Ajaxslt for XPath</h3>
<p>You can also use Ajaxslt for XPath evaluation, which is really handy when writing tests on your rendered document:</p>
<pre>
var expr = xpathParse(myXpathString);
var result = expr.evaluate(new ExprContext(myXmlDocument));
var resultAsString = result.stringValue();
</pre>
<h3>Troublshooting</h3>
<p><b>The order of the JavaScript include statements matters:</b> if Ajaxslt is giving strange errors, make sure the scripts are included in the exact same order as  in the Setup section of this page.</p>
<p><b>Make sure you know what you&#8217;re passing to Ajaxslt:</b> if you&#8217;re sending <tt>request.responseXML</tt> to a method, don&#8217;t look at <tt>request.responseText</tt> to see what you&#8217;re passing; use <tt>xmlText(request.responseXML)</tt>.</p>
<p><b>The root element isn&#8217;t selectable:</b> if your XML is <tt>&lt;foo id=&quot;myId&quot;&gt;&#8230;&lt;/foo&gt;</tt>, then  you don&#8217;t select foo&#8217;s id with <tt>/foo/@id</tt> but rather with just <tt>@id</tt>.</p>
<p><b>Case matters in xpath:</b> If you&#8217;re evaluating an xpath expression on an HTML DOM, note that the node names are most likely uppercase. So do <tt>/HTML/BODY</tt> instead of <tt>/html/body</tt></p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2005/09/30/how-to-use-googles-ajaxslt-library/feed/</wfw:commentRss>
		</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>
		</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 &#8212; &#8211;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>
		</item>
	</channel>
</rss>
