<?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>redemption in a blog &#187; Web development</title>
	<atom:link href="http://blog.codefront.net/category/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codefront.net</link>
	<description>Rails, Firefox, Anime, Mac</description>
	<lastBuildDate>Tue, 24 Jan 2012 01:47:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Firefox quick tip: view images in a new tab quickly</title>
		<link>http://blog.codefront.net/2009/11/25/firefox-quick-tip-view-images-in-a-new-tab-quickly/</link>
		<comments>http://blog.codefront.net/2009/11/25/firefox-quick-tip-view-images-in-a-new-tab-quickly/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 11:13:21 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[Mozilla]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/?p=1183</guid>
		<description><![CDATA[A mini-tip on viewing images quickly in a new tab in Firefox. Useful for web devs who want to look at the URL of an image quickly. As an example, here&#8217;s the Youtube webpage of the owner of Maru the Cat: To open the thumbnail image of Maru in a new tab, right-click the image, [...]]]></description>
			<content:encoded><![CDATA[<p>A mini-tip on viewing images quickly in a new tab in Firefox. Useful for web devs who want to look at the URL of an image quickly.</p>
<p>As an example, here&#8217;s the <a href="http://www.youtube.com/user/mugumogu">Youtube webpage</a> of the owner of <a href="http://en.wikipedia.org/wiki/Maru_the_Cat">Maru the Cat</a>:</p>
<p><img src="http://blog.codefront.net/wp-content/uploads/2009/06/ff3-middle-click-step-1.png" alt="ff3-middle-click-step-1" title="ff3-middle-click-step-1" width="320" height="148" class="alignnone size-full wp-image-1186" /></p>
<p>To open the thumbnail image of Maru in a new tab, right-click the image, and then <strong>middle-click</strong> the <strong>View Image</strong> item from the context menu:</p>
<p><img src="http://blog.codefront.net/wp-content/uploads/2009/06/ff3-middle-click-step-2.png" alt="ff3-middle-click-step-2" title="ff3-middle-click-step-2" width="416" height="222" class="alignnone size-full wp-image-1185" /></p>
<p>It should open up in a new tab:</p>
<p><img src="http://blog.codefront.net/wp-content/uploads/2009/06/ff3-middle-click-step-3.png" alt="ff3-middle-click-step-3" title="ff3-middle-click-step-3" width="507" height="226" class="alignnone size-full wp-image-1184" /></p>
<p>You can also do the same for background images (middle-click <strong>View Background Image</strong> from the right-click context menu).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2009/11/25/firefox-quick-tip-view-images-in-a-new-tab-quickly/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>New in Rails: ActiveResource timeouts and why it matters</title>
		<link>http://blog.codefront.net/2008/04/22/new-in-rails-activeresource-timeouts-and-why-it-matters/</link>
		<comments>http://blog.codefront.net/2008/04/22/new-in-rails-activeresource-timeouts-and-why-it-matters/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 14:25:57 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/?p=1100</guid>
		<description><![CDATA[If you&#8217;ve ever used ActiveResource or for that matter, coded anything that involves interacting with a remote API, you&#8217;ve probably had the misfortune of having that API go down or become unresponsive. The experienced old geezers would already have forseen the problem and set a timeout on those API calls. For the rest of us, [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever used ActiveResource or for that matter, coded anything that involves interacting with a remote API, you&#8217;ve probably had the misfortune of having that API go down or become unresponsive. The experienced old geezers would already have forseen the problem and set a timeout on those API calls. For the rest of us, it&#8217;s a good time to get acquainted with the idea of <a href="http://en.wikipedia.org/wiki/Fail-fast">fail-fast</a> and how <em>no response is better than a slow response</em>.</p>
<p>So what happens when the API you&#8217;re accessing via ActiveResource or <code>Net::HTTP</code> (or whatever, really, so long as there&#8217;s a network socket involved!) becomes unresponsive?</p>
<h3>Unfettered outbound remote connections can kill your server</h3>
<p>ActiveResource (or <code>Net::HTTP</code> or whatever you&#8217;re using) would block while waiting for the API to return. What did we learn about blocking IO in Operating Systems 101? That your process or thread gets stuck waiting for a response that&#8217;s what. So now your Mongrel/Thin/Ebb/X web server process basically stuck waiting for a response from an API that might return a response in 30 seconds, or maybe it&#8217;ll never return at all. It won&#8217;t be long before all your web server processes get blocked and its time to &#8220;kill dash nine&#8221; (<code>kill -9</code>) some processes and bring out the 500 error page. Yes, your server has been incapacitated and your website is now basically offline.</p>
<div class="img"><img src="http://blog.codefront.net/wp-content/uploads/2008/04/im-in-ur-serverz.gif" alt="im in ur serverz making thingz better" /></div>
<p><br style="clear:both;" /></p>
<h3>Timeouts and you</h3>
<p>If you&#8217;re using Net::HTTP you can easily safeguard yourself against errors like this by setting a timeout on the socket connection. Like so:</p>
<pre><code class="ruby">def fetch_something_from_some_api(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  http.read_timeout = timeout

  response = http.start { http.request(Net::HTTP::Get.new("#{uri.path}?#{uri.query}")) }
  response.is_a?(Net::HTTPSuccess) ? do_something_with(response.body) : nil
rescue Timeout::Error
  logger.warn("Timeout accessing #{uri}: #{$!}")
  nil
rescue
  logger.warn("Error accessing #{uri}: #{$!}")
  nil
end</code></pre>
<p>Take note that you really do need to explicitly rescue from the <code>Timeout::Error</code> (or <code>Interrupt</code>) since it&#8217;s a child of the <code>Interrupt</code> class and not a <code>StandardError</code>.</p>
<p>Any good client library should also allow you to specify a timeout on the socket connection so read the API documentation.</p>
<h3>So what&#8217;s this about ActiveResource?</h3>
<p>So back to ActiveResource: Michael Koziarski was kind enough to commit my 2 patches (<a href="http://github.com/rails/rails/commit/105910429d5873dce677ef32eef5f705e0625d86">this</a> and <a href="http://github.com/rails/rails/commit/cf32baf915442ffe153ec0e4d8148f147776c30a">this</a>) to ActiveResource so now on <a href="http://github.com/rails/rails/tree/master">edge Rails</a>, you can do this:</p>
<pre><code class="ruby">class Person &lt; ActiveResource::Base
  self.site = 'http://api.people.com:3000/'
  self.timeout = 5
end</code></pre>
<p>This sets the timeout to 5 seconds on the underlying <code>Net::HTTP</code> instance that ActiveResource uses. The default timeout (to be exact, the <code>read_timeout</code>) for <code>Net::HTTP</code> is <strong>60 seconds</strong> on most Ruby implementations (that includes MRI). That&#8217;s far too long if your ActiveResource call is in a public-facing controller!</p>
<p>If you are using ActiveResource anywhere and haven&#8217;t safeguarded your application with a timeout, I think it&#8217;s a good idea to fix it by using edge Rails or at least applying the <a href="http://github.com/rails/rails/commit/105910429d5873dce677ef32eef5f705e0625d86">patch</a>!</p>
<p>The <a href="http://github.com/rails/rails/commit/cf32baf915442ffe153ec0e4d8148f147776c30a">2nd ActiveResource patch</a> introduces the <code>ActiveResource::TimeoutError</code> exception that you should rescue from whenever you make an ActiveResource call. When this happens, it&#8217;s up to your application to decide what to do (like show a placating we&#8217;re sorry message). At least your application is still able to do something!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2008/04/22/new-in-rails-activeresource-timeouts-and-why-it-matters/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Generating migrations gets easier &#8211; Rails 2.0 a feature a day #6</title>
		<link>http://blog.codefront.net/2007/12/24/generating-migrations-gets-easier-rails-20-a-feature-a-day-6/</link>
		<comments>http://blog.codefront.net/2007/12/24/generating-migrations-gets-easier-rails-20-a-feature-a-day-6/#comments</comments>
		<pubDate>Mon, 24 Dec 2007 08:58:07 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/12/24/generating-migrations-gets-easier-rails-20-a-feature-a-day-6/</guid>
		<description><![CDATA[You may not have noticed this yet, but Rails 2.0 has a new convenient syntax for generating ActiveRecord migrations. Go ahead, run the migration generator with script/generate migration. I&#8217;ll wait. Yup, you can now specify the columns you want to add in your migration by passing attribute/type pairs to the migration generator. Ergo, script/generate migration [...]]]></description>
			<content:encoded><![CDATA[<p>You may not have noticed this yet, but Rails 2.0 has a new convenient syntax for generating ActiveRecord migrations. Go ahead, run the migration generator with <code>script/generate migration</code>. I&#8217;ll wait.</p>
<p>Yup, you can now specify the columns you want to add in your migration by passing attribute/type pairs to the migration generator. Ergo,</p>
<pre><code>script/generate migration AddMoreToAnime episodes:integer licensed:boolean</code></pre>
<p>will generate a migration like so:</p>
<pre><code class="ruby">class AddMoreToAnime &lt; ActiveRecord::Migration
  def self.up
    add_column :animes, :episodes, :integer
    add_column :animes, :licensed, :boolean
  end

  def self.down
    remove_column :animes, :licensed
    remove_column :animes, :episodes
  end
end</code></pre>
<p>This ties in pretty nicely with the syntax for the model generator (<code>script/generate model</code>), which also accepts attribute pairs. Tiny change but &#8220;it&#8217;s all about consistency&#8221;.</p>
<p>For the geeks among you, check out the <a href="http://dev.rubyonrails.org/changeset/7422">related changeset</a> and <a href="http://dev.rubyonrails.org/ticket/9166">ticket</a>.</p>
<h3>About the contributor, Pratik Naik</h3>
<p><a href="http://m.onkey.org/">Pratik Naik</a> (<a href="http://workingwithrails.com/recommendation/new/person/5424-pratik-naik">WorkingWithRails profile</a>), probably better known as lifofifo or just lifo, is an active Rails contributor and can be found on <a href="irc://irc.freenode.net/rails-contrib">#rails-contrib</a> and <a href="irc://irc.freenode.net/rubyonrails">#rubyonrails</a> nearly 24/7. Almost everyone who&#8217;s used Rails has used code that lifo has written. Oh, and lifo also <a href="http://weblog.workingwithrails.com/2007/11/1/hackfest-winner-interview-pratik-naik">won one of the monthly Rails Hackfests</a>.</p>
<p>He also keeps a <a href="http://m.onkey.org/">pretty good blog</a> where he posts mostly on Rails hacking and tips.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/12/24/generating-migrations-gets-easier-rails-20-a-feature-a-day-6/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Concatenate your stylesheets and JavaScripts in 3 seconds &#8211; Rails 2.0 a feature a day #3</title>
		<link>http://blog.codefront.net/2007/12/12/concatenate-your-stylesheets-and-javascripts-in-3-seconds-rails-20-a-feature-a-day-3/</link>
		<comments>http://blog.codefront.net/2007/12/12/concatenate-your-stylesheets-and-javascripts-in-3-seconds-rails-20-a-feature-a-day-3/#comments</comments>
		<pubDate>Wed, 12 Dec 2007 15:25:39 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/12/12/concatenate-your-stylesheets-and-javascripts-in-3-seconds-rails-20-a-feature-a-day-3/</guid>
		<description><![CDATA[I don&#8217;t think I need to explain how concatenating your 5 stylesheets and 12 JavaScript files into single files each makes your webpages load faster. Unless you&#8217;re using multiple asset hosts (Rails 2 allows for multiple asset hosts), then it becomes tricky to say for sure which method is better, but I digress. There&#8217;re a [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t think I need to explain how <a href="http://developer.yahoo.com/performance/rules.html#num_http">concatenating your 5 stylesheets and 12 JavaScript files into single files</a> each makes your webpages load faster. Unless you&#8217;re using multiple asset hosts (Rails 2 allows for <a href="http://dev.rubyonrails.org/changeset/6161">multiple asset hosts</a>), then it becomes tricky to say for sure which method is better, but I digress.</p>
<p>There&#8217;re a bunch of really useful plugins that help you bundle your JS and CSS files, most of them offering minifying/packing of JS and stripping of comments and whitespace in CSS as well. I was a big fan (still love it) of <a href="http://synthesis.sbecker.net/pages/asset_packager">Asset Packager</a>, but newer plugins like <a href="http://blog.jcoglan.com/packr/">PackR</a> and <a href="http://code.google.com/p/bundle-fu/">Bundle-fu</a> work great too.</p>
<p>With Rails 2.0 though, unless you want the minifying/packing/comment-stripping features, you can do away with installing a packager plugin. Remember your trusty <code>javascript_include_tag</code> and <code>stylesheet_link_tag</code>? They now accept a <code>:cache</code> option:</p>
<pre><code class="ruby"># :cache =&gt; true creates a cached javascript named all.js.
&lt;%= javascript_include_tag :all, :cache =&gt; true %&gt;
# This becomes:
# &lt;script type="text/javascript" src="/javascripts/all.js"&gt;&lt;/script&gt;

# You can name your javascript too, by passing a name instead of true.
&lt;%= javascript_include_tag 'jquery', 'swfobject', 'application', :cache =&gt; 'base' %&gt;
# This becomes:
# &lt;script type="text/javascript" src="/javascripts/base.js"&gt;&lt;/script&gt;

# Same deal for stylesheets...
# :cache =&gt; true creates a stylesheet named all.css.
&lt;%= stylesheet_link_tag :all, :cache =&gt; true %&gt;
# This becomes:
# &lt;link href="/stylesheets/all.css"  media="screen" rel="Stylesheet" type="text/css" /&gt;

# You can name your stylesheet like for javascript_include_tag.
&lt;%= stylesheet_link_tag 'yui/reset-fonts-grids', 'application', :cache =&gt; 'styles' %&gt;
# This becomes:
# &lt;link href="/stylesheets/styles.css"  media="screen" rel="Stylesheet" type="text/css" /&gt;
</code></pre>
<p>Of course, this only takes place if caching is turned on (<code>ActionController::Base.perform_caching</code>), which is on by default in the production environment and off in the development environment.</p>
<p>For those who want to look at some code, check out <a href="http://dev.rubyonrails.org/changeset/6164">the changeset</a>.</p>
<h3>About the contributor, DHH</h3>
<p>Well, <a href="http://loudthinking.com/">DHH</a> hardly needs any mention. David has_one <a href="http://en.wikipedia.org/wiki/David_Heinemeier_Hansson">Wikipedia entry</a>. Nice little touch on this new feature though, I love the convenience.</p>
<p>I promise, like <a href="http://blog.codefront.net/2007/12/09/rails-20-a-feature-a-day/">I&#8217;d said before</a>, to cover contributions by someone less &#8220;mainstream&#8221; in the next <a href="http://blog.codefront.net/2007/12/09/rails-20-a-feature-a-day/">Rails 2 &#8220;feature a day&#8221;</a> blog post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/12/12/concatenate-your-stylesheets-and-javascripts-in-3-seconds-rails-20-a-feature-a-day-3/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Firefox 3, Beta</title>
		<link>http://blog.codefront.net/2007/11/20/firefox-3-beta/</link>
		<comments>http://blog.codefront.net/2007/11/20/firefox-3-beta/#comments</comments>
		<pubDate>Tue, 20 Nov 2007 09:23:57 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Mozilla]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/11/20/firefox-3-beta/</guid>
		<description><![CDATA[Firefox 3 Beta 1 is out! Check out the pretty sweet list of changes. If you want to try it, you&#8217;ll probably want to run Firefox 3 and Firefox 2 at the same time. Only this time you should remember to rename Firefox 3 so that it doesn&#8217;t override your install of Firefox 2. I&#8217;d [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mozilla.com/en-US/firefox/3.0b1/releasenotes/">Firefox 3 Beta 1</a> is out! Check out the pretty sweet <a href="http://www.mozilla.com/en-US/firefox/3.0b1/releasenotes/#whatsnew">list of changes</a>.</p>
<div class="img"><img src='http://blog.codefront.net/wp-content/uploads/2007/11/firefox-3.png' alt='' /></div>
<p><br style="clear: both;" /></p>
<p>If you want to try it, you&#8217;ll probably want to <a href="http://blog.codefront.net/2007/08/20/how-to-have-firefox-3-and-firefox-2-running-at-the-same-time/">run Firefox 3 and Firefox 2 at the same time</a>. Only this time you should remember to rename Firefox 3 so that it doesn&#8217;t override your install of Firefox 2.</p>
<p>I&#8217;d blogged about some of the changes previously if you care for some screenshots of <em>some</em> of the new features (Mozilla Links <a href="http://mozillalinks.org/wp/category/subject/firefox3/">covers a good number of new Firefox 3 changes</a> too):</p>
<ul>
<li><a href="http://blog.codefront.net/2007/09/18/whats-new-in-firefox-3-download-resume/">Resumable downloading: users can now resume downloads after restarting the browser or resetting your network connection.</a></li>
<li><a href="http://blog.codefront.net/2007/05/05/growl-integration-in-firefox-3/">supports Growl for notifications of completed downloads and available updates.</a></li>
<li><a href="http://blog.codefront.net/2007/09/07/smarter-and-less-annoying-password-manager-in-firefox-3/">Easier password management: an information bar replaces the old password dialog so you can now save passwords after a successful login.</a></li>
<li><a href="http://blog.codefront.net/2007/09/13/open-in-tabs-fixed-in-firefox-3-at-last/">Optimized Open in Tabs behavior: opening a folder of bookmarks in tabs now appends the new tabs rather than overwriting.</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/11/20/firefox-3-beta/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>New in Rails: a request profiler for profiling your app</title>
		<link>http://blog.codefront.net/2007/10/26/new-in-rails-a-request-profiler-for-profiling-your-app/</link>
		<comments>http://blog.codefront.net/2007/10/26/new-in-rails-a-request-profiler-for-profiling-your-app/#comments</comments>
		<pubDate>Fri, 26 Oct 2007 02:01:30 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/10/26/new-in-rails-a-request-profiler-for-profiling-your-app/</guid>
		<description><![CDATA[Jeremy Kemper (aka bitsweat) committed a very useful tool into the Rails trunk not too long ago: a request profiler! It&#8217;s a human-friendly wrapper script around the ruby-prof library, a nice ruby code profiler, that lets you run multiple requests against a URI in your application and get a detailed code profile report in text [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://bitsweat.net/">Jeremy Kemper</a> (aka bitsweat) <a href="http://dev.rubyonrails.org/changeset/8016">committed</a> a very useful tool into the Rails trunk not too long ago: a <a href="http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_controller/request_profiler.rb">request profiler</a>! It&#8217;s a human-friendly wrapper script around the <a href="http://ruby-prof.rubyforge.org/">ruby-prof library</a>, a nice ruby code profiler, that lets you run multiple requests against a URI in your application and get a detailed code profile report in text and HTML.</p>
<p>Wanna try it out? Update your vendor/rails to at least revision 8016, run <code>rake rails:update</code> to copy the new <code>script/performance/request</code> script into your app&#8217;s <code>script</code> directory, install the ruby-prof gem (<code>sudo gem install ruby-prof</code>), then let it loose on your app.</p>
<p>The options are pretty simple, just pass the script the URI you wanna profile. There&#8217;re options for specifying how many requests you want to process, and for profiling POST requests, there&#8217;s one for<br />
specifying a POST fixture file.</p>
<pre><code>USAGE: script/performance/request uri [options]
    -u, --uri [URI]                  Request URI. Defaults to http://localhost:3000/
    -n, --times [0000]               How many requests to process. Defaults to 1000.
    -b, --benchmark                  Benchmark instead of profiling
        --method [GET]               HTTP request method. Defaults to GET.
        --fixture [FILE]             Path to POST fixture file
        --open [CMD]                 Command to open profile results. Defaults to "open %s &#038;"
    -h, --help                       Show this help</code></pre>
<p>At the end of the run, you get a text and HTML report with the methods called and the time spent in them. If you haven&#8217;t seen a code profile before, it looks something like this:</p>
<div class="img"><img src='http://blog.codefront.net/wp-content/uploads/2007/10/rails-request-prof-text-output_thumb.png' alt='Rails request profiler text output report' /></div>
<p><br style="clear: both;" /></p>
<p>Very nice stuff! It&#8217;s <em>extremely</em> convenient to have profiling like this built into Rails itself &#8211; personally I&#8217;ve not even run any profiling on my Rails code because it seemed like a hassle (though I do run httperf benchmarks and take note the number of requests per second). With code profiling, you can easily see which parts of your code are the bottlenecks and optimize away &#8211; it&#8217;s a godsend that it&#8217;s so easy to do it in Rails!</p>
<p>Anyway, this is all still new stuff I imagine and subject to change (and improvements), but still very exciting for those of us who are hitting performance bottlenecks in our Rails apps (and are not already doing code profiling).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/10/26/new-in-rails-a-request-profiler-for-profiling-your-app/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>New in Edge Rails: helper for creating form labels</title>
		<link>http://blog.codefront.net/2007/09/25/new-in-edge-rails-helper-for-creating-form-labels/</link>
		<comments>http://blog.codefront.net/2007/09/25/new-in-edge-rails-helper-for-creating-form-labels/#comments</comments>
		<pubDate>Tue, 25 Sep 2007 02:40:55 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/09/25/new-in-edge-rails-helper-for-creating-form-labels/</guid>
		<description><![CDATA[Edge Rails now comes with a helper for creating form labels, so in your views, instead of doing this: &#60;% form_for(:user, @user, :url => user_path(@user)) do &#124;f&#124; -%&#62; &#60;label for="user_login"&#62;Login&#60;/label&#62; &#60;%= f.text_field :login %&#62; &#60;end&#62; where the &#60;label&#62; is given a for attribute of user_login (since the convention for form input ids is {MODEL_NAME}_{ATTRIBUTE_NAME}), you [...]]]></description>
			<content:encoded><![CDATA[<p>Edge Rails now comes with a <a href="http://dev.rubyonrails.org/changeset/7541">helper for creating form labels</a>, so in your views, instead of doing this:</p>
<pre><code class="ruby">&lt;% form_for(:user, @user, :url => user_path(@user)) do |f| -%&gt;
<strong>&lt;label for="user_login"&gt;Login&lt;/label&gt;</strong>
&lt;%= f.text_field :login %&gt;
&lt;end&gt;</code></pre>
<p>where the <code>&lt;label&gt;</code> is given a <code>for</code> attribute of <code>user_login</code> (since the convention for form input ids is <code>{MODEL_NAME}_{ATTRIBUTE_NAME}</code>), you can instead now do:</p>
<pre><code class="ruby">&lt;%= label(:user, :login) %&gt;</code></pre>
<p>Quite a bit neater. Makes it easier to explain to beginners how they should use <code>&lt;label&gt;</code>s appropriately since the syntax is similar to the other form helpers. Now give your <code>&lt;label&gt;</code>s proper <code>for</code> attributes if you&#8217;re not doing so already.</p>
<p>This makes plugins like this <a href="http://simple-rails-plugins.googlecode.com/svn/form-label-helper/">form label helper plugin</a> obsolete.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/09/25/new-in-edge-rails-helper-for-creating-form-labels/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dugg? No problem with WordPress</title>
		<link>http://blog.codefront.net/2007/09/13/dugg-no-problem-with-wordpress/</link>
		<comments>http://blog.codefront.net/2007/09/13/dugg-no-problem-with-wordpress/#comments</comments>
		<pubDate>Thu, 13 Sep 2007 10:53:25 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/09/13/dugg-no-problem-with-wordpress/</guid>
		<description><![CDATA[An old post I wrote on a spoof MMORPG named &#8220;Outside&#8221; was Dugg not too long ago and I was quite pleased to find that my server and the blogging software (WordPress) that I use was handling the load extremely well. Quite obviously I was getting more hits in a day than entire months: My [...]]]></description>
			<content:encoded><![CDATA[<p>An <a href="/2007/08/13/mmorpg-called-outside/">old post I wrote on a spoof <acronym title="Massively Multiplayer Online Role-Playing Game">MMORPG</acronym> named &#8220;Outside&#8221;</a> was <a href="http://digg.com/offbeat_news/Incredible_MMORPG_called_Outside_PIC">Dugg</a> not too long ago and I was quite pleased to find that my server and the blogging software (<a href="http://wordpress.org/">WordPress</a>) that I use was handling the load extremely well. Quite obviously I was getting more hits in a day than entire months:</p>
<div class="img"><img src='http://blog.codefront.net/wp-content/uploads/2007/09/blog-stats-after-getting-dugg.png' alt='Blog stats after getting Dugg' /></div>
<p><br style="clear: both;" /></p>
<p>My setup is a Virtual Private Server (VPS) with 256MB RAM hosted at <a href="http://www.slicehost.com/">SliceHost</a> and this blog is served off <a href="http://nginx.net/">Nginx</a> and PHP FastCGI processes to handle PHP scripts. The wonderful (because it just works and is really easy to setup) <a href="http://mnm.uib.es/gallir/wp-cache-2/">WP-Cache WordPress plugin</a> keeps a cache of pages that&#8217;s swept at logical times (i.e. whenever there are any updates or comments).</p>
<p>I may be a Rails/merb fanboy, but this awesome piece of blog software that can stand up to the <a href="http://en.wikipedia.org/wiki/Digg_effect">Digg Effect</a> with ease is <strong>great</strong>. WordPress <code>FTW</code>!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/09/13/dugg-no-problem-with-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t trust web apps (or how Google Reader and my ISP conspired to lose all my feeds)</title>
		<link>http://blog.codefront.net/2007/08/17/dont-trust-web-apps-or-how-google-reader-and-my-isp-conspired-to-lose-all-my-feeds/</link>
		<comments>http://blog.codefront.net/2007/08/17/dont-trust-web-apps-or-how-google-reader-and-my-isp-conspired-to-lose-all-my-feeds/#comments</comments>
		<pubDate>Fri, 17 Aug 2007 10:33:11 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/08/17/dont-trust-web-apps-or-how-google-reader-and-my-isp-conspired-to-lose-all-my-feeds/</guid>
		<description><![CDATA[About a month ago, I logged in to Google Reader to see a shocking thing &#8211; all my subscriptions were gone and I was subscribed to some feeds that I&#8217;ve never heard of. I would say I&#8217;m a pretty heavy Google Reader user, checking my several hundred subscriptions several times a day, so it was [...]]]></description>
			<content:encoded><![CDATA[<p>About a month ago, I logged in to Google Reader to see a shocking thing &#8211; all my subscriptions were gone and I was subscribed to some feeds that I&#8217;ve never heard of.</p>
<div class="img"><img src='http://blog.codefront.net/wp-content/uploads/2007/07/greader-subs-all-gone.png' alt='All my feed subscriptions gone from Google Reader' /></div>
<p><br style="clear: both;" /></p>
<p>I would say I&#8217;m a pretty heavy Google Reader user, checking my several hundred subscriptions several times a day, so it was pretty distressing. I posted a <a href="http://groups.google.com/group/Google-Labs-Reader/t/2e9436dd16c5f6e2">thread at the Google Reader Google Group</a> (the link doesn&#8217;t work now because they&#8217;ve moved the Google Group somewhere else for some reason) and several other people responded that they were seeing the same weirdness.</p>
<blockquote><p>I had the same problem, and here&#8217;s the really weird part: I got YOUR feeds! If you look at the top of the page you can see the account you&#8217;re supposedly logged in with. Well, I saw your account there. And this was before I read this message. I also saw some other names every so often. Right now it says <em>email censored</em>.</p>
<p>Maybe it&#8217;s some weird mix up with the ISP. Do you use SingNet?</p></blockquote>
<p>Some time later I started seeing other people&#8217;s accounts:</p>
<div class="img"><img src='http://blog.codefront.net/wp-content/uploads/2007/07/greader-other-accounts.png' alt='Seeing other people’s accounts in Google Reader' /></div>
<p><br style="clear: both;" /></p>
<p>The problem affected only <a href="http://www.singnet.com.sg/">Singnet</a> (a major local ISP) users in Singapore (about 7 other users in Singapore using Singnet responded with the same problems). No one really knew what the problem was and neither the Google Reader team nor Singnet (someone sent them an email) responded.</p>
<p>My guess was Singnet started caching Google Reader at a proxy and somehow managed to bypass all the credentials that was needed to modify any feed subscriptions. Sounds like a pretty nasty security bug to me.</p>
<p>The aftermath:</p>
<ul>
<li>Google Reader no longer has my feed subscriptions (I didn&#8217;t manage to recover them so it&#8217;s still in the sad state of having only 1 feed subscription to some UK property shite).</li>
<li>I&#8217;ve manually rebuilt my OPML file (containing my feed subscriptions).</li>
<li>I&#8217;m now using <a href="http://www.newsfirerss.com/">NewsFire</a>. It&#8217;s not decentralized so I can&#8217;t sync it between machines, which really sucks since I have 2 primary machines, but I&#8217;m wary of using online feed readers right now.</li>
<li>I regularly export my OPML file and check it into Subversion.</li>
<li>I&#8217;m looking for a reliable decentralized feed reader once again (having used <a href="http://bloglines.com/">Bloglines</a> and of course, <a href="http://blog.codefront.net/2006/10/24/google-reader-a-bloglines-users-perspective/">Google Reader</a> prior to the catastrophe).</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/08/17/dont-trust-web-apps-or-how-google-reader-and-my-isp-conspired-to-lose-all-my-feeds/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Adium themes use plain old XHTML, CSS and JS</title>
		<link>http://blog.codefront.net/2007/07/28/adium-themes-use-plain-old-xhtml-css-and-js/</link>
		<comments>http://blog.codefront.net/2007/07/28/adium-themes-use-plain-old-xhtml-css-and-js/#comments</comments>
		<pubDate>Sat, 28 Jul 2007 06:07:18 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[Asides]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/07/28/adium-themes-use-plain-old-xhtml-css-and-js/</guid>
		<description><![CDATA[Just a quick note of something I found out while reading Transcending CSS: all the themes (message styles, contact list styles, etc.) in Adium are crafted with our old friends XHTML, CSS and JS. I opened up a few AdiumXtras to see for myself and true enough, &#60;div&#62;s and CSS rules make up the style. [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick note of something I found out while reading <a href="http://www.transcendingcss.com/">Transcending CSS</a>: all the <a href="http://adiumxtras.com/">themes</a> (message styles, contact list styles, etc.) in <a href="http://adiumx.com/">Adium</a> are crafted with our old friends XHTML, CSS and JS. I opened up a few <a href="http://adiumxtras.com/">AdiumXtras</a> to see for myself and true enough, <code>&lt;div&gt;</code>s and CSS rules make up the style. Very smart decision by the Adium developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/07/28/adium-themes-use-plain-old-xhtml-css-and-js/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Some of the world&#8217;s hottest startups &#8211; Bezurk on Business 2.0</title>
		<link>http://blog.codefront.net/2007/07/27/some-of-the-worlds-hottest-startups-bezurk-on-business-20/</link>
		<comments>http://blog.codefront.net/2007/07/27/some-of-the-worlds-hottest-startups-bezurk-on-business-20/#comments</comments>
		<pubDate>Fri, 27 Jul 2007 10:15:18 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/07/27/some-of-the-worlds-hottest-startups-bezurk-on-business-20/</guid>
		<description><![CDATA[Business 2.0 recently published an article on the world&#8217;s hottest startups outside the US and Bezurk was picked. Nice to know that ;)]]></description>
			<content:encoded><![CDATA[<p><a href="http://money.cnn.com/magazines/business2/index.html">Business 2.0</a> recently published an <a href="http://money.cnn.com/galleries/2007/biz2/0707/gallery.web_world.biz2/index.html">article on the world&#8217;s hottest startups outside the US</a> and <a href="http://money.cnn.com/galleries/2007/biz2/0707/gallery.web_world.biz2/8.html">Bezurk was picked</a>. Nice to know that ;)</p>
<div class="img"><a href="http://money.cnn.com/galleries/2007/biz2/0707/gallery.web_world.biz2/8.html"><img src='http://blog.codefront.net/wp-content/uploads/2007/07/bezurk-on-business-20.png' alt='Bezurk on Business 2.0' /></a></div>
<p><br style="clear: both;" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/07/27/some-of-the-worlds-hottest-startups-bezurk-on-business-20/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>YSlow &#8211; Firefox extension that helps to optimize your web pages</title>
		<link>http://blog.codefront.net/2007/07/26/yslow-firefox-extension-that-helps-to-optimize-your-web-pages/</link>
		<comments>http://blog.codefront.net/2007/07/26/yslow-firefox-extension-that-helps-to-optimize-your-web-pages/#comments</comments>
		<pubDate>Thu, 26 Jul 2007 05:24:51 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Asides]]></category>
		<category><![CDATA[Neat Stuff]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/07/26/yslow-firefox-extension-that-helps-to-optimize-your-web-pages/</guid>
		<description><![CDATA[YSlow is a Firefox extension that works with Firebug to grade your web pages&#8217; performance and gives you advice on how to fix any problems. Awesome. It has JSLint built into it too. And yes I got a big freaking F. Via Arun (IRL).]]></description>
			<content:encoded><![CDATA[<p><a href="http://developer.yahoo.com/yslow/">YSlow</a> is a Firefox extension that works with <a href="http://getfirebug.com/">Firebug</a> to grade your web pages&#8217; performance and gives you advice on how to fix any problems. Awesome. It has <a href="http://www.jslint.com/">JSLint</a> built into it too. And yes I got a big freaking <strong>F</strong>. Via <a href="http://arunthampi.wordpress.com/">Arun</a> (<acronym title="In Real Life">IRL</acronym>).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/07/26/yslow-firefox-extension-that-helps-to-optimize-your-web-pages/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL command line &#8220;secret&#8221;</title>
		<link>http://blog.codefront.net/2007/07/19/mysql-command-line-secret/</link>
		<comments>http://blog.codefront.net/2007/07/19/mysql-command-line-secret/#comments</comments>
		<pubDate>Thu, 19 Jul 2007 10:02:25 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Neat Stuff]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/07/19/mysql-command-line-secret/</guid>
		<description><![CDATA[Tom Preston-Werner (of Gravatar and Chronic fame) wrote about this neat MySQL command line &#8220;trick&#8221; where instead of ending your SELECT statements with the usual semi-colon, using a &#8220;\G&#8221; gives you a very readable output. mysql> select * from locations where location_code = 'SIN' limit 1\G *************************** 1. row *************************** location_code: SIN location_name: Singapore Changi [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://rubyisawesome.com/">Tom Preston-Werner</a> (of Gravatar and Chronic fame) <a href="http://rubyisawesome.com/2007/7/10/mysql-secrets-g-instead-of">wrote about this neat MySQL command line &#8220;trick&#8221;</a> where instead of ending your SELECT statements with the usual semi-colon, using a &#8220;\G&#8221; gives you a very readable output.</p>
<pre><code>mysql> select * from locations where location_code = 'SIN'  limit 1\G
*************************** 1. row ***************************
          location_code: SIN
          location_name: Singapore Changi Apt
multi_airport_city_code: SIN
          location_type: A
      location_sub_type: A
           country_code: SG
             state_code:
                 active: 1
               latitude: 01.22.00N
              longitude: 103.59.00E
1 row in set (0.10 sec)</code></pre>
<p>Very useful, especially when SELECTing multiple fields where the output becomes unfit for visual consumption (you know what I mean, just go SELECT some stuff in your MySQL command line and you&#8217;ll see if you don&#8217;t).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/07/19/mysql-command-line-secret/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>God and your web server</title>
		<link>http://blog.codefront.net/2007/07/18/god-and-your-web-server/</link>
		<comments>http://blog.codefront.net/2007/07/18/god-and-your-web-server/#comments</comments>
		<pubDate>Wed, 18 Jul 2007 15:43:38 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[Operating Systems]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/07/18/god-and-your-web-server/</guid>
		<description><![CDATA[God is a new process monitoring Ruby framework with a rather apt name (no, it&#8217;s not that God). The config files looks something like that: God.meddle do &#124;god&#124; god.watch do &#124;w&#124; ... end end Interesting code to write. And feels safe too to have God watching over your mongrels. Oh and you can use it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://god.rubyforge.org/">God</a> is a new process monitoring Ruby framework with a rather apt name (no, it&#8217;s not <em>that</em> <a href="http://en.wikipedia.org/wiki/God">God</a>). The config files looks something like that:</p>
<pre><code class="ruby">God.meddle do |god|
  god.watch do |w|
    ...
  end
end</code></pre>
<p>Interesting code to write. And feels safe too to have God watching over your mongrels. Oh and you can use it to clean up stale mongrel PID files without <a href="http://textsnippets.com/posts/show/931">patching mongrel_rails</a>. Right now I use <a href="http://www.tildeslash.com/monit/">monit</a> a lot and it&#8217;s really rather unDRY to have repeated configs for each mongrel on the server (is there a better way of doing this in monit?). God would be able to DRY it up.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/07/18/god-and-your-web-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Better JSON output from Rails with the Jsonifier plugin</title>
		<link>http://blog.codefront.net/2007/07/11/better-json-output-from-rails-with-the-jsonifier-plugin/</link>
		<comments>http://blog.codefront.net/2007/07/11/better-json-output-from-rails-with-the-jsonifier-plugin/#comments</comments>
		<pubDate>Wed, 11 Jul 2007 04:46:38 +0000</pubDate>
		<dc:creator>Chu Yeow</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://blog.codefront.net/2007/07/11/better-json-output-from-rails-with-the-jsonifier-plugin/</guid>
		<description><![CDATA[Update: Edge Rails now does whatever Jsonifier does (and more). Check out my blog post on JSON serialization maturity in edge Rails. This means that this plugin is virtually obsolete if you&#8217;re using edge Rails. If you&#8217;ve tried to output JSON from your Rails applications before, you&#8217;d probably have noticed how inadequate it all seems. [...]]]></description>
			<content:encoded><![CDATA[<p><ins>Update: Edge Rails now does whatever Jsonifier does (and more). Check out my blog <a href="http://blog.codefront.net/2007/10/10/new-on-edge-rails-json-serialization-of-activerecord-objects-reaches-maturity/">post on JSON serialization maturity in edge Rails</a>. This means that this plugin is virtually obsolete if you&#8217;re using edge Rails.</ins></p>
<p>If you&#8217;ve tried to output JSON from your Rails applications before, you&#8217;d probably have noticed how inadequate it all seems. Let&#8217;s look at the kind of output (pretty-printed for easier reading) you get from calling <code>to_json</code> on your typical User model:</p>
<pre><code class="javascript">{
  attributes: {
    id: 1,
    name: "Konata",
    awesome: true,
    created_at: "07/01/2007"
  }
}</code></pre>
<p>Some of the common problems developers who write applications that speak JSON have with this are:</p>
<ul>
<li>The <code>attributes</code> part is often unnecessary cruft when consuming ActiveRecord objects as JSON.</li>
<li>There&#8217;s no obvious way to control which attributes are shown in the JSON (e.g. you may want to hide private or lengthy attributes). The way to workaround this is either to write your own <code>to_json</code> instance method for your ActiveRecord model, or to use a Rails plugin (like <a href="http://geekblog.vodpod.com/?p=9">acts_as_json</a>) or a <a href="http://json.rubyforge.org/">gem</a>.</li>
<li>Including 2nd (or higher order) associations is tricky (as above, you can achieve this by defining your own <code>to_json</code> instance methods).</li>
</ul>
<h3>Under the hood of the current ActiveRecord#to_json</h3>
<p>The <code>to_json</code> method for ActiveRecord objects falls back on using that defined in <a href="http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/json">ActiveSupport&#8217;s JSON classes</a>. The way it&#8217;s currently done, there are a bunch of JSON encoder classes defined for various types &#8211; when you call <code>to_json</code> on an ActiveRecord object it simply uses <a href="http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/json/encoders/object.rb"><code>Object#to_json</code></a>.</p>
<p>By the way, see that <code>created_at: "07/01/2007"</code> bit in the example above which is the JSON output for a Date attribute? That&#8217;s in MM/DD/YYYY format. Yup, 1st July 2007 not 7th January 2007 you &#8220;rest of the world you&#8221;. Ouch, but at least you can use JavaScript&#8217;s <code>Date.parse()</code> on it directly as detailed in <a href="http://dev.rubyonrails.org/ticket/8399">this patch</a>. And before you ask, yes, <code>Date.parse()</code> doesn&#8217;t understand the YYYY-MM-DD format.</p>
<h3>Can has Rails patch?</h3>
<p>So about 2 days ago, I wrote a <a href="http://dev.rubyonrails.org/ticket/8920">Rails patch to boost ActiveRecord#to_json with an options hash</a>. Yep much like what you can do with <a href="http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html#M000910"><code>ActiveRecord#to_xml</code></a>.</p>
<p>I hope it gets committed into the core some day. I mean, gosh, isn&#8217;t Rails all about Web 2.0 mashups. It even has that fancy <code>render :json</code> thing. I do believe JSON output needs to be treated more seriously in Rails rather than the half-hearted attempt at JSON encoding. No? Well, if the patch doesn&#8217;t get accepted I&#8217;ll still have <a href="http://trac.codefront.net/jsonifier/">my plugin</a> (<a href="#jsonifier-plugin">see below</a>).</p>
<p>Anyway, let&#8217;s see some examples:</p>
<h3>Some examples, see?</h3>
<p>Assuming User and Post models where User has_many Posts:</p>
<pre><code class="ruby">david = User.find(1)
david.to_json  # {id: 1, name: "David", awesome: true, created_at: "07/01/2007"}
</code></pre>
<p>No more <code>attributes</code> cruft!</p>
<p><code>:only</code> and <code>:except</code> work the same way that as for <code>ActiveRecord#to_xml</code>:</p>
<pre><code class="ruby">david.to_json(:only => :name)                 # {name: "David"}
david.to_json(:only => [:id, :name])          # {id: 1, name: "David"}
david.to_json(:except => :created_at)         # {id: 1, name: "David", awesome: true}
david.to_json(:except => [:id, :created_at])  # {name: "David", awesome: true}</code></pre>
<p>You can use the <code>:methods</code> options as well to include any methods on the object.</p>
<pre><code class="ruby">david.to_json(:methods => :permalink)
  # {id: 1, name: "David", awesome: true, created_at: "07/01/2007", permalink => "1-David"}
david.to_json(:methods => [:permalink, :interestingness])
  # {id: 1, name: "David", awesome: true, created_at: "07/01/2007", \
  #   permalink => "1-David", :interestingness => 666}</code></pre>
<p>The <code>:include</code> option lets you include associations.</p>
<pre><code class="ruby">david.to_json(:include => :posts)
  # {id: 1, name: "David", awesome: true, created_at: "07/01/2007", \
  #    posts: [{id: 1, author_id: 1, title: "Welcome to the weblog"}, \
  #            {id: 2, author_id: 1, title: "So I was thinking"}]}</code></pre>
<p><code>:only</code>, <code>:except</code>, and <code>:methods</code> works on the included associations as well:</p>
<pre><code class="ruby">david.to_json(:include => { :posts => { :only => :title } })
  # {id: 1, name: "David", awesome: true, created_at: "07/01/2007", \
  #    posts: [{title: "Welcome to the weblog"}, \
  #            {title: "So I was thinking"}]}</code></pre>
<p>Of course, 2nd level (and higher order) associations work too:</p>
<pre><code class="ruby">david.to_json(:include => { :posts => { \
                              :include => { :comments => { \
                                              :only => :body } }, \
                              :only => :title } })
  # {id: 1, name: "David", awesome: true, created_at: "07/01/2007", \
  #    posts: [{comments: [{body: "1st post!"}, {body: "OMGWTFBBQ!"}], \
  #             title: "Welcome to the weblog"}, \
  #            {comments: [{body: "Don't think too hard"}], \
  #             title: "So I was thinking"}]}</code></pre>
<p>Please do give any feedback at the <a href="http://dev.rubyonrails.org/ticket/8920">Rails patch for ActiveRecord#to_json</a> or here. I wanna know what you feel about adding this functionality into the Rails core!</p>
<h3 id="jsonifier-plugin">Jsonifier plugin</h3>
<p>I&#8217;ve rolled the patch into a plugin: Jsonifier. The <a href="http://trac.codefront.net/jsonifier/">Jsonifier Trac</a> is at <a href="http://trac.codefront.net/jsonifier/">http://trac.codefront.net/jsonifier/</a>.</p>
<p>Git repository:</p>
<pre><code>git://github.com/chuyeow/jsonifier.git</code></pre>
<p>Github project page:</p>
<pre><code>http://github.com/chuyeow/jsonifier/</code></pre>
<p>To install the plugin:</p>
<pre><code>ruby script/plugin install git://github.com/chuyeow/jsonifier.git</code</pre>
<p> or <code>git clone</code> the repository manually into the <code>vendor/plugins</code> directory.</p>
<h3>A note on valid JSON</h3>
<p>The JSON Rails spits out by default is not strictly valid JSON since the <a href="http://www.ietf.org/rfc/rfc4627.txt?number=4627">JSON specifications</a> require keys to be double quoted. To get strictly valid JSON, add</p>
<pre><code class="ruby">ActiveSupport::JSON.unquote_hash_key_identifiers = false</code></pre>
<p>in your environment.rb (or in the Rails initializers directory if you're on edge). See <a href="http://blog.codefront.net/2007/06/20/how-to-get-strictly-valid-json-from-rails/">my blog post on how to get strictly valid JSON from Rails</a> for more info.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codefront.net/2007/07/11/better-json-output-from-rails-with-the-jsonifier-plugin/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
	</channel>
</rss>

