<?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>Cory O&#039;Daniel - These are just words &#187; rails</title>
	<atom:link href="http://coryodaniel.com/index.php/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://coryodaniel.com</link>
	<description>Software development, thoughts, and randomness</description>
	<lastBuildDate>Wed, 01 Sep 2010 18:49:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Attaching local or remote files to Paperclip and Milton Models in Rails (Mocking content_type and original_filename in a Tempfile)</title>
		<link>http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/</link>
		<comments>http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 22:53:18 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[content_type]]></category>
		<category><![CDATA[milton]]></category>
		<category><![CDATA[paperclip]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tempfile]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=495</guid>
		<description><![CDATA[I was working on a project today where I needed to import some data from MySpace accounts (yeah, MySpace), which included importing the users profile image. In the controller that did the importing I was using OpenURI to retrieve the image and then turn it into a Tempfile to be attached the the model, like [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on a project today where I needed to import some data from MySpace accounts (yeah, MySpace), which included importing the users profile image. In the controller that did the importing I was using OpenURI to retrieve the image and then turn it into a Tempfile to be attached the the model, like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> import
<span style="color:#008000; font-style:italic;">#...snip</span>
  tempfile = <span style="color:#CC00FF; font-weight:bold;">Tempfile</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> my_filename<span style="color:#006600; font-weight:bold;">&#41;</span>
  tempfile.<span style="color:#9900CC;">write</span> <span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span> image_url <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span>
  <span style="color:#0066ff; font-weight:bold;">@imported_user</span>.<span style="color:#9900CC;">images</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:file</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> tempfile<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#...snip</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>This doesn't work.  It blows up missing one of two methods:</p>
<ol>
<li>#original_filename</li>
<li>#content_type</li>
</ol>
<p>If you inspect a normal file upload in Rails which has these methods, you'll find that is just a regular old Tempfile.  But it has the methods! If you create a Tempfile manually, it won't have the methods.  That's because Rails magics them on.  I am not sure why they don't create a subclass like Rails::Tempfile that contains these methods and just use that.  I guess its because OOP is retarded (sarcasm).</p>
<p>So, I wrote a little subclass that will take a file path, and quack like the magic'd rails Tempfiles you get from an upload so you can attach local files to models or even remote files.</p>
<p>This works pretty straightforward and I'm currently using it in production. It won't work on windows systems because the dependency on the 'file' binary. Also, some linux systems are missing this library by default, so make sure you yum|dpkg|apt|port or whatever to get it installed.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open-uri'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'digest/sha1'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> RemoteFile <span style="color:#006600; font-weight:bold;">&lt;</span> ::<span style="color:#CC00FF; font-weight:bold;">Tempfile</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>path, tmpdir = <span style="color:#CC00FF; font-weight:bold;">Dir</span>::tmpdir<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@original_filename</span>  = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">basename</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@remote_path</span>        = path
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">super</span> <span style="color:#6666ff; font-weight:bold;">Digest::SHA1</span>.<span style="color:#9900CC;">hexdigest</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>, tmpdir
    fetch
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> fetch
    string_io = OpenURI.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:open</span>, <span style="color:#0066ff; font-weight:bold;">@remote_path</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">write</span> string_io.<span style="color:#9900CC;">read</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">rewind</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> original_filename
    <span style="color:#0066ff; font-weight:bold;">@original_filename</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> content_type
    mime = <span style="color:#996600;">`file --mime -br #{self.path}`</span>.<span style="color:#9900CC;">strip</span>
    mime = mime.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^.<span style="color:#006600; font-weight:bold;">*</span>: <span style="color:#006600; font-weight:bold;">*/</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    mime = mime.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>;.<span style="color:#006600; font-weight:bold;">*</span>$<span style="color:#006600; font-weight:bold;">/</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    mime = mime.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>,.<span style="color:#006600; font-weight:bold;">*</span>$<span style="color:#006600; font-weight:bold;">/</span>,<span style="color:#996600;">&quot;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    mime
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Usage is pretty simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">remote_file = RemoteFile.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;http://www.google.com/intl/en_ALL/images/logo.gif&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
remote_file.<span style="color:#9900CC;">original_filename</span> <span style="color:#008000; font-style:italic;">#=&gt; logo.gif</span>
remote_file.<span style="color:#9900CC;">content_type</span> <span style="color:#008000; font-style:italic;">#= image/gif</span></pre></td></tr></table></div>

<p>Using it in your controller:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> import
  <span style="color:#008000; font-style:italic;">#...snip</span>
  <span style="color:#0066ff; font-weight:bold;">@imported_user</span>.<span style="color:#9900CC;">images</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:file</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> RemoteFile.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> url_to_image <span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;">#...snip</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p align="left"><a class="tt" href="http://twitter.com/home/?status=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i...+http://bit.ly/8ZmCoU" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;title=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;t=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;title=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;title=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Expiring rails fragement caches using an expires time instead of a sweeper (expires_in) &#8211; (a &#8216;duh&#8217; post)</title>
		<link>http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 00:50:33 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[duh]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=466</guid>
		<description><![CDATA[So, I'm setting up fragment caching for some stuff, and I honestly don't care about setting up an observer to sweep some stuff. I just want to cache this one slow fragment for like 5 minutes. I've seen these tutorials all over the web that shows people using the expires_in setting to auto-expire cache like [...]]]></description>
			<content:encoded><![CDATA[<p>So, I'm setting up fragment caching for some stuff, and I honestly don't care about setting up an observer to sweep some stuff. I just want to cache this one slow fragment for like 5 minutes. I've seen these tutorials all over the web that shows people using the expires_in setting to auto-expire cache like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Rails.<span style="color:#9900CC;">cache</span>.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'test_key'</span>, <span style="color:#996600;">'test_value'</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 5.<span style="color:#9900CC;">minutes</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>That's great and all, but how the hell do I use it for a Page, Action or Fragment cache? Well you just pass the same :expires_in param and yay it works as long as its a mem_cache_store (although, I did see a hackaroo for file_store)</p>
<p>Even the<a href="http://guides.rubyonrails.org/caching_with_rails.html"> Rails Guide</a> on caching mentions 'expires_in' but doesn't show how to use it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> StupidController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  caches_page <span style="color:#ff3333; font-weight:bold;">:whatever</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 5.<span style="color:#9900CC;">minutes</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>What really got me was when I was trying to use it with a fragment cache. I tried this and it didn't work:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">-</span> cache<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'home'</span>, <span style="color:#ff3333; font-weight:bold;">:action_suffix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'advertisements'</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 10.<span style="color:#9900CC;">minutes</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span></pre></td></tr></table></div>

<p>Why? Well, the cache method takes two hashes, and if you leave off the curly braces, that expires_in ends up in the first one, which is used for the name... To roll it right do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">-</span> cache<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:action <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'home'</span>, <span style="color:#ff3333; font-weight:bold;">:action_suffix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'advertisements'</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:expires_in</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> 10.<span style="color:#9900CC;">minutes</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span></pre></td></tr></table></div>

<p>Duh, the more you know.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29+http://bit.ly/bhC1Vi" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/&amp;title=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/&amp;t=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/&amp;title=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/&amp;title=Expiring+rails+fragement+caches+using+an+expires+time+instead+of+a+sweeper+%28expires_in%29+%E2%80%93+%28a+%E2%80%98duh%E2%80%99+post%29" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/10/expiring-rails-fragement-caches-using-an-expires-time-instead-of-a-sweeper-expires_in-a-duh-post/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Rails rake file for compressing your Javascript with YUI</title>
		<link>http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 18:35:29 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=462</guid>
		<description><![CDATA[Here is a quick rake file I threw together to use with my Ruby YUI 2.0 (I-refuse-to-make-a-gem-
edition).
This of course should be used with the Ruby YUI Compressor I posted about the other day.
This script will tar/gzip all of your javascripts just incase something stupid happens. So you have a little safety net.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'fileutils'
require 'pathname'
&#160;
namespace :js [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a quick rake file I threw together to use with my Ruby YUI 2.0 (I-refuse-to-make-a-gem-<br />
edition).</p>
<p>This of course should be used with the <a href="http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/">Ruby YUI Compressor</a> I posted about the other day.</p>
<p>This script will tar/gzip all of your javascripts just incase something stupid happens. So you have a little safety net.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'fileutils'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'pathname'</span>
&nbsp;
namespace <span style="color:#ff3333; font-weight:bold;">:js</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  desc <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>INFO
Output Compressed JavaScript to STDOUT; 
  COMPRESS=ALL to compress all Javascript files that DONT contain <span style="color:#996600;">&quot;.min.&quot;</span> 
INFO
&nbsp;
  task <span style="color:#ff3333; font-weight:bold;">:yui</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    targz<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'COMPRESS'</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'ALL'</span>
      <span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span>Rails.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#ff3333; font-weight:bold;">:public</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#ff3333; font-weight:bold;">:javascripts</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#996600;">'**/*.js'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>javascript<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> javascript =~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#9900CC;">min</span>\.<span style="color:#006600; font-weight:bold;">/</span>
&nbsp;
        compress<span style="color:#006600; font-weight:bold;">&#40;</span>javascript<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exist</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>Rails.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;config/yui.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        javascripts = <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;config/yui.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;javascripts&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">if</span> !javascripts.<span style="color:#9900CC;">blank</span>?
          javascripts.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>script<span style="color:#006600; font-weight:bold;">|</span> compress<span style="color:#006600; font-weight:bold;">&#40;</span>Rails.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;public/javascripts&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> script<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, <span style="color:#996600;">&quot;No javascript files in config/yui.yml&quot;</span> 
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, <span style="color:#996600;">&quot;config/yui.yml Not Found; Do rake js:generate to create one or COMPRESS=ALL to run without a config file&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  desc <span style="color:#996600;">&quot;Generate YUI Compressor Config file&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:generate</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    config_path = Rails.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;config/yui.yml&quot;</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>config_path, <span style="color:#996600;">&quot;w+&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
      f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>CONFIG
<span style="color:#006600; font-weight:bold;">---</span>
javascripts:
  <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#996600;">&quot;application.js&quot;</span>
  <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#996600;">&quot;jquery.js&quot;</span>
CONFIG
      <span style="color:#CC0066; font-weight:bold;">puts</span> config_path
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> targz<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    tgz_file = <span style="color:#996600;">&quot;#{Time.now.to_i}.javascripts.tgz&quot;</span>
    <span style="color:#996600;">`cd #{Rails.root + &quot;public&quot;}; tar -zcf #{tgz_file} javascripts/`</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Backed up assets to: #{tgz_file}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> compress<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Compressing #{path}&quot;</span>
    file_handle = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    compressed_output = YUI.<span style="color:#9900CC;">compress_safe</span> file_handle
    file_handle.<span style="color:#9900CC;">close</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#overwrite the file</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path, <span style="color:#996600;">&quot;w+&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span> file.<span style="color:#CC0066; font-weight:bold;">puts</span> compressed_output <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Wanna use it?<br />
1. Drop it in your lib/tasks folder or wherever your Rakefile looks</p>
<p>You can compress 'everything' or specific files. </p>
<p>If you want to only compress specific Javascript files do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">rake js:generate 
<span style="color: #666666; font-style: italic;"># Edit your config/yui.yml file</span>
rake js:yui</pre></td></tr></table></div>

<p>If you want to compress "everything" (It actually won't compress files that contain ".min.". You can remove the regexp's if you don't like it.)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">rake js:yui <span style="color: #007800;">COMPRESS</span>=ALL</pre></td></tr></table></div>

<p>Yay, now your raking it in (Pun harhar) w/ your web2.0 yui compressed rails app. Woot woot.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI+http://bit.ly/c5ZCBM" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;title=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;t=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;title=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;title=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Excel CSV exporter for ActiveRecord</title>
		<link>http://coryodaniel.com/index.php/2010/01/27/an-excel-csv-exporter-for-activerecord/</link>
		<comments>http://coryodaniel.com/index.php/2010/01/27/an-excel-csv-exporter-for-activerecord/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 02:55:08 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=424</guid>
		<description><![CDATA[This is a mix of two blog posts on exporting ActiveRecord data to CSV.  This explicitly was designed to export stuff so excel wouldn't freak out.
This strips new lines from any string field, set the BOM and converts the encoding to utf16.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require &#34;fastercsv&#34;
require &#34;iconv&#34;
&#160;
# Excel info from: http://blog.plataformatec.com.br/2009/09/exporting-data-to-csv-and-excel-in-your-rails-app/?utm_source=feedburner&#38;utm_medium=feed&#38;utm_campaign=Feed:+PlataformaBlog+(Plataforma+Blog)
# Original post: http://www.brynary.com/2007/4/28/export-activerecords-to-csv
&#160;
# Usage:
#   [...]]]></description>
			<content:encoded><![CDATA[<p>This is a mix of two blog posts on exporting ActiveRecord data to CSV.  This explicitly was designed to export stuff so excel wouldn't freak out.</p>
<p>This strips new lines from any string field, set the BOM and converts the encoding to utf16.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;fastercsv&quot;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">&quot;iconv&quot;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Excel info from: http://blog.plataformatec.com.br/2009/09/exporting-data-to-csv-and-excel-in-your-rails-app/?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed:+PlataformaBlog+(Plataforma+Blog)</span>
<span style="color:#008000; font-style:italic;"># Original post: http://www.brynary.com/2007/4/28/export-activerecords-to-csv</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Usage:</span>
<span style="color:#008000; font-style:italic;">#   Given User is an ActiveRecord model</span>
<span style="color:#008000; font-style:italic;">#   </span>
<span style="color:#008000; font-style:italic;">#   Options for Model.to_csv, Array.to_csv, and ActionController::Base#send_csv</span>
<span style="color:#008000; font-style:italic;"># </span>
<span style="color:#008000; font-style:italic;">#   :only     - Array, trumps :exclude. Only these attributes will be included instead of all attributes (things listed in :methods will still be sent)</span>
<span style="color:#008000; font-style:italic;">#     :only =&gt; [ :id, :name, :created_at ]</span>
<span style="color:#008000; font-style:italic;">#   :methods  - Array, additional methods to evaluate and add to the CSV response. Note: you can send nested method calls</span>
<span style="color:#008000; font-style:italic;">#     :methods =&gt; [ :to_s, :complex_method, &quot;my.method.on.a.related.object&quot;]</span>
<span style="color:#008000; font-style:italic;">#   :exclude  - Array, attributes to exclude from CSV result</span>
<span style="color:#008000; font-style:italic;"># </span>
<span style="color:#008000; font-style:italic;">#  From ActionController</span>
<span style="color:#008000; font-style:italic;">#     send_csv User, :only =&gt; [:first_name, :email, :created_at] #This will do all records</span>
<span style="color:#008000; font-style:italic;">#     send_csv User.all(:conditions =&gt; [&quot;created_at &gt; ?&quot;, some_date]), :only =&gt; [:first_name, :email, :created_at]</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;">#  From ActiveRecord Model</span>
<span style="color:#008000; font-style:italic;">#   User.to_csv :only =&gt; [ :username, :email, :created_at ], :methods =&gt; [ :age, &quot;account.id&quot;] # All users additionally get their related Account#id number</span>
<span style="color:#008000; font-style:italic;">#   User.all(:limit =&gt; 10).to_csv :exclude =&gt; [:password, :birthdate]</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Shortcut for CSV of whole table</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">to_csv</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    find<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_csv</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Get the column headers</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">csv_columns</span><span style="color:#006600; font-weight:bold;">&#40;</span>options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    tmp_columns = <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:only</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:only</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#008000; font-style:italic;">#only trumps exclude</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">content_columns</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>curr_col<span style="color:#006600; font-weight:bold;">|</span> curr_col.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">-</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:exclude</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>curr_col<span style="color:#006600; font-weight:bold;">|</span> curr_col.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    tmp_columns <span style="color:#006600; font-weight:bold;">+</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:methods</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>curr_col<span style="color:#006600; font-weight:bold;">|</span> curr_col.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Record to a row level csv array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> to_csv<span style="color:#006600; font-weight:bold;">&#40;</span>options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">csv_columns</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>curr_col<span style="color:#006600; font-weight:bold;">|</span>
      curr_col = curr_col.<span style="color:#9900CC;">to_s</span>  
&nbsp;
      <span style="color:#008000; font-style:italic;">#Its a chain of method calls, on intermediary nil, just return nil</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> !curr_col.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        col_val = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>curr_col<span style="color:#006600; font-weight:bold;">&#41;</span> 
      <span style="color:#9966CC; font-weight:bold;">else</span>
        col_val = <span style="color:#0000FF; font-weight:bold;">self</span>
        curr_col.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;.&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>curr_method<span style="color:#006600; font-weight:bold;">|</span> col_val = col_val.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>curr_method<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> col_val.<span style="color:#0000FF; font-weight:bold;">nil</span>?<span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      col_val.<span style="color:#CC0066; font-weight:bold;">gsub!</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>, <span style="color:#996600;">&quot; &quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> col_val.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">String</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># Strip newlines, Appease Excel Gods      </span>
      col_val
    <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Convert an array of objects into a CSV String.</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> to_csv<span style="color:#006600; font-weight:bold;">&#40;</span>options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    column_options    = <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#ff3333; font-weight:bold;">:only</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:only</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
      <span style="color:#ff3333; font-weight:bold;">:exclude</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:exclude</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
      <span style="color:#ff3333; font-weight:bold;">:methods</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:methods</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    options = <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#ff3333; font-weight:bold;">:col_sep</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\t</span>&quot;</span> <span style="color:#008000; font-style:italic;"># Appease Excel Gods</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">merge</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> all? <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> e.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:to_csv</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      header_row = first.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">csv_columns</span><span style="color:#006600; font-weight:bold;">&#40;</span>column_options<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_csv</span>
&nbsp;
      content_rows = map <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> 
        <span style="color:#008000; font-style:italic;"># Get all the values of non-excluded rows</span>
        e.<span style="color:#9900CC;">to_csv</span><span style="color:#006600; font-weight:bold;">&#40;</span>column_options<span style="color:#006600; font-weight:bold;">&#41;</span> 
      <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span> 
        <span style="color:#008000; font-style:italic;"># Call to_csv on the array of row-level values, this will join them into a CSV row</span>
        r.<span style="color:#9900CC;">to_csv</span><span style="color:#006600; font-weight:bold;">&#40;</span>column_options<span style="color:#006600; font-weight:bold;">&#41;</span> 
      <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
      <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>header_row<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> content_rows<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">join</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      FasterCSV.<span style="color:#9900CC;">generate_line</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, options<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
&nbsp;
<span style="color:#008000; font-style:italic;"># module for extending ActionController</span>
<span style="color:#9966CC; font-weight:bold;">module</span> ExcelCSVExporter
  BOM = <span style="color:#996600;">&quot;<span style="color:#000099;">\3</span>77<span style="color:#000099;">\3</span>76&quot;</span> <span style="color:#008000; font-style:italic;">#Byte Order Mark, Appease Excel Gods</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> send_csv<span style="color:#006600; font-weight:bold;">&#40;</span>kollection, options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    filename = options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:filename</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> I18n.<span style="color:#9900CC;">l</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>, <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:short</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;.csv&quot;</span>
&nbsp;
    content = kollection.<span style="color:#9900CC;">to_csv</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Appease Excel Gods</span>
    content = BOM <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#CC00FF; font-weight:bold;">Iconv</span>.<span style="color:#9900CC;">conv</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;utf-16le&quot;</span>, <span style="color:#996600;">&quot;utf-8&quot;</span>, content<span style="color:#006600; font-weight:bold;">&#41;</span>
    send_data content, <span style="color:#ff3333; font-weight:bold;">:filename</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> filename    
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">send</span> <span style="color:#ff3333; font-weight:bold;">:include</span>, ExcelCSVExporter</pre></td></tr></table></div>

<p>Wanna use it?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> MyCoolController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    respond_to <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>want<span style="color:#006600; font-weight:bold;">|</span>
      want.<span style="color:#9900CC;">csv</span><span style="color:#006600; font-weight:bold;">&#123;</span> send_csv MyCoolModel.<span style="color:#9900CC;">all</span>, <span style="color:#ff3333; font-weight:bold;">:exclude</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:secret_column</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:methods</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:complex_method</span>, <span style="color:#996600;">&quot;related.table.method&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p align="left"><a class="tt" href="http://twitter.com/home/?status=An+Excel+CSV+exporter+for+ActiveRecord+http://bit.ly/b23yXZ" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/01/27/an-excel-csv-exporter-for-activerecord/&amp;title=An+Excel+CSV+exporter+for+ActiveRecord" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/01/27/an-excel-csv-exporter-for-activerecord/&amp;t=An+Excel+CSV+exporter+for+ActiveRecord" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/01/27/an-excel-csv-exporter-for-activerecord/&amp;title=An+Excel+CSV+exporter+for+ActiveRecord" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/01/27/an-excel-csv-exporter-for-activerecord/&amp;title=An+Excel+CSV+exporter+for+ActiveRecord" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/01/27/an-excel-csv-exporter-for-activerecord/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
