<?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>jonathanjulian.com &#187; Uncategorized</title>
	<atom:link href="http://jonathanjulian.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanjulian.com</link>
	<description>Ruby, Rails, JavaScript, software development</description>
	<lastBuildDate>Fri, 13 Jan 2012 21:03:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>The order of email parts matters to gmail</title>
		<link>http://jonathanjulian.com/2011/02/the-order-of-email-parts-matters-to-gmail/</link>
		<comments>http://jonathanjulian.com/2011/02/the-order-of-email-parts-matters-to-gmail/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 22:10:50 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[actionmailer]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[gmail]]></category>

		<guid isPermaLink="false">http://jonathanjulian.com/?p=282</guid>
		<description><![CDATA[Gmail only shows the &#8220;last&#8221; part of a multi-part email in the inbox, even if it&#8217;s text/plain. So always order your parts accordingly: text/plain first, then text/html. This just caused me to run around in circles for a while looking for the reason my awesome html email was not being displayed correctly in gmail. I [...]]]></description>
			<content:encoded><![CDATA[<p>Gmail only shows the &#8220;last&#8221; part of a multi-part email in the inbox, even if it&#8217;s text/plain. So always order your parts accordingly: text/plain first, then text/html. This just caused me to run around in circles for a while looking for the reason my awesome html email was not being displayed correctly in gmail.</p>

<p>I only found <a href="http://www.bennadel.com/blog/188-GMail-Chooses-ONLY-Non-HTML-Mail-If-Given-The-Option.htm">a little bit</a> of info when googling for this, so hopefully this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanjulian.com/2011/02/the-order-of-email-parts-matters-to-gmail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RubyNation 2010</title>
		<link>http://jonathanjulian.com/2010/04/rubynation-2010/</link>
		<comments>http://jonathanjulian.com/2010/04/rubynation-2010/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 14:02:07 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonathanjulian.com/?p=216</guid>
		<description><![CDATA[This past weekend was fun-filled with geekyness in the DC area! Here are a few of my favorite sessions and thoughts from RubyNation 2010. Grease Your Suite by Nick Gauthier: Nick killed it with his presentation on how he used all sorts of optimizations to get his project&#8217;s test suite from 13 minutes down to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.rubynation.org/"><img src="http://www.rubynation.org/images/conference/badges/2010/rubyWebBadgeConference.png" class="alignright"/></a>
This past weekend was fun-filled with geekyness in the DC area! Here are a few of my favorite sessions and thoughts from <a href="http://www.rubynation.org/">RubyNation</a> 2010.</p>

<p><a href="http://grease-your-suite.heroku.com/">Grease Your Suite</a> by <a href="http://twitter.com/ngauthier">Nick Gauthier</a>: Nick killed it with his presentation on how he used all sorts of optimizations to get his project&#8217;s test suite from 13 minutes down to less than 30 seconds. Wow! Presentation style was fun and playful, while still being hardcore and informative (optimizing the filesystem? whoa). I&#8217;m really glad this talk was inserted, and wished more people could have seen it.</p>

<p><a href="http://pure-rspec-scotruby.heroku.com/">Pure RSpec</a> by <a href="http://blog.l4rk.com/">John Larkowski</a>: This talk was all about rspec syntax, and how to make your tests read better. I really dig this type of talk, since I always come away with a dozen new tips to integrate into my code right away.</p>

<p><strong>Git Guts</strong> by <a href="http://www.nearinfinity.com/home/bios/jeff_kunkle.html">Jeff Kunkle</a>: Oh man, Jeff unleashed all the crazy details about internal git objects and trees within the first 3 minutes. This was not just a discussion of the <a href="/2009/08/basic-git/">basic git</a> commands! This was all about how git works under the covers. Not surprisingly, there were plenty of questions and discussions at the end. Maybe git workflow with branches and merging and rebasing is something we should talk about publicly more often.</p>

<p><a href="http://cloud.github.com/downloads/russolsen/presentations/InsideRuby-1.pdf">Looking Inside Your Ruby Implementation</a> by <a href="http://jroller.com/rolsen/">Russ Olsen</a>: &#8220;How many of you are comfortable with C programming?&#8221; Russ asked. Heh. He went on to take us on a tour of the C code behind <code>puts</code>. Then he took us through the jRuby implementation too! Unique and informative.</p>

<p><a href="http://www.scribd.com/doc/27174770/Garbage-Collection-and-the-Ruby-Heap">Garbage Collection and the Ruby Heap</a> by <a href="http://timetobleed.com/">Joe Damato</a> and <a href="http://twitter.com/tmm1">Aman Gupta</a>: I heard this described this as &#8220;if you work with Ruby, you <em>gotta</em> go to this one&#8221;. It reminded me of a talk I saw at JavaOne way back when about the Java gc. Very detailed.</p>

<p>Sadly, I lollygagged back from lunch on Friday and missed <a href="http://smartlogicsolutions.com/john">John Trupiano</a> presenting <a href="http://i-dont-trust-your-code.heroku.com/">I Don&#8217;t Trust Your Code</a> which contains excellent details about how gem maintainers should structure, build, and manage public gems. Excellent advice! This presentation, as well as a few others this weekend, were built with <a href="http://github.com/schacon/showoff">showoff</a>. It&#8217;s easy to use:</p>

<pre><code>gem install showoff
git clone git://github.com/jtrupiano/i_dont_trust_your_code.git
cd i_dont_trust_your_code
showoff serve
</code></pre>

<p>Now open a browser to http://localhost:9090, and right-arrow through the slides!</p>

<p><a href="http://github.com/jcasimir/teach_anything">How to Teach Anything to Anyone, Even Your Dev Team</a> by <a href="http://twitter.com/j3">Jeff Casimir</a>: Jeff says he wishes he could speak like Dave Thomas&#8230;I say, I wish I could speak like Jeff. This was a non-technical and inspirational presentation of some of the basic concepts of teaching. Using books, graphs, and statistics, Jeff took us through the various ways people learn, especially highlighting the &#8220;gradual release of responsibility principle&#8221;. If I had to pick a favorite talk from the weekend, this would be it. <em>&#8220;I do, we do, you do&#8221;</em>, Jeff.</p>

<p><strong>Blocks, Procs and Lambdas, Oh My! Functional Programming in Ruby</strong> by <a href="http://paulbarry.com/">Paul Barry</a>: Out of my colleagues, Paul holds the title of &#8220;The Language Mayor&#8221;. During this talk, he showed us some &#8220;functional&#8221; programming constructs and how they can be applied to Ruby as well as other languages such as Clojure. Rich with syntax and details.</p>

<p><a href="http://www.scribd.com/doc/15336842/Rails-in-the-Large-How-Were-Developing-the-Largest-Rails-Project-in-the-World">Rails in the Large</a> by <a href="http://www.nealford.com/">Neal Ford</a>: Other than using rock/paper/scissors to solve office dilemmas, Neal also seems to work on one of the hugest Rails projects around. 11 pairs of programmers!?! It sounds insane, and it probably is. Wow. Great insights from an &#8220;enterprise&#8221; team, including PMs, BAs, UAT, and coffee delivery services.</p>

<p>But, as always, the real value of these local conferences is with the <em>people</em> who attend, and some of the most fun was not sitting and listening, but sitting and <em>conversing</em> and <em>working</em> and <em>learning</em>. I spent some time building an interactive Google map into a project <a href="http://twitter.com/jtrupiano">@jtrupiano</a> is working on. I hung out with <a href="http://twitter.com/nerded">@nerded</a> and learned why I should be using <a href="http://gitx.frim.nl/">gitx</a> more often. Over a beer at the RuBy-B-Q, I caught up with <a href="http://twitter.com/abatalion">@abatalion</a> and met <a href="http://twitter.com/marchdoe">@marchdoe</a>, then Paul and I owned the foosball table. I learned how <a href="http://twitter.com/bryanl">@bryanl</a> likes to set up maintenance contracts with his clients, and we both agreed that there is plenty of work in the mid-atlantic region right now for all of us. I met <a href="http://twitter.com/greggpollack">@greggpollack</a> for the first time. I talked natural database id&#8217;s with <a href="http://twitter.com/avdi">@avdi</a>. I think I convinced <a href="http://twitter.com/stevenhaddox">@stevenhaddox</a> that he can make it to more <a href="http://bmoreonrails.org">bmoreonrails</a> meetings if he brings his family! I got to meet <a href="http://twitter.com/mbleigh">@mbleigh</a> and learned about his Rack-based authentication system for twitter/oauth/facebook/etc called <a href="http://github.com/intridea/omniauth">OmniAuth</a>. Over beers in the hotel bar, I hacked on the new <a href="http://bmoreonrails.org">bmoreonrails</a> website, adding live tweets and a member roster. And the ride up and back from Baltimore with <a href="http://twitter.com/pjb3">@pjb3</a> was chock-filled with fun talk about rspec, rss readers, and rails!</p>

<p>Hopefully, I&#8217;ll have a similar experience this coming week at <a href="http://jsconf.us/2010">JSConf</a>, as well as at <a href="http://railsconf.com">Railsconf</a> this June!</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanjulian.com/2010/04/rubynation-2010/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Rails to_json or as_json?</title>
		<link>http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/</link>
		<comments>http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 17:08:36 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonathanjulian.com/?p=187</guid>
		<description><![CDATA[A really great modification was introduced in Rails 2.3.3 &#8211; and while everyone clamored about JSON encoding speeds and C vs Ruby implementations, the blogosphere overlooked the clean separation of responsibility that was introduced. In the &#8220;old days&#8221;, you&#8217;d override to_json in your model class to provide a JSON implementation of your model. Then in [...]]]></description>
			<content:encoded><![CDATA[<p>A really great modification was introduced in <a href="http://weblog.rubyonrails.org/2009/7/20/rails-2-3-3-touching-faster-json-bug-fixes">Rails 2.3.3</a> &#8211; and while everyone clamored about JSON <a href="http://www.ruby-forum.com/topic/200017">encoding speeds</a> and <a href="http://flori.github.com/json/">C vs Ruby implementations</a>, the blogosphere overlooked the clean separation of responsibility that was introduced.</p>

<p>In the &#8220;old days&#8221;, you&#8217;d override <code>to_json</code> in your model class to provide a JSON implementation of your model. Then in your controller, <code>render :json =&gt; @model</code> would work perfectly. And some folks would even redundantly code <code>render :json =&gt; @model.to_json</code>, and that would work too.</p>

<p><code>to_json</code> even had some great options for ActiveRecord objects! You could tell the method to only render certain attributes, or to include associations or method calls!</p>

<pre><code>render :json =&gt; 
  @user.to_json(:only =&gt; [:email], :include =&gt; [:addresses])
</code></pre>

<p>Life was good. But things start to fall apart when you want to do something a little out of the ordinary. Like return JSON with the model as part of a bigger structure.</p>

<pre><code>render :json =&gt; { :success =&gt; true, 
  :user =&gt; @user.to_json(:only =&gt; [:email]) }
</code></pre>

<p>Oops. <code>{\"user\":{\"email\":\"me@example.com\","success":true}</code>has the JSON characters <em>escaped</em>, which is not what we want. So what do we do? We hack around it:</p>

<pre><code>render :json =&gt; { :success =&gt; true, 
  :user =&gt; { :email =&gt; @user.email } }
</code></pre>

<p>But this doesn&#8217;t scale &#8211; we have to explicitly create the JSON <em>by hand</em> in the <em>controller</em>. What if we need 5 or more attributes? Yuck!</p>

<p>Enter <strong>ActiveSupport 2.3.3</strong>. Now the <em>creation</em> of the json is separate from the <em>rendering</em> of the json. <code>as_json</code> is used to create the structure of the JSON as a Hash, and the rendering of that hash into a JSON string is left up to <a href="http://as.rubyonrails.org/classes/ActiveSupport/JSON.html"><code>ActiveSupport::json.encode</code></a>. You should never use <code>to_json</code> to <em>create</em> a representation, only to <em>consume</em> the representation.</p>

<pre><code>def as_json(options={})
  { :email =&gt; self.email }
end
</code></pre>

<p>Anytime <code>to_json</code> is called on an object, <code>as_json</code> is invoked to create the data structure, and then that hash is encoded as a JSON string using <code>ActiveSupport::json.encode</code>. This happens for all types: Object, Numeric, Date, String, etc (see <a href="http://github.com/rails/rails/tree/2-3-stable/activesupport/lib/active_support/json">active_support/json</a>).</p>

<p>ActiveRecord objects behave the same way. There is a default <code>as_json</code> <a href="http://github.com/rails/rails/blob/2-3-stable/activerecord/lib/active_record/serializers/json_serializer.rb">implementation</a> that creates a Hash that includes all the model&#8217;s attributes. <strong>You should override <code>as_json</code> in your Model to create the JSON structure you want</strong>. <code>as_json</code>, just like the old <code>to_json</code>, takes an option hash where you can specify attributes and methods to include declaratively.</p>

<pre><code>def as_json(options={})
  super(:only =&gt; [:email, :avatar], :include =&gt;[:addresses])
end
</code></pre>

<p>Your controller code to display one model should always look like this:</p>

<pre><code>render :json =&gt; @user
</code></pre>

<p>And if you have to do anything <a href="http://namxam.tumblr.com/post/396486333/rails-as-json-vs-to-json">out of the ordinary</a>, call <code>as_json</code> passing your options.</p>

<pre><code>render :json =&gt; { :success =&gt; true, 
  :user =&gt; @user.as_json(:only =&gt; [:email]) }
</code></pre>

<p>The moral of the story is: <strong>In controllers, do not call <code>to_json</code> directly, allow <code>render</code> to do that for you. If you need to tweak the JSON output, override <code>as_json</code> in your model, or call <code>as_json</code> directly.</strong></p>

<p>Fix your code now to use <code>as_json</code> &#8211; it will be one less thing to worry about when you migrate to Rails 3.</p>

<p><em>This post was inspired by the investigation I went into while exploring the answer to <a href="http://stackoverflow.com/questions/2572284/override-to-json-in-rails-2-3-5/2574900">this question on Stack Overflow</a>.</em></p>

<p><em>Hey Brian Morearty &#8211; Rails was never Javaficated to begin with. So the answer is <a href="http://bmorearty.wordpress.com/2009/07/20/to_json-as_json/">yes</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>

