<?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>Thu, 17 Nov 2011 21:18:05 +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>Rails 3 Easy Search forms using SimpleForm and ActiveModel</title>
		<link>http://coryodaniel.com/index.php/2011/11/17/rails-3-easy-search-forms-using-simpleform-and-activemodel/</link>
		<comments>http://coryodaniel.com/index.php/2011/11/17/rails-3-easy-search-forms-using-simpleform-and-activemodel/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 21:15:17 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[active_model]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[simple_form]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=649</guid>
		<description><![CDATA[For the sites I'm currently working on I have search pages all over the place for different resources, especially on the backend. In the past I would forego using form builders and just do some HTML to make a form GET the data. I hate HTML.
So I did some poking around to see what I [...]]]></description>
			<content:encoded><![CDATA[<p>For the sites I'm currently working on I have search pages all over the place for different resources, especially on the backend. In the past I would forego using form builders and just do some HTML to make a form GET the data. I hate HTML.</p>
<p>So I did some poking around to see what I could do to DRY up my time on creating these search forms. My first thought was to create a Search class and use active model, but it sucked because I everytime I had to create a new form I had to come into the search class and add a bunch of attr_accessors for all of the search fields. That sucks.</p>
<p>So I decided to mix it up with OpenStruct and ActiveModel</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'ostruct'</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Search <span style="color:#006600; font-weight:bold;">&lt;</span> OpenStruct
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::Validations</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::Conversion</span>
  extend  <span style="color:#6666ff; font-weight:bold;">ActiveModel::Naming</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<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;">super</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> persisted? ; <span style="color:#0000FF; font-weight:bold;">false</span> ; <span style="color:#9966CC; font-weight:bold;">end</span>;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now I use this one model for searching of all of my resources.</p>
<p>I create the form super easily with SimpleForm:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">= simple_form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@search, url: my_cool_path<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, html:<span style="color:#006600; font-weight:bold;">&#123;</span>method: <span style="color:#ff3333; font-weight:bold;">:get</span><span style="color:#006600; font-weight:bold;">&#125;</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:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:text</span>
  = f.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#ff3333; font-weight:bold;">:category</span>,  <span style="color:#6666ff; font-weight:bold;">Product::CATEGORIES</span>
  = f.<span style="color:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:minimum_price</span>
  = f.<span style="color:#9900CC;">input</span> <span style="color:#ff3333; font-weight:bold;">:maximum_price</span>
  = f.<span style="color:#9900CC;">submit</span> <span style="color:#ff3333; font-weight:bold;">:search</span></pre></div></div>

<p>In your controller you are going to get a params[:search] hash with :text, :category, :minimum_price, :maximum_price.</p>
<p>Want to persist that search across page reloads?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
  before_filter <span style="color:#ff3333; font-weight:bold;">:persist_search</span>
  protected
  <span style="color:#9966CC; font-weight:bold;">def</span> persist_search
    <span style="color:#0066ff; font-weight:bold;">@search</span> = Search.<span style="color:#9900CC;">new</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:search</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now you'll have a Search object in all of your requests with your user's search params. Use it as you will to return them the resources they are looking for.</p>
<p>*Side note*<br />
I use the above logic for all of my resources, until one of my resources has a requirement like a validation. Then I break it into something like FlightSearch:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'ostruct'</span>
<span style="color:#008000; font-style:italic;"># ostruct for that lazy goodness</span>
<span style="color:#9966CC; font-weight:bold;">class</span> FlightSearch <span style="color:#006600; font-weight:bold;">&lt;</span> OpenStruct
  <span style="color:#008000; font-style:italic;"># Get activemodel in there for all its cool functionality like model name, pluralization, validation, yada yada yada</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::Validations</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveModel::Conversion</span>
  extend  <span style="color:#6666ff; font-weight:bold;">ActiveModel::Naming</span>
&nbsp;
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:origin_airport</span>, <span style="color:#ff3333; font-weight:bold;">:destination_airport</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># you can do cool things here, just make sure you call super so ostruct </span>
  <span style="color:#008000; font-style:italic;"># takes all those hash params and makes them methods</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<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;">super</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># ActiveModel needs to know that this wasn't persisted.</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> persisted? ; <span style="color:#0000FF; font-weight:bold;">false</span> ; <span style="color:#9966CC; font-weight:bold;">end</span>;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p align="left"><a class="tt" href="http://twitter.com/home/?status=Rails+3+Easy+Search+forms+using+SimpleForm+and+ActiveModel+http://coryodaniel.com/?p=649" 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/2011/11/17/rails-3-easy-search-forms-using-simpleform-and-activemodel/&amp;title=Rails+3+Easy+Search+forms+using+SimpleForm+and+ActiveModel" 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/2011/11/17/rails-3-easy-search-forms-using-simpleform-and-activemodel/&amp;t=Rails+3+Easy+Search+forms+using+SimpleForm+and+ActiveModel" 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/2011/11/17/rails-3-easy-search-forms-using-simpleform-and-activemodel/&amp;title=Rails+3+Easy+Search+forms+using+SimpleForm+and+ActiveModel" 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/2011/11/17/rails-3-easy-search-forms-using-simpleform-and-activemodel/&amp;title=Rails+3+Easy+Search+forms+using+SimpleForm+and+ActiveModel" 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/2011/11/17/rails-3-easy-search-forms-using-simpleform-and-activemodel/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using devise&#8217;s scoped url helpers in Cucumber; new_registration_path, new_session_path</title>
		<link>http://coryodaniel.com/index.php/2011/10/07/using-devises-scoped-url-helpers-in-cucumber-new_registration_path-new_session_path/</link>
		<comments>http://coryodaniel.com/index.php/2011/10/07/using-devises-scoped-url-helpers-in-cucumber-new_registration_path-new_session_path/#comments</comments>
		<pubDate>Fri, 07 Oct 2011 20:40:36 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[BDD]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[devise]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=642</guid>
		<description><![CDATA[You can use devise's scoped url helpers from cucumber, but you have to use them by their real Url Helper method names:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module NavigationHelpers
  def path_to&#40;page_name&#41;
    case page_name
    when /the sign up page/
      # dont use the helper's helper
      [...]]]></description>
			<content:encoded><![CDATA[<p>You can use devise's scoped url helpers from cucumber, but you have to use them by their real Url Helper method names:</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> NavigationHelpers
  <span style="color:#9966CC; font-weight:bold;">def</span> path_to<span style="color:#006600; font-weight:bold;">&#40;</span>page_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> page_name
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>the sign up page<span style="color:#006600; font-weight:bold;">/</span>
      <span style="color:#008000; font-style:italic;"># dont use the helper's helper</span>
      <span style="color:#008000; font-style:italic;"># new_registration_path(:user)      </span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># use the underlying url helper</span>
      new_user_registration_path
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>the sign <span style="color:#9966CC; font-weight:bold;">in</span> page<span style="color:#006600; font-weight:bold;">/</span>
      <span style="color:#008000; font-style:italic;"># dont use the helper's helper</span>
      <span style="color:#008000; font-style:italic;"># new_session_path(:user)      </span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># use the underlying url helper</span>
      new_user_session_path
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#008000; font-style:italic;">#...yada, yada</span></pre></td></tr></table></div>

<p align="left"><a class="tt" href="http://twitter.com/home/?status=Using+devise%E2%80%99s+scoped+url+helpers+in+Cucumber%3B+new_registration_path%2C+new_session_path+http://coryodaniel.com/?p=642" 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/2011/10/07/using-devises-scoped-url-helpers-in-cucumber-new_registration_path-new_session_path/&amp;title=Using+devise%E2%80%99s+scoped+url+helpers+in+Cucumber%3B+new_registration_path%2C+new_session_path" 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/2011/10/07/using-devises-scoped-url-helpers-in-cucumber-new_registration_path-new_session_path/&amp;t=Using+devise%E2%80%99s+scoped+url+helpers+in+Cucumber%3B+new_registration_path%2C+new_session_path" 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/2011/10/07/using-devises-scoped-url-helpers-in-cucumber-new_registration_path-new_session_path/&amp;title=Using+devise%E2%80%99s+scoped+url+helpers+in+Cucumber%3B+new_registration_path%2C+new_session_path" 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/2011/10/07/using-devises-scoped-url-helpers-in-cucumber-new_registration_path-new_session_path/&amp;title=Using+devise%E2%80%99s+scoped+url+helpers+in+Cucumber%3B+new_registration_path%2C+new_session_path" 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/2011/10/07/using-devises-scoped-url-helpers-in-cucumber-new_registration_path-new_session_path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>cucumber.yml was found, but could not be parsed. Please refer to cucumber&#8217;s documentation on correct profile usage.</title>
		<link>http://coryodaniel.com/index.php/2011/04/12/cucumber-yml-was-found-but-could-not-be-parsed-please-refer-to-cucumbers-documentation-on-correct-profile-usage/</link>
		<comments>http://coryodaniel.com/index.php/2011/04/12/cucumber-yml-was-found-but-could-not-be-parsed-please-refer-to-cucumbers-documentation-on-correct-profile-usage/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 16:24:33 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rerun]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=623</guid>
		<description><![CDATA[Yeah, I got that message today and wasted about 30 minutes of my life.
I hadn't changed my cuke yaml file since I started work on the app.
I tried:
* upgrading cucumber (FAIL)
* using another yaml file (FAIL)
* upgrading gherkin (FAIL)
* hard reseting the branch I was on (FAIL)
Turned out to just be a bum rerun.txt file. [...]]]></description>
			<content:encoded><![CDATA[<p>Yeah, I got that message today and wasted about 30 minutes of my life.</p>
<p>I hadn't changed my cuke yaml file since I started work on the app.</p>
<p>I tried:<br />
* upgrading cucumber (FAIL)<br />
* using another yaml file (FAIL)<br />
* upgrading gherkin (FAIL)<br />
* hard reseting the branch I was on (FAIL)</p>
<p>Turned out to just be a bum rerun.txt file. So I deleted it.</p>
<p>I need a refund on that 30 minutes.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=cucumber.yml+was+found%2C+but+could+not+be+parsed.+Please+refer+to+cucumber%E2%80%99s+documentation+on+correct+profile+usage.+http://coryodaniel.com/?p=623" 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/2011/04/12/cucumber-yml-was-found-but-could-not-be-parsed-please-refer-to-cucumbers-documentation-on-correct-profile-usage/&amp;title=cucumber.yml+was+found%2C+but+could+not+be+parsed.+Please+refer+to+cucumber%E2%80%99s+documentation+on+correct+profile+usage." 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/2011/04/12/cucumber-yml-was-found-but-could-not-be-parsed-please-refer-to-cucumbers-documentation-on-correct-profile-usage/&amp;t=cucumber.yml+was+found%2C+but+could+not+be+parsed.+Please+refer+to+cucumber%E2%80%99s+documentation+on+correct+profile+usage." 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/2011/04/12/cucumber-yml-was-found-but-could-not-be-parsed-please-refer-to-cucumbers-documentation-on-correct-profile-usage/&amp;title=cucumber.yml+was+found%2C+but+could+not+be+parsed.+Please+refer+to+cucumber%E2%80%99s+documentation+on+correct+profile+usage." 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/2011/04/12/cucumber-yml-was-found-but-could-not-be-parsed-please-refer-to-cucumbers-documentation-on-correct-profile-usage/&amp;title=cucumber.yml+was+found%2C+but+could+not+be+parsed.+Please+refer+to+cucumber%E2%80%99s+documentation+on+correct+profile+usage." 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/2011/04/12/cucumber-yml-was-found-but-could-not-be-parsed-please-refer-to-cucumbers-documentation-on-correct-profile-usage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Biting myself on the ass with Cucumber</title>
		<link>http://coryodaniel.com/index.php/2011/03/23/biting-myself-on-the-ass-with-cucumber/</link>
		<comments>http://coryodaniel.com/index.php/2011/03/23/biting-myself-on-the-ass-with-cucumber/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 03:30:17 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[BDD]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=605</guid>
		<description><![CDATA[So I spent a few hours today biting my own ass with cucumber.
If you are ever trying to "see" stuff in your page and notice that it has strangely disappeared, like oh say, your flash messages or validation errors. You may want to pay close attention if you are testing the page you are on [...]]]></description>
			<content:encoded><![CDATA[<p>So I spent a few hours today biting my own ass with cucumber.</p>
<p>If you are ever trying to "see" stuff in your page and notice that it has strangely disappeared, like oh say, your flash messages or validation errors. You may want to pay close attention if you are testing the page you are on just before hand.</p>
<p>I found out a sore lesson today, there is a big difference between:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="plain" style="font-family:monospace;">Then I am on the sign up page
And I should see &quot;Email can't be blank&quot;</pre></td></tr></table></div>

<p>And</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="plain" style="font-family:monospace;">Then I should be on the sign up page
And I should see &quot;Email can't be blank&quot;</pre></td></tr></table></div>

<p>The difference here? "The I am on" reloads the page. I didn't know that. So I was <i>trying</i> to make sure I was on the right page, then checking for errors, etc. The page would reload, all my errors would be gone, and my feature was failing.</p>
<p>"Then I should be on" tests that the url matches the one you are on.</p>
<p>The more you know.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Biting+myself+on+the+ass+with+Cucumber+http://coryodaniel.com/?p=605" 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/2011/03/23/biting-myself-on-the-ass-with-cucumber/&amp;title=Biting+myself+on+the+ass+with+Cucumber" 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/2011/03/23/biting-myself-on-the-ass-with-cucumber/&amp;t=Biting+myself+on+the+ass+with+Cucumber" 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/2011/03/23/biting-myself-on-the-ass-with-cucumber/&amp;title=Biting+myself+on+the+ass+with+Cucumber" 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/2011/03/23/biting-myself-on-the-ass-with-cucumber/&amp;title=Biting+myself+on+the+ass+with+Cucumber" 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/2011/03/23/biting-myself-on-the-ass-with-cucumber/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HamlburgerHelper sets the Table &#8211; Easily create and display standard tables in Rails</title>
		<link>http://coryodaniel.com/index.php/2011/02/16/hamlburgerhelper-sets-the-table-easily-create-and-display-standard-tables-in-rails/</link>
		<comments>http://coryodaniel.com/index.php/2011/02/16/hamlburgerhelper-sets-the-table-easily-create-and-display-standard-tables-in-rails/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 02:04:02 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[haml]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[tables]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=580</guid>
		<description><![CDATA[I create tables a lot in my back end for display information. 
I posted a table display/create helper method on dzone that easy to use but has a ton of options.
This is the helper I currently use on my admin backend pages to create the simplest table to a pretty complex table.
Here are all of [...]]]></description>
			<content:encoded><![CDATA[<p>I create tables a lot in my back end for display information. </p>
<p>I posted a <a href="http://snippets.dzone.com/posts/show/12951">table display/create helper method</a> on dzone that easy to use but has a ton of options.</p>
<p>This is the helper I currently use on my admin backend pages to create the simplest table to a pretty complex table.</p>
<p>Here are all of the available options and their defaults:</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">      <span style="color:#ff3333; font-weight:bold;">:table_class</span>        <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'display_grid'</span>, <span style="color:#008000; font-style:italic;"># CSS class name of the table</span>
      <span style="color:#ff3333; font-weight:bold;">:table_id</span>           <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;display_grid_#{objects.first.class.to_s.underscore.pluralize}&quot;</span>, <span style="color:#008000; font-style:italic;"># CSS ID of the table</span>
      <span style="color:#ff3333; font-weight:bold;">:heading_class</span>      <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'display_grid_heading'</span>, <span style="color:#008000; font-style:italic;"># CSS class name of the first TR (one containing TH)</span>
      <span style="color:#ff3333; font-weight:bold;">:heading_id</span>         <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;display_grid_heading_#{objects.first.class.to_s.underscore.pluralize}&quot;</span>, <span style="color:#008000; font-style:italic;"># CSS ID of the first TR</span>
      <span style="color:#ff3333; font-weight:bold;">:th_class</span>           <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'display_grid_th'</span>, <span style="color:#008000; font-style:italic;"># CSS class for all TH elements</span>
      <span style="color:#ff3333; font-weight:bold;">:tr_class</span>           <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'display_grid_tr'</span>, <span style="color:#008000; font-style:italic;"># CSS class for all TR elements</span>
      <span style="color:#ff3333; font-weight:bold;">:td_class</span>           <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'display_grid_td'</span>,  <span style="color:#008000; font-style:italic;"># CSS class for all TD elements</span>
      <span style="color:#ff3333; font-weight:bold;">:even_odd</span>           <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#008000; font-style:italic;"># Should even/odd classes be added</span>
      <span style="color:#ff3333; font-weight:bold;">:format_date</span>        <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#008000; font-style:italic;">#nil | lambda{|datetime|} # Time formatter (receives date object, expects string)</span>
      <span style="color:#ff3333; font-weight:bold;">:numeric_td_class</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'numeric'</span>, <span style="color:#008000; font-style:italic;"># CSS class for any TDs containing a number</span>
      <span style="color:#ff3333; font-weight:bold;">:date_td_class</span>      <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'date'</span>, <span style="color:#008000; font-style:italic;"># CSS class for any TDs containing something date-like</span>
      <span style="color:#ff3333; font-weight:bold;">:string_td_class</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'string'</span>, <span style="color:#008000; font-style:italic;"># CSS class for any TDs containing a string</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># Any of this will create an 'Actions' heading, the lambdas receive the object, and expect a string</span>
      <span style="color:#ff3333; font-weight:bold;">:show_action</span>        <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#008000; font-style:italic;"># lambda{|object| link_to '', my_path(object)}</span>
      <span style="color:#ff3333; font-weight:bold;">:edit_action</span>        <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#008000; font-style:italic;"># lambda{|object|} </span>
      <span style="color:#ff3333; font-weight:bold;">:destroy_action</span>     <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#008000; font-style:italic;"># lambda{|object|}</span></pre></td></tr></table></div>

<p>Using it is pretty easy, just drop it in your app/helpers folder and...</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">display_grid<span style="color:#006600; font-weight:bold;">&#40;</span>User.<span style="color:#9900CC;">all</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#ff3333; font-weight:bold;">:show_action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">lambda</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>user<span style="color:#006600; font-weight:bold;">|</span> link_to user.<span style="color:#9900CC;">display_name</span>, public_user_path<span style="color:#006600; font-weight:bold;">&#40;</span>user<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#ff3333; font-weight:bold;">:table_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'cool_users'</span>
<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Its pretty simple. It'll dump out a table, and then you can go and style it using the CSS classes/ids above.</p>
<p>Woot, back end DRY.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=HamlburgerHelper+sets+the+Table+%E2%80%93+Easily+create+and+display+standard+tables+in+Rails+http://coryodaniel.com/?p=580" 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/2011/02/16/hamlburgerhelper-sets-the-table-easily-create-and-display-standard-tables-in-rails/&amp;title=HamlburgerHelper+sets+the+Table+%E2%80%93+Easily+create+and+display+standard+tables+in+Rails" 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/2011/02/16/hamlburgerhelper-sets-the-table-easily-create-and-display-standard-tables-in-rails/&amp;t=HamlburgerHelper+sets+the+Table+%E2%80%93+Easily+create+and+display+standard+tables+in+Rails" 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/2011/02/16/hamlburgerhelper-sets-the-table-easily-create-and-display-standard-tables-in-rails/&amp;title=HamlburgerHelper+sets+the+Table+%E2%80%93+Easily+create+and+display+standard+tables+in+Rails" 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/2011/02/16/hamlburgerhelper-sets-the-table-easily-create-and-display-standard-tables-in-rails/&amp;title=HamlburgerHelper+sets+the+Table+%E2%80%93+Easily+create+and+display+standard+tables+in+Rails" 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/2011/02/16/hamlburgerhelper-sets-the-table-easily-create-and-display-standard-tables-in-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>methods_like helper for finding ruby methods</title>
		<link>http://coryodaniel.com/index.php/2010/11/02/methods_like-helper-for-finding-ruby-methods/</link>
		<comments>http://coryodaniel.com/index.php/2010/11/02/methods_like-helper-for-finding-ruby-methods/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 01:06:05 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[console irb]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[Object]]></category>
		<category><![CDATA[padrino]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=543</guid>
		<description><![CDATA[Here is a little gem I have in my ~/.irbrc I use it a lot when debugging code. I tend to use call #methods a lot on objects to figure out how they quack. 
Sometimes I can remember kinda what a method's name is I want to use, but doing something like the following can [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a little gem I have in my ~/.irbrc I use it a lot when debugging code. I tend to use call #methods a lot on objects to figure out how they quack. </p>
<p>Sometimes I can remember kinda what a method's name is I want to use, but doing something like the following can generate a huge amount of output especially when working with ActiveRecord</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;">my_object.<span style="color:#9900CC;">methods</span>.<span style="color:#9900CC;">sort</span></pre></td></tr></table></div>

<p>So I threw together this little method that adds to ruby's base Object class</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> methods_like<span style="color:#006600; font-weight:bold;">&#40;</span>pat<span style="color:#006600; font-weight:bold;">&#41;</span>
    pattern =  pat.<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:#006600; font-weight:bold;">/</span><span style="color:#008000; font-style:italic;">#{pat}/ : pat</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">methods</span>.<span style="color:#9900CC;">sort</span>.<span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span>
      m =~ pattern
    <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>Now from IRB, Rails console, or Padrino console you can do things like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_collection.<span style="color:#9900CC;">methods_like</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^update<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; [ array of method names that start with update ]</span>
user.<span style="color:#9900CC;">methods_like</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;name&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; [ :first_name, :last_name, :etc ]</span>
<span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">methods_like</span> <span style="color:#996600;">&quot;sort&quot;</span> <span style="color:#008000; font-style:italic;">#=&gt; [&quot;sort&quot;, &quot;sort!&quot;, &quot;sort_by&quot;]</span></pre></div></div>

<p>Its a handy little snippet when you can't remember an exact method name, and generally for me works faster than googling for it. Toss it in your ~/.irbrc and tell me what you think.</p>
<p><strong>UPDATE</strong><br />
I added some more functionality for getting additional methods (private, protected, singleton):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#008000; font-style:italic;"># pattern - string or pattern to match</span>
  <span style="color:#008000; font-style:italic;"># access  - :public, :private, :protected, :singleton, :all</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> methods_like<span style="color:#006600; font-weight:bold;">&#40;</span>pattern, access = <span style="color:#ff3333; font-weight:bold;">:public</span>, details = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>    
    master_method_list = <span style="color:#9966CC; font-weight:bold;">case</span> access
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:public</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">methods</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:private</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">private_methods</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:protected</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">protected_methods</span> 
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:singleton</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">singleton_methods</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:all</span>
      <span style="color:#006600; font-weight:bold;">&#40;</span>
        <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">methods</span>            <span style="color:#006600; font-weight:bold;">+</span>
        <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">private_methods</span>    <span style="color:#006600; font-weight:bold;">+</span>
        <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">protected_methods</span>  <span style="color:#006600; font-weight:bold;">+</span>
        <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">singleton_methods</span>
      <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">uniq</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    matched_method_list = master_method_list.<span style="color:#9900CC;">sort</span>.<span style="color:#CC0066; font-weight:bold;">select</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>m<span style="color:#006600; font-weight:bold;">|</span> m =~ <span style="color:#006600; font-weight:bold;">&#40;</span> pattern.<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:#006600; font-weight:bold;">/</span><span style="color:#008000; font-style:italic;">#{pattern}/ : pattern ) }</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> !details
      matched_method_list
    <span style="color:#9966CC; font-weight:bold;">else</span>
      details_list = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
      matched_method_list.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>current_method<span style="color:#006600; font-weight:bold;">|</span>
        details_list<span style="color:#006600; font-weight:bold;">&#91;</span>current_method<span style="color:#006600; font-weight:bold;">&#93;</span> = method_details<span style="color:#006600; font-weight:bold;">&#40;</span>current_method<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      details_list
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Returns details about method</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> method_details<span style="color:#006600; font-weight:bold;">&#40;</span>current_method<span style="color:#006600; font-weight:bold;">&#41;</span>
    current_method = current_method.<span style="color:#9900CC;">to_s</span>
    access_level = <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">methods</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>current_method<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#ff3333; font-weight:bold;">:public</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">private_methods</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>current_method<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#ff3333; font-weight:bold;">:private</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">protected_methods</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>current_method<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#ff3333; font-weight:bold;">:protected</span>
    <span style="color:#9966CC; font-weight:bold;">elsif</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">singleton_methods</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>current_method<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#ff3333; font-weight:bold;">:singleton</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#ff3333; font-weight:bold;">:owner</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_method<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">owner</span>,
      <span style="color:#ff3333; font-weight:bold;">:arity</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_method<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">arity</span>,
      <span style="color:#ff3333; font-weight:bold;">:receiver</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">method</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_method<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">receiver</span>,
      <span style="color:#ff3333; font-weight:bold;">:access</span>   <span style="color:#006600; font-weight:bold;">=&gt;</span> access_level
    <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></div></div>

<p>Now you can do:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">variable.<span style="color:#9900CC;">methods_like</span> pattern, <span style="color:#ff3333; font-weight:bold;">:private</span> <span style="color:#008000; font-style:italic;">#=&gt; Array of private methods with pattern</span>
variable.<span style="color:#9900CC;">methods_like</span> pattern, <span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#008000; font-style:italic;">#=&gt; Hash of methods with additional details</span></pre></div></div>

<p>If you pass 'true' for details you will get a hash that looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span>.<span style="color:#9900CC;">methods_like</span> <span style="color:#996600;">&quot;sort&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#008000; font-style:italic;">#=&gt;</span>
<span style="color:#006600; font-weight:bold;">&#123;</span>
  <span style="color:#996600;">&quot;sort_by&quot;</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:owner<span style="color:#006600; font-weight:bold;">=&gt;</span>Enumerable, <span style="color:#ff3333; font-weight:bold;">:access</span><span style="color:#006600; font-weight:bold;">=&gt;</span>:public, <span style="color:#ff3333; font-weight:bold;">:receiver</span><span style="color:#006600; font-weight:bold;">=&gt;</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;">:arity</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#125;</span>, 
  <span style="color:#996600;">&quot;sort!&quot;</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:owner<span style="color:#006600; font-weight:bold;">=&gt;</span>Array, <span style="color:#ff3333; font-weight:bold;">:access</span><span style="color:#006600; font-weight:bold;">=&gt;</span>:public, <span style="color:#ff3333; font-weight:bold;">:receiver</span><span style="color:#006600; font-weight:bold;">=&gt;</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;">:arity</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#125;</span>, 
  <span style="color:#996600;">&quot;sort&quot;</span>     <span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006600; font-weight:bold;">&#123;</span>:owner<span style="color:#006600; font-weight:bold;">=&gt;</span>Array, <span style="color:#ff3333; font-weight:bold;">:access</span><span style="color:#006600; font-weight:bold;">=&gt;</span>:public, <span style="color:#ff3333; font-weight:bold;">:receiver</span><span style="color:#006600; font-weight:bold;">=&gt;</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;">:arity</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Access options available are :public, :private, :singleton, :protected, :all</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=methods_like+helper+for+finding+ruby+methods+http://coryodaniel.com/?p=543" 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/11/02/methods_like-helper-for-finding-ruby-methods/&amp;title=methods_like+helper+for+finding+ruby+methods" 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/11/02/methods_like-helper-for-finding-ruby-methods/&amp;t=methods_like+helper+for+finding+ruby+methods" 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/11/02/methods_like-helper-for-finding-ruby-methods/&amp;title=methods_like+helper+for+finding+ruby+methods" 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/11/02/methods_like-helper-for-finding-ruby-methods/&amp;title=methods_like+helper+for+finding+ruby+methods" 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/11/02/methods_like-helper-for-finding-ruby-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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://coryodaniel.com/?p=495" 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>6</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://coryodaniel.com/?p=466" 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>2</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://coryodaniel.com/?p=462" 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://coryodaniel.com/?p=424" 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>

