<?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; ruby</title>
	<atom:link href="http://coryodaniel.com/index.php/tag/ruby/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>0</slash:comments>
		</item>
		<item>
		<title>Backdateable mongoid models &#8211; a simple little module I use a lot</title>
		<link>http://coryodaniel.com/index.php/2011/03/24/backdateable-mongoid-models-a-simple-little-module-i-use-a-lot/</link>
		<comments>http://coryodaniel.com/index.php/2011/03/24/backdateable-mongoid-models-a-simple-little-module-i-use-a-lot/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 16:50:21 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[dates]]></category>
		<category><![CDATA[mongoid]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=607</guid>
		<description><![CDATA[I have a lot of models in one of the projects I am currently working on that need to be backdate-able. This is just a simple module I add to any class that needs to support it. It could be easily tweaked for ActiveRecord.

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
module Backdateable
  module ClassMethods;end;
&#160;
  def self.included&#40;base&#41;
    base.extend&#40;ClassMethods&#41;
 [...]]]></description>
			<content:encoded><![CDATA[<p>I have a lot of models in one of the projects I am currently working on that need to be backdate-able. This is just a simple module I add to any class that needs to support it. It could be easily tweaked for ActiveRecord.</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Backdateable
  <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods;end;
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>ClassMethods<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;">#allows this to be an accessor on the front end, although, </span>
    <span style="color:#008000; font-style:italic;"># if the date is left blank, it gets set properly in before</span>
    base.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:field</span>, <span style="color:#ff3333; font-weight:bold;">:backdated</span>, type: Boolean, default: <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:field</span>, <span style="color:#ff3333; font-weight:bold;">:recorded_at</span>, type: <span style="color:#CC00FF; font-weight:bold;">Time</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> base.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:default_scope</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      base.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:default_scope</span>, <span style="color:#ff3333; font-weight:bold;">:asc</span>, <span style="color:#ff3333; font-weight:bold;">:recorded_at</span><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;"># Changing this to before_create only allows backdating on creation...</span>
    base.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:before_save</span>, <span style="color:#ff3333; font-weight:bold;">:set_recorded_at</span><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;"># defaulting recorded at to created_at lets this be sortable by recorded at</span>
  <span style="color:#008000; font-style:italic;"># we override backdated here in case the date was originally left blank</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> set_recorded_at
    <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:recorded_at</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:created_at</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:backdated</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:recorded_at</span><span style="color:#006600; font-weight:bold;">&#93;</span> != <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:created_at</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">true</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 you can do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Measurement
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Mongoid::Document</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">Mongoid::Timestamps</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> Backdateable
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Measurement.<span style="color:#9900CC;">create</span>
<span style="color:#008000; font-style:italic;"># =&gt; created_at &amp; recorded_at are set to the same, backdated is set to false</span>
&nbsp;
Measurement.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:recorded_at</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Time</span>.<span style="color:#9900CC;">now</span>.<span style="color:#9900CC;">advance</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:days</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">7</span><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;"># =&gt; recorded_at is set to the past, backdated is set to true</span></pre></td></tr></table></div>

<p>Why do I default recorded at to created at? So I can sort by recorded_at.</p>
<p>Done, son.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Backdateable+mongoid+models+%E2%80%93+a+simple+little+module+I+use+a+lot+http://coryodaniel.com/?p=607" 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/24/backdateable-mongoid-models-a-simple-little-module-i-use-a-lot/&amp;title=Backdateable+mongoid+models+%E2%80%93+a+simple+little+module+I+use+a+lot" 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/24/backdateable-mongoid-models-a-simple-little-module-i-use-a-lot/&amp;t=Backdateable+mongoid+models+%E2%80%93+a+simple+little+module+I+use+a+lot" 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/24/backdateable-mongoid-models-a-simple-little-module-i-use-a-lot/&amp;title=Backdateable+mongoid+models+%E2%80%93+a+simple+little+module+I+use+a+lot" 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/24/backdateable-mongoid-models-a-simple-little-module-i-use-a-lot/&amp;title=Backdateable+mongoid+models+%E2%80%93+a+simple+little+module+I+use+a+lot" 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/24/backdateable-mongoid-models-a-simple-little-module-i-use-a-lot/feed/</wfw:commentRss>
		<slash:comments>0</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>Matching Unicode characters and strings with Regexp in Ruby 1.9.2</title>
		<link>http://coryodaniel.com/index.php/2011/02/22/matching-unicode-characters-and-strings-with-regexp-in-ruby-1-9-2/</link>
		<comments>http://coryodaniel.com/index.php/2011/02/22/matching-unicode-characters-and-strings-with-regexp-in-ruby-1-9-2/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 22:13:38 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[matching]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby1.9]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=592</guid>
		<description><![CDATA[So I've been working on a revamp of a project (still secret, ohhhh ahhh) and I needed Unicode support for multiple languages. I whipped up a pretty simple regexp and found that it didn't work for my test case.
So I've got something equivalent to this running inside some rails rspec test. And my test is [...]]]></description>
			<content:encoded><![CDATA[<p>So I've been working on a revamp of a project (still secret, ohhhh ahhh) and I needed Unicode support for multiple languages. I whipped up a pretty simple regexp and found that it didn't work for my test case.</p>
<p>So I've got something equivalent to this running inside some rails rspec test. And my test is failing because I'm expecting some russian words.</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;">greeting  = <span style="color:#996600;">&quot;Добрый день.&quot;</span>
greeting.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">/</span>\w<span style="color:#006600; font-weight:bold;">/</span>u <span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; []</span></pre></td></tr></table></div>

<p>WTF.</p>
<p>I double check:</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;">greeting  = <span style="color:#996600;">&quot;Добрый день.&quot;</span>
pattern = <span style="color:#006600; font-weight:bold;">/</span>\w<span style="color:#006600; font-weight:bold;">/</span>u
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> greeting.<span style="color:#9900CC;">encoding</span>
<span style="color:#008000; font-style:italic;"># =&gt; UTF-8</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> pattern.<span style="color:#9900CC;">encoding</span>
<span style="color:#008000; font-style:italic;"># =&gt; UTF-8</span></pre></td></tr></table></div>

<p>Hell, I even have "# encoding: UTF-8" at the top of the file.<br />
Fuuuuuuuuuuuuuuuuuuuuuuu.</p>
<p>After some google and some irc'ing I find <a href="http://www.ruby-forum.com/topic/210770">this answer</a>, which solves the problem and allows for matches of unicode characters in ruby 1.9.2, but I still can't figure out what the 'p' stands for. I'm guessing "please". The problem is apparently there was an issue with \w matching unicode characters, so \w specifically only works with ASCII.</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;">greeting  = <span style="color:#996600;">&quot;Добрый день.&quot;</span>
greeting.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">/</span>\<span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#123;</span>Word<span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">/</span>u <span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt; [&quot;Добрый&quot;, &quot;день&quot;]</span></pre></td></tr></table></div>

<p>Wööt.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Matching+Unicode+characters+and+strings+with+Regexp+in+Ruby+1.9.2+http://coryodaniel.com/?p=592" 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/22/matching-unicode-characters-and-strings-with-regexp-in-ruby-1-9-2/&amp;title=Matching+Unicode+characters+and+strings+with+Regexp+in+Ruby+1.9.2" 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/22/matching-unicode-characters-and-strings-with-regexp-in-ruby-1-9-2/&amp;t=Matching+Unicode+characters+and+strings+with+Regexp+in+Ruby+1.9.2" 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/22/matching-unicode-characters-and-strings-with-regexp-in-ruby-1-9-2/&amp;title=Matching+Unicode+characters+and+strings+with+Regexp+in+Ruby+1.9.2" 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/22/matching-unicode-characters-and-strings-with-regexp-in-ruby-1-9-2/&amp;title=Matching+Unicode+characters+and+strings+with+Regexp+in+Ruby+1.9.2" 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/22/matching-unicode-characters-and-strings-with-regexp-in-ruby-1-9-2/feed/</wfw:commentRss>
		<slash:comments>0</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://bit.ly/aK660I" 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>Ruby 1.9 Date#strftime adds a space on %b</title>
		<link>http://coryodaniel.com/index.php/2010/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/</link>
		<comments>http://coryodaniel.com/index.php/2010/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/#comments</comments>
		<pubDate>Fri, 07 May 2010 19:15:24 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby19]]></category>
		<category><![CDATA[strftime]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=504</guid>
		<description><![CDATA[So, I had a spec failing in my integration suite and I couldn't figure out what the hell it was - EVERYTHING LOOKED LEGIT. I even logged into the site, and visually verified it. Whats the dilly?
I upgraded to ruby 1.9 from ruby 1.8 and apparently, when doing strftime on date, you get a free [...]]]></description>
			<content:encoded><![CDATA[<p>So, I had a spec failing in my integration suite and I couldn't figure out what the hell it was - EVERYTHING LOOKED LEGIT. I even logged into the site, and visually verified it. Whats the dilly?</p>
<p>I upgraded to ruby 1.9 from ruby 1.8 and apparently, when doing strftime on date, you get a free whitespace character with "%b"</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:#008000; font-style:italic;"># Ruby 1.8</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:006:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>.<span style="color:#9900CC;">strftime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%b %e, %Y&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;May 7, 2010&quot;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:007:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>.<span style="color:#9900CC;">strftime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%b %e, %Y&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;May  7, 2010&quot;</span> <span style="color:#008000; font-style:italic;"># TWO SPACES</span></pre></td></tr></table></div>

<p>Super dumb - wasted a good 10 minutes of my day because I could see the diff between:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;May  7, 2010&quot;</span>
<span style="color:#996600;">&quot;May 7, 2010&quot;</span></pre></td></tr></table></div>

<p>In the middle of a web page full of other content.</p>
<p>Why the freebee space character on the %b? Dunno. I just smashed my stftime statement together and my specs are rolling.</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:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">today</span>.<span style="color:#9900CC;">strftime</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;%b%e, %Y&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Uh, yeah - this is my bug report.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b+http://bit.ly/deWu7e" 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/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/&amp;title=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b" 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/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/&amp;t=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b" 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/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/&amp;title=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b" 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/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/&amp;title=Ruby+1.9+Date%23strftime+adds+a+space+on+%25b" 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/05/07/ruby-1-9-datestrftime-adds-a-space-on-b/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Padrino, Compass, and Sass &#8211; Working happily via Ian Serlin</title>
		<link>http://coryodaniel.com/index.php/2010/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/</link>
		<comments>http://coryodaniel.com/index.php/2010/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 22:22:59 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[compass]]></category>
		<category><![CDATA[padrino]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sass]]></category>
		<category><![CDATA[sinatra]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=500</guid>
		<description><![CDATA[My cohort, Ian Serlin, discovered this.  In a project we are working on we could get Compass to play well with PadrinoRb.  It seems like the #sass method doesn't care about the options being passed to it, and we kept getting stack traces rendered into our CSS files. The stack trace was ruby [...]]]></description>
			<content:encoded><![CDATA[<p>My cohort, Ian Serlin, discovered this.  In a project we are working on we could get Compass to play well with PadrinoRb.  It seems like the #sass method doesn't care about the options being passed to it, and we kept getting stack traces rendered into our CSS files. The stack trace was ruby looking for - and failing to find compass/reset.css.</p>
<p>This is the code that DOESN'T work (padrino 0.9.10, compass 0.8.17, sinatra 1.0).</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  configure <span style="color:#9966CC; font-weight:bold;">do</span>
    register SassInitializer <span style="color:#008000; font-style:italic;">#The Rack Sass reloader...</span>
&nbsp;
    Compass.<span style="color:#9900CC;">configuration</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
      config.<span style="color:#9900CC;">project_path</span> = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      config.<span style="color:#9900CC;">sass_dir</span> = <span style="color:#996600;">&quot;stylesheets&quot;</span>
      config.<span style="color:#9900CC;">project_type</span> = <span style="color:#ff3333; font-weight:bold;">:stand_alone</span>
      config.<span style="color:#9900CC;">http_path</span> = <span style="color:#996600;">&quot;/&quot;</span>
      config.<span style="color:#9900CC;">css_dir</span> = <span style="color:#996600;">&quot;stylesheets&quot;</span>
      config.<span style="color:#9900CC;">images_dir</span> = <span style="color:#996600;">&quot;images&quot;</span>
      config.<span style="color:#9900CC;">output_style</span> = <span style="color:#ff3333; font-weight:bold;">:compressed</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>   
  get <span style="color:#996600;">'/stylesheets/:file.css'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    content_type <span style="color:#996600;">'text/css'</span>, <span style="color:#ff3333; font-weight:bold;">:charset</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'utf-8'</span>
    <span style="color:#008000; font-style:italic;"># This is the doc on how to give Sass some more load paths to find the compass  files. </span>
    <span style="color:#008000; font-style:italic;">#   it doesnt work :P and for that matter, neither does:</span>
    <span style="color:#008000; font-style:italic;"># sass :file, Compass.sass_engine_options</span>
    <span style="color:#008000; font-style:italic;"># or the set :sass, whatever_hash_here</span>
    sass <span style="color:#ff3333; font-weight:bold;">:file</span>, <span style="color:#ff3333; font-weight:bold;">:sass</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> Compass.<span style="color:#9900CC;">sass_engine_options</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Our solution was to force Sass options and Compass options to merge...</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
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">  configure <span style="color:#9966CC; font-weight:bold;">do</span>
    register SassInitializer
&nbsp;
    Compass.<span style="color:#9900CC;">configuration</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>config<span style="color:#006600; font-weight:bold;">|</span>
      config.<span style="color:#9900CC;">project_path</span> = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      config.<span style="color:#9900CC;">sass_dir</span> = <span style="color:#996600;">&quot;stylesheets&quot;</span>
      config.<span style="color:#9900CC;">project_type</span> = <span style="color:#ff3333; font-weight:bold;">:stand_alone</span>
      config.<span style="color:#9900CC;">http_path</span> = <span style="color:#996600;">&quot;/&quot;</span>
      config.<span style="color:#9900CC;">css_dir</span> = <span style="color:#996600;">&quot;stylesheets&quot;</span>
      config.<span style="color:#9900CC;">images_dir</span> = <span style="color:#996600;">&quot;images&quot;</span>
      config.<span style="color:#9900CC;">output_style</span> = <span style="color:#ff3333; font-weight:bold;">:compressed</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#6666ff; font-weight:bold;">Sass::Plugin</span>.<span style="color:#9900CC;">options</span>.<span style="color:#9900CC;">merge</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>Compass.<span style="color:#9900CC;">sass_engine_options</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  get <span style="color:#996600;">'/stylesheets/:file.css'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    content_type <span style="color:#996600;">'text/css'</span>, <span style="color:#ff3333; font-weight:bold;">:charset</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'utf-8'</span>
    sass <span style="color:#ff3333; font-weight:bold;">:file</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Yay, magic spells - it works. You can check out more ian magic spells at ianserlin.com.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin+http://bit.ly/cdI2jj" 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/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/&amp;title=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin" 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/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/&amp;t=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin" 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/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/&amp;title=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin" 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/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/&amp;title=Padrino%2C+Compass%2C+and+Sass+%E2%80%93+Working+happily+via+Ian+Serlin" 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/04/17/padrino-compass-and-sass-working-happily-via-ian-serlin/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://bit.ly/8ZmCoU" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;title=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;t=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;title=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/&amp;title=Attaching+local+or+remote+files+to+Paperclip+and+Milton+Models+in+Rails+%28Mocking+content_type+and+original_filename+i..." title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/03/05/attaching-local-or-remote-files-to-paperclip-and-milton-models-in-rails-mocking-content_type-and-original_filename-in-a-tempfile/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

