<?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>footle &#187; rails</title>
	<atom:link href="http://blog.footle.org/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.footle.org</link>
	<description>(intr.v.) 1. To waste time; trifle. 2. To talk nonsense. (n.) Nonsense; foolishness</description>
	<lastBuildDate>Fri, 30 Jul 2010 04:34:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>MySQL-Style Output for the Rails Console</title>
		<link>http://blog.footle.org/2009/03/01/mysql-style-output-for-the-rails-console/</link>
		<comments>http://blog.footle.org/2009/03/01/mysql-style-output-for-the-rails-console/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 01:42:47 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.footle.org/?p=103</guid>
		<description><![CDATA[While poking around the database in my rails console, I often found myself jumping to the mysql console just so I could get an easier-to-digest view of the data. I finally had enough of this silliness and wrote up a quick function to dump of set of ActiveRecord objects in the mysql report format. >> [...]]]></description>
			<content:encoded><![CDATA[<p>While poking around the database in my rails console, I often found myself jumping to the mysql console just so I could get an easier-to-digest view of the data. I finally had enough of this silliness and wrote up a quick function to dump of set of ActiveRecord objects in the mysql report format.</p>
<p><code>
<pre>>> report(records, :id, :amount, :created_at)
+------+-----------+--------------------------------+
| id   | amount    | created_at                     |
+------+-----------+--------------------------------+
| 8301 | $12.40    | Sat Feb 28 09:20:47 -0800 2009 |
| 6060 | $39.62    | Sun Feb 15 14:45:38 -0800 2009 |
| 6061 | $167.52   | Sun Feb 15 14:45:38 -0800 2009 |
| 6067 | $12.00    | Sun Feb 15 14:45:40 -0800 2009 |
| 6059 | $1,000.00 | Sun Feb 15 14:45:38 -0800 2009 |
+------+-----------+--------------------------------+
5 rows in set</pre>
<p></code></p>
<p><a href="http://gist.github.com/72234">Grab it from GitHub</a> and stick it in your <code>.irbrc</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.footle.org/2009/03/01/mysql-style-output-for-the-rails-console/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ConditionsConstructor</title>
		<link>http://blog.footle.org/2008/02/27/conditionsconstructor/</link>
		<comments>http://blog.footle.org/2008/02/27/conditionsconstructor/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 15:04:59 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.footle.org/2008/02/27/conditionsconstructor/</guid>
		<description><![CDATA[I hadn&#8217;t posted this before because it seemed kind of trivial and silly, but I used it again the other day and decided maybe I should share the joy after all. Far too often I find myself jumping through hoops building ActiveRecord find conditions arrays dynamically depending on what arguments were passed in to the [...]]]></description>
			<content:encoded><![CDATA[<p>I hadn&#8217;t posted this before because it seemed kind of trivial and silly, but I used it again the other day and decided maybe I should share the joy after all.</p>
<p>Far too often I find myself jumping through hoops building ActiveRecord <code>find</code> conditions arrays dynamically depending on what arguments were passed in to the method. So I cooked up a simple little class to clean that up a bit:</p>
<p><code>
<pre>
# class to construct conditions for AR queries
# cc = ConditionsConstructor.new('foobar &gt; ?', 7)
# cc.add('blah is not null')
# cc.add('baz in (?)', [1,2,3])
# cc.conditions
# # => ["foobar &gt; ? and blah is not null and baz in (?)", 7, [1, 2, 3]]
class ConditionsConstructor
  def initialize(str = nil, *args)
    @conditions_strs = str ? [str] : []
    @conditions_args = args
  end

  def add(str, *args)
    @conditions_strs &lt;&lt; str
    @conditions_args += args
  end

  def conditions
    [@conditions_strs.join(' and ')] + @conditions_args
  end
end
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.footle.org/2008/02/27/conditionsconstructor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary multipart POSTs in Javascript</title>
		<link>http://blog.footle.org/2007/07/31/binary-multipart-posts-in-javascript/</link>
		<comments>http://blog.footle.org/2007/07/31/binary-multipart-posts-in-javascript/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 12:32:57 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://blog.footle.org/2007/08/01/binary-multipart-posts-in-javascript/</guid>
		<description><![CDATA[We recently released a very slick Firefox extension at Wesabe. It was written by my colleague Tim Mason, but I helped figure out one small piece of it&#8212;namely, how to do binary multipart POSTs in Javascript&#8212;and since it involved many hours of hair-pulling for both of us, I thought I&#8217;d share the knowledge. (Tim says [...]]]></description>
			<content:encoded><![CDATA[<p>We recently released a very slick <a href="http://blog.wesabe.com/index.php/2007/07/25/the-wesabe-firefox-uploader/">Firefox extension</a> at <a href="https://www.wesabe.com">Wesabe</a>. It was written by my colleague Tim Mason, but I helped figure out one small piece of it&mdash;namely, how to do binary multipart POSTs in Javascript&mdash;and since it involved many hours of hair-pulling for both of us, I thought I&#8217;d share the knowledge. (Tim says I should note that &#8220;this probably only works in Firefox version 2.0 and greater _and_ that it uses Mozilla specific calls that are only allowed for privileged javascript&mdash;basically only for extensions.&#8221;)</p>
<p>One of the cool features of the plugin is the ability to take a <a href="http://blog.wesabe.com/index.php/2007/07/25/one-more-thing-browser-snapshot-and-file-attachments/">snapshot</a> of a full browser page and either save the snapshot to disk or upload it to Wesabe (so you can, for example, save the receipt for a web purchase along with that transaction in your account). The snapshot is uploaded to Wesabe via a standard multipart POST, the same way that a file is uploaded via a web form.</p>
<p>Tim was having trouble getting the POST to work with binary data at first, and he had other things to finish, so he wanted to just base-64-encode it and be done with it. I was reluctant to do that, as the size of the upload would be significantly larger (about <a href="http://en.wikipedia.org/wiki/Base64">137%</a> of the original). Also, Rails didn&#8217;t automatically decode base-64-encoded file attachments. But Tim had other bugs to fix, so I submitted a <a href="http://dev.rubyonrails.org/ticket/9016">patch</a> to Rails to do the base-64 decoding. I was pretty proud of this patch until it was pointed out to me that <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.4.5">RFC 2616</a> specifically disallows the use of Content-Transfer-Encoding in HTTP. Doh. They also realized that it is a colossal waste of bandwidth.</p>
<p>Since Tim was cramming to meet a hard(-ish) deadline set for the release of the plugin, I offered to lend my eyeballs to the binary post problem. This could be a very long story, but I&#8217;ll just get to the point: you can read binary data in to a Javascript string and dump it right out to a file just fine, but if you try to do any concatenation with that string, Javascript ends up munging it mercilessly. I&#8217;m not sure whether it is trying to interpret it as UTF8 or if it terminates it as soon as it hits a null byte (which is what seemed to be happening), but regardless, doing <code>"some string" + binaryData + "another string"</code>, as is necessary when putting together a mutipart post, just does not work.</p>
<p>The answer required employing Rube Goldbergian system of input and output streams. The seed of the solution was found on <a href="http://developer.taboca.com/cases/en/XMLHTTPRequest_post_utf-8/">this post</a>, although that didn&#8217;t explain how to mix in all of the strings needed for the post and MIME envelope. So here it is, in all it&#8217;s goriness:</p>
<pre><code>
var dataURL = this.canvas.toDataURL(this.getImageType()); // grab the snapshot as base64
var imgData = atob(dataURL.substring(13 + this.getImageType().length)); // convert to binary

var filenameTimestamp = (new Date().getTime());
var separator = "----------12345-multipart-boundary-" + filenameTimestamp;

// Javascript munges binary data when it undergoes string operations (such as concatenation), so we need
// to jump through a bunch of hoops with streams to make sure that doesn't happen

// create a string input stream with the form preamble
var prefixStringInputStream = Components.classes["@mozilla.org/io/string-input-stream;1"].createInstance(Components.interfaces.nsIStringInputStream);
var formData =
	"--" + separator + "\\r\\n" +
	"Content-Disposition: form-data; name=\"data\"; filename=\"snapshot_" + filenameTimestamp +
	(this.getImageType() === "image/jpeg" ? ".jpg" : ".png") + "\"\\r\\n" +
	"Content-Type: " + this.getImageType() + "\\r\\n\\r\\n";
prefixStringInputStream.setData(formData, formData.length);

// write the image data via a binary output stream, to a storage stream
var binaryOutputStream = Components.classes["@mozilla.org/binaryoutputstream;1"].createInstance(Components.interfaces.nsIBinaryOutputStream);
var storageStream = Components.classes["@mozilla.org/storagestream;1"].createInstance(Components.interfaces.nsIStorageStream);
storageStream.init(4096, imgData.length, null);
binaryOutputStream.setOutputStream(storageStream.getOutputStream(0));
binaryOutputStream.writeBytes(imgData, imgData.length);
binaryOutputStream.close();

// write out the rest of the form to another string input stream
var suffixStringInputStream = Components.classes["@mozilla.org/io/string-input-stream;1"].createInstance(Components.interfaces.nsIStringInputStream);
formData =
	"\\r\\n--" + separator + "\\r\\n" +
	"Content-Disposition: form-data; name=\"description\"\\r\\n\\r\\n" + description + "\\r\\n" +
	"--" + separator + "--\\r\\n";
suffixStringInputStream.setData(formData, formData.length);

// multiplex the streams together
var multiStream = Components.classes["@mozilla.org/io/multiplex-input-stream;1"].createInstance(Components.interfaces.nsIMultiplexInputStream);
multiStream.appendStream(prefixStringInputStream);
multiStream.appendStream(storageStream.newInputStream(0));
multiStream.appendStream(suffixStringInputStream);

// post it
req.open("POST", "http://yoursite.com/upload_endpoint", true);
req.setRequestHeader("Accept", "*/*, application/xml");
req.setRequestHeader("Content-type", "multipart/form-data; boundary=" + separator);
req.setRequestHeader("Content-length", multiStream.available());
req.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
req.setRequestHeader("User-Agent", "YourUserAgent/1.0.0");
req.send(multiStream);
</code></pre>
<p><strong>Update:</strong> Spaces removed from multipart boundary per Gijsbert&#8217;s suggestion in the comments (thanks!).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.footle.org/2007/07/31/binary-multipart-posts-in-javascript/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Reloading Ruby Classes in Rails</title>
		<link>http://blog.footle.org/2007/05/13/reloading-ruby-classes-in-rails/</link>
		<comments>http://blog.footle.org/2007/05/13/reloading-ruby-classes-in-rails/#comments</comments>
		<pubDate>Mon, 14 May 2007 02:58:54 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.footle.org/2007/05/13/reloading-ruby-classes-in-rails/</guid>
		<description><![CDATA[I ran across a bug in Date::Format today, and after spending a few hours hacking away at a fix (the date/format.rb code is *uuuuugly* and *sloooow*&#8230;someone should really rewrite that. Better yet, rewrite it in C), I thought I&#8217;d submit a patch. So I grabbed the ruby_1_8 branch and lo and behold, my issue had [...]]]></description>
			<content:encoded><![CDATA[<p>I ran across a bug in Date::Format today, and after spending a few hours hacking away at a fix (the date/format.rb code is *uuuuugly* and *sloooow*&#8230;someone should really rewrite that. Better yet, rewrite it in C), I thought I&#8217;d submit a patch. So I grabbed the <a href="http://www.ruby-lang.org/en/community/ruby-core/">ruby_1_8 branch</a> and lo and behold, my issue had already been fixed!</p>
<p>So the question now was how to monkeypatch the entire Date::Format module? Simply <code>require</code>-ing it as a plugin doesn&#8217;t work, since Date::Format is already loaded at that point. The trick then is to use <code>load</code> instead of require.</p>
<p>First, I tried this:</p>
<pre><code>
load 'date/format.rb'
</code></pre>
<p>(note: load needs the actual filename; it doesn&#8217;t have the magic that require does) but that gave me an error:</p>
<pre><code>
in `load': wrong number of arguments (1 for 0) (ArgumentError)
</code></pre>
<p>Turns out Rails::Plugin::Loader defines its own <code>load</code>, so:</p>
<pre><code>
Kernel::load 'date/format.rb'
</code></pre>
<p>et voila!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.footle.org/2007/05/13/reloading-ruby-classes-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Productivity in Java vs. Rails</title>
		<link>http://blog.footle.org/2006/05/31/productivity-in-java-vs-rails/</link>
		<comments>http://blog.footle.org/2006/05/31/productivity-in-java-vs-rails/#comments</comments>
		<pubDate>Wed, 31 May 2006 17:57:35 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[geek]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://footle.org/blog/?p=55</guid>
		<description><![CDATA[I am far more productive when writing Rails code than when writing Java. I just realized that one of the reasons for my lower productivity in Java is the need to recompile every time a make a change to a page on the site. In the 15 seconds or so it takes to recompile and [...]]]></description>
			<content:encoded><![CDATA[<p>I am far more productive when writing Rails code than when writing Java. I just realized that one of the reasons for my lower productivity in Java is the need to recompile every time a make a change to a page on the site. In the 15 seconds or so it takes to recompile and redeploy to Tomcat, I get bored and am apt to go check my new favorite news site, <a href="http://popurls.com">popurls</a>, or my RSS feeds, or (less likely) post to my blog. Suddenly those 15 seconds have become 5 minutes. And this happens many times throughout the day.</p>
<p>With Rails, I make a change, refresh my browser, and there it is. On to the next step.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.footle.org/2006/05/31/productivity-in-java-vs-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
