<?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>Jonathan Spooner is a Web Developer in the San Diego Area. &#187; Ruby 5</title>
	<atom:link href="http://www.jonathanspooner.com/category/web-development/ruby-5/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jonathanspooner.com</link>
	<description>Flex Developer, Air Developer, and ActionScript Programmer</description>
	<lastBuildDate>Thu, 31 Mar 2011 16:02:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby &#8211; Time.next(:friday)</title>
		<link>http://www.jonathanspooner.com/web-development/ruby-time-nextfriday/</link>
		<comments>http://www.jonathanspooner.com/web-development/ruby-time-nextfriday/#comments</comments>
		<pubDate>Thu, 31 Mar 2011 15:59:26 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Ruby 5]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.jonathanspooner.com/?p=781</guid>
		<description><![CDATA[This patch adds a method to find the next requested weekday.

]]></description>
			<content:encoded><![CDATA[<p>This patch adds a method to find the next requested weekday.<br />
<script src="https://gist.github.com/896635.js?file=time.rb"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonathanspooner.com/web-development/ruby-time-nextfriday/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Single Table HABTM with Rails 3</title>
		<link>http://www.jonathanspooner.com/web-development/single-table-habtm-with-rails-3/</link>
		<comments>http://www.jonathanspooner.com/web-development/single-table-habtm-with-rails-3/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 19:42:41 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Ruby 5]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.jonathanspooner.com/?p=755</guid>
		<description><![CDATA[I'm building a user model that keeps track of your parents and any children you should have.  The acts_as_tree plugin
wouldn't have worked because it wouldn't allow for a parent to have many children.  What's needed is a HABTM relationship.  It's a quite simple relationship except for the :association_foreign_key option not being listed [...]]]></description>
			<content:encoded><![CDATA[<p>I'm building a user model that keeps track of your parents and any children you should have.  The acts_as_tree plugin<br />
wouldn't have worked because it wouldn't allow for a parent to have many children.  What's needed is a HABTM relationship.  It's a quite simple relationship except for the :association_foreign_key option not being listed in the docs.  Anyways I found some people discussing it <a href="http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/5f6eaa79ec60521f?pli=1">here</a>. </p>
<pre class="ruby">

class User < ActiveRecord::Base
  has_and_belongs_to_many :parents,  :class_name => "User", :join_table => "parents_children", :foreign_key => "child_id", :association_foreign_key => "parent_id"
  has_and_belongs_to_many :children, :class_name => "User", :join_table => "parents_children", :foreign_key => "parent_id" , :association_foreign_key => "child_id"
end
</pre>
<pre class="ruby">
describe User do
  it "should have 0 children" do
    User.new.should have(0).children
  end
  it "should have 0 parents" do
    User.new.should have(0).parents
  end
  it "should have 1 child" do
    parent = User.new
    parent.children << User.new
    parent.should have(1).children
  end
  it "should have 0 parents" do
    child = User.new
    child.parents << User.new
    child.should have(1).parents
  end
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jonathanspooner.com/web-development/single-table-habtm-with-rails-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby5 #3 &#8211; Rspec stubbing named_scope in a controller</title>
		<link>http://www.jonathanspooner.com/web-development/ruby5-3-rspec-stubbing-named_scope-in-a-controller/</link>
		<comments>http://www.jonathanspooner.com/web-development/ruby5-3-rspec-stubbing-named_scope-in-a-controller/#comments</comments>
		<pubDate>Fri, 28 May 2010 16:42:35 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Ruby 5]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.jonathanspooner.com/?p=663</guid>
		<description><![CDATA[I found the stub_chain method very helpful when stubbing out objects for Rspec controller testing.
My index action is calling 2 named_scopes and then doing a find.
&#160;
  def index
    @exercise_logs = ExerciseLog.this_user&#40;current_user&#41;.past.all
  end
&#160;
When testing controllers your just trying to stub out a value for @exercise_logs not simulating an actual call to [...]]]></description>
			<content:encoded><![CDATA[<p>I found the <code>stub_chain</code> method very helpful when stubbing out objects for Rspec controller testing.</p>
<p>My index action is calling 2 named_scopes and then doing a find.</p>
<pre class="ruby">&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    <span style="color:#0066ff; font-weight:bold;">@exercise_logs</span> = ExerciseLog.<span style="color:#9900CC;">this_user</span><span style="color:#006600; font-weight:bold;">&#40;</span>current_user<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">past</span>.<span style="color:#9900CC;">all</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>When testing controllers your just trying to stub out a value for @exercise_logs not simulating an actual call to your model.  So we'll use the stub_chain method to stub out our named_scopes like this.  </p>
<pre class="ruby">&nbsp;
    it <span style="color:#996600;">&quot;assigns all exercise_logs as @exercise_logs&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      ExerciseLog.<span style="color:#9900CC;">stub_chain</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:this_user</span>,:past<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>mock<span style="color:#006600; font-weight:bold;">&#40;</span>ExerciseLog<span style="color:#006600; font-weight:bold;">&#41;</span>,mock<span style="color:#006600; font-weight:bold;">&#40;</span>ExerciseLog<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      get <span style="color:#ff3333; font-weight:bold;">:index</span>
      response.<span style="color:#9900CC;">should</span> be_success
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>Resources:<br />
<a href="http://stackoverflow.com/questions/1638814/stubbing-named-scope-in-an-rspec-controller/2930723#2930723">http://stackoverflow.com/questions/1638814/stubbing-named-scope-in-an-rspec-controller/2930723#2930723</a></p>
<p><a href="http://apidock.com/rspec/Spec/Mocks/Methods/stub_chain">http://apidock.com/rspec/Spec/Mocks/Methods/stub_chain</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonathanspooner.com/web-development/ruby5-3-rspec-stubbing-named_scope-in-a-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby5 #2 &#8211; Rails Securing Passwords</title>
		<link>http://www.jonathanspooner.com/web-development/ruby5-2-rails-securing-passwords/</link>
		<comments>http://www.jonathanspooner.com/web-development/ruby5-2-rails-securing-passwords/#comments</comments>
		<pubDate>Fri, 28 May 2010 16:19:27 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Ruby 5]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.jonathanspooner.com/?p=650</guid>
		<description><![CDATA[It's important to filter out any sensitive data such as passwords from your log files.  You can easily filter out data across your while application by calling filter_paramter_logging from your ApplicationController.  In the example below I'm passing :password and :password_confirmation to remove their values from being placed in the logs.
ActionController::Base
&#160;
class ApplicationController &#60; ActionController::Base
 [...]]]></description>
			<content:encoded><![CDATA[<p>It's important to filter out any sensitive data such as passwords from your log files.  You can easily filter out data across your while application by calling <code>filter_paramter_logging</code> from your ApplicationController.  In the example below I'm passing <code>:password</code> and <code>:password_confirmation</code> to remove their values from being placed in the logs.</p>
<p><a href="http://rails.rubyonrails.org/classes/ActionController/Base.html#M000453">ActionController::Base</a></p>
<pre class="ruby">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController &lt; <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
  filter_parameter_logging <span style="color:#ff3333; font-weight:bold;">:password</span>, <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>You will now see "FILTERED" in place of sensitive data.</p>
<pre class="ruby">&nbsp;
  Parameters: <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;x&quot;</span>=&gt;<span style="color:#996600;">&quot;37&quot;</span>, <span style="color:#996600;">&quot;y&quot;</span>=&gt;<span style="color:#996600;">&quot;14&quot;</span>, <span style="color:#996600;">&quot;action&quot;</span>=&gt;<span style="color:#996600;">&quot;login&quot;</span>, <span style="color:#996600;">&quot;authenticity_token&quot;</span>=&gt;<span style="color:#996600;">&quot;JRFNcG9chNIpcsHoJzcQRRy1D6lIenjl7cWmvp3UpaI=&quot;</span>, <span style="color:#996600;">&quot;controller&quot;</span>=&gt;<span style="color:#996600;">&quot;videos&quot;</span>, <span style="color:#996600;">&quot;user_id&quot;</span>=&gt;<span style="color:#996600;">&quot;7-Jonathan-SpoonerJune&quot;</span>, <span style="color:#996600;">&quot;video&quot;</span>=&gt;<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;password&quot;</span>=&gt;<span style="color:#996600;">&quot;[FILTERED]&quot;</span>, <span style="color:#996600;">&quot;email&quot;</span>=&gt;<span style="color:#996600;">&quot;june@gmail.com&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jonathanspooner.com/web-development/ruby5-2-rails-securing-passwords/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby5 #1 &#8211; How does a Ruby block work?</title>
		<link>http://www.jonathanspooner.com/web-development/ruby-5/how-does-a-ruby-block-work/</link>
		<comments>http://www.jonathanspooner.com/web-development/ruby-5/how-does-a-ruby-block-work/#comments</comments>
		<pubDate>Fri, 21 May 2010 17:30:54 +0000</pubDate>
		<dc:creator>Administrator</dc:creator>
				<category><![CDATA[Ruby 5]]></category>

		<guid isPermaLink="false">http://www.jonathanspooner.com/?p=634</guid>
		<description><![CDATA[Ruby implements Blocks, Procs and lambdas which are referred to as closures in the computer science community.  
If you beginning to learn Ruby you will quickly run into code that looks like this. 
&#160;
a = &#91;&#34;dog&#34;, &#34;cat&#34;, &#34;bird&#34;&#93;
a.alter_each! do &#124;n, i&#124;
  &#34;#{i}_#{n}&#34;
end
&#160;
 So whats going on here?
We start off with an array of [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby implements Blocks, Procs and lambdas which are referred to as closures in the computer science community.  </p>
<p>If you beginning to learn Ruby you will quickly run into code that looks like this. </p>
<pre class="ruby">&nbsp;
a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;dog&quot;</span>, <span style="color:#996600;">&quot;cat&quot;</span>, <span style="color:#996600;">&quot;bird&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
a.<span style="color:#9900CC;">alter_each</span>! <span style="color:#9966CC; font-weight:bold;">do</span> |n, i|
  <span style="color:#996600;">&quot;#{i}_#{n}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p> So whats going on here?<br />
We start off with an array of animal names and call the <code>alter_each!</code> method passing a block.  In this block of code we can specify how we want to alter each item.  Our example will prefix each animal name with it's position in the array.  As the alter_each! method iterates through each item it will execute our block passing the value and index.  Our block captures these params, prefixes the index to the name and returns the result.  </p>
<p>Now lets look at the alter_each! method.<br />
Notice the method doesn't specify any params, that's because a block is automatically assigned to yield keyword.  yield is called like a function passing in the value and index of each item in the array and overriding the original value.</p>
<pre class="ruby">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> alter_each!
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">each_with_index</span> <span style="color:#9966CC; font-weight:bold;">do</span> |n, i|
      <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>n,i<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>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>What if you need to pass a param to this method?<br />
You can modify the method signature to accept params and finally catch the block with a param starting with an ampersand.  In the example below our block will be captured with the &block param which we will invoke the <code>call</code> method.  This is in place of using <code>yield</code> </p>
<pre class="ruby">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> modify_each!<span style="color:#006600; font-weight:bold;">&#40;</span>add_one = <span style="color:#0000FF; font-weight:bold;">true</span>, &amp;block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">each_with_index</span> <span style="color:#9966CC; font-weight:bold;">do</span> |n, i|
      j = <span style="color:#006600; font-weight:bold;">&#40;</span>add_one<span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#006600; font-weight:bold;">&#40;</span>i + <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> : i
      <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = block.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>n,j<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>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>Here is a full example that will prefix the index with the <code>yield</code> method and append the index with the <code>call</code> method.</p>
<pre class="ruby">&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> alter_each!
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">each_with_index</span> <span style="color:#9966CC; font-weight:bold;">do</span> |n, i|
      <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>n,i<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>
  <span style="color:#9966CC; font-weight:bold;">def</span> modify_each!<span style="color:#006600; font-weight:bold;">&#40;</span>add_one = <span style="color:#0000FF; font-weight:bold;">true</span>, &amp;block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">each_with_index</span> <span style="color:#9966CC; font-weight:bold;">do</span> |n, i|
      j = <span style="color:#006600; font-weight:bold;">&#40;</span>add_one<span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#006600; font-weight:bold;">&#40;</span>i + <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> : i
      <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = block.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>n,j<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>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;dog&quot;</span>, <span style="color:#996600;">&quot;cat&quot;</span>, <span style="color:#996600;">&quot;cow&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
a.<span style="color:#9900CC;">alter_each</span>! <span style="color:#9966CC; font-weight:bold;">do</span> |n, i|
  <span style="color:#996600;">&quot;#{i}_#{n}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
a.<span style="color:#9900CC;">modify_each</span>! <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">do</span> |n,i|
  <span style="color:#996600;">&quot;#{n}_#{i}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> a
&nbsp;</pre>
<p>For blocks in the wild check out the ActiveRecord save method.</p>
<pre class="ruby">&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> save<span style="color:#006600; font-weight:bold;">&#40;</span>perform_validation = <span style="color:#0000FF; font-weight:bold;">true</span>, &amp;block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">if</span> perform_validation &amp;&amp; block_given? &amp;&amp; authenticate_with_sorenson? &amp;&amp; !authenticate_with_sorenson
    result = <span style="color:#9966CC; font-weight:bold;">super</span>
    <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>result<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
    result
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;</pre>
<p>References:<br />
<a href="http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/">Understanding Ruby Blocks, Procs and lambdas</a><br />
 <a href="http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/"></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonathanspooner.com/web-development/ruby-5/how-does-a-ruby-block-work/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

