<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Webficient</title>
	
	<link>http://www.webficient.com</link>
	<description>Your Web Development Partner</description>
	<pubDate>Wed, 24 Feb 2010 09:06:08 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Webficient" /><feedburner:info uri="webficient" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:feedFlare href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare href="http://www.bloglines.com/sub/http://feeds.feedburner.com/Webficient" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare href="http://www.plusmo.com/add?url=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient" src="http://plusmo.com/res/graphics/fbplusmo.gif">Subscribe with Plusmo</feedburner:feedFlare><feedburner:feedFlare href="http://www.live.com/?add=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><feedburner:feedFlare href="http://www.yourminis.com/subscribe.aspx?u=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient" src="http://www.yourminis.com/images/addtoyourminisbadge.gif">Subscribe with Yourminis.com</feedburner:feedFlare><feedburner:feedFlare href="http://www.addtoany.com/?linkname=Webficient&amp;linkurl=http%3A%2F%2Ffeeds.feedburner.com%2FWebficient&amp;type=feed" src="http://www.addtoany.com/addfr-b.gif">Add to Any Feed Reader</feedburner:feedFlare><item>
		<title>Why We Recommend Engine Yard for Hosting Ruby Apps</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/JtcsuheRhOE/why-we-recommend-engine-yard-for-hosting-ruby-apps</link>
		<comments>http://www.webficient.com/2009/11/10/why-we-recommend-engine-yard-for-hosting-ruby-apps#comments</comments>
		<pubDate>Tue, 10 Nov 2009 20:28:17 +0000</pubDate>
		<dc:creator>Phil Misiowiec</dc:creator>
		
		<category><![CDATA[Commentary]]></category>

		<category><![CDATA[Resources]]></category>

		<category><![CDATA[Solutions]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=957</guid>
		<description><![CDATA[I often get asked by clients, peers and other developers about Engine Yard vs. other hosting solutions. Here&#8217;s a breakdown of reasons based on our experience working with a number of service providers over the last few years.
Engine Yard vs.
Managing Amazon EC2 instances directly: we’ve built our own server images for Amazon EC2 in the [...]]]></description>
			<content:encoded><![CDATA[<p>I often get asked by clients, peers and other developers about <a href="http://www.engineyard.com">Engine Yard</a> vs. other hosting solutions. Here&#8217;s a breakdown of reasons based on our experience working with a number of service providers over the last few years.</p>
<p><strong>Engine Yard vs.</strong></p>
<p><strong>Managing Amazon EC2 instances directly</strong>: we’ve built our own server images for Amazon EC2 in the past and managed them, so we know first hand the time and effort involved with a DIY-approach. Engine Yard has a mature, sophisticated stack that runs on top of Amazon EC2. Coupled with an easy-to-use Web-based dashboard, Engine Yard Cloud makes it super easy for development-centric organizations like ours to deploy and manage client systems without having to deal with the complexity of EC2. In addition, data is persisted to Amazon EBS, so we don’t have to worry about setting up our own internal processes or writing custom scripts for handling backups and persistence.</p>
<p><strong>Other hosting service providers</strong>: if you are running your own virtualized servers on behalf of clients or for your own needs, there are many things you have to deal with. Configuring the server, compiling custom components, setting up monitoring, configuring maintenance processes and tasks (e.g. log rotation), and especially security. Over time, we’ve been moving away from handling these details ourselves so that we can focus on the things that we do best and keep our operations lean. We prefer a sophisticated cloud management solution instead. On top of that, it&#8217;s refreshing to receive security advisories from Engine Yard about patches to Nginx or similar &#8212; these patches are deployed through a single-click in our Engine Yard Cloud dashboard.</p>
<p><strong>Other cloud management solutions</strong>: the problem with these other options is they don’t have the resources on staff who understand Ruby-based architectures the way EY’s engineers do. Engine Yard has experienced, world-reknown Ruby engineers behind their stack like Ezra. Other companies claim they support Ruby but when complexities arise, they simply can’t deliver the level of support the way EY does. All it requires is a simple comparison of the content in the <a href="https://cloud-support.engineyard.com/">knowledge base and customer support forum</a> managed by Engine Yard with their competitors. It’s clear that Engine Yard has Ruby developers at the top of its priority list.</p>
<p>Over the last few months, we&#8217;ve been migrating existing clients as well as some of our own apps to Engine Yard. We appreciate their awesome service and support. This is why we&#8217;re proud to be one of their <a href="http://www.engineyard.com/partners/select">partners</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=JtcsuheRhOE:QNpBgyco_s8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=JtcsuheRhOE:QNpBgyco_s8:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=JtcsuheRhOE:QNpBgyco_s8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=JtcsuheRhOE:QNpBgyco_s8:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/JtcsuheRhOE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/11/10/why-we-recommend-engine-yard-for-hosting-ruby-apps/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/11/10/why-we-recommend-engine-yard-for-hosting-ruby-apps</feedburner:origLink></item>
		<item>
		<title>Using Amazon CloudFront with Paperclip</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/T2FBY0QMWU0/using-amazon-cloudfront-with-paperclip</link>
		<comments>http://www.webficient.com/2009/10/21/using-amazon-cloudfront-with-paperclip#comments</comments>
		<pubDate>Thu, 22 Oct 2009 05:44:56 +0000</pubDate>
		<dc:creator>Phil Misiowiec</dc:creator>
		
		<category><![CDATA[Solutions]]></category>

		<category><![CDATA[amazon cloudfront]]></category>

		<category><![CDATA[amazon s3]]></category>

		<category><![CDATA[paperclip]]></category>

		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=938</guid>
		<description><![CDATA[There was a recent blog post over at Engine Yard discussing ways you can optimize client-side behavior. One of the recommendations provides guidance when using Amazon S3 for storage. Recently, we&#8217;ve been experimenting with Amazon CloudFront, their Content Delivery Network (CDN) in-the-cloud offering. For serving static content like photos in medium to high volume scenarios, [...]]]></description>
			<content:encoded><![CDATA[<p>There was a <a href="http://www.engineyard.com/blog/2009/rails-in-the-wild-5-client-side-performance-observations">recent blog post</a> over at Engine Yard discussing ways you can optimize client-side behavior. One of the recommendations provides guidance when using Amazon S3 for storage. Recently, we&#8217;ve been experimenting with <a href="http://aws.amazon.com/cloudfront/">Amazon CloudFront</a>, their Content Delivery Network (CDN) in-the-cloud offering. For serving static content like photos in medium to high volume scenarios, CloudFront seems to be a smart bet and the costs are ridiculously low compared to the historical licensing costs when using a CDN. You&#8217;ve got lower latency, geographical distribution, and behind-the-scenes, CloudFront uses S3 for persisting your source content, so you can use the same interfaces and tools to tap into it. Optionally, CloudFront can generate request log files and persist them to an S3 bucket.</p>
<p>Naturally, uploading photos in a Ruby on Rails application usually involves a robust gem/plugin such as Thoughtbot&#8217;s <a href="http://thoughtbot.com/projects/paperclip">Paperclip</a>. We found that it&#8217;s very easy to configure it to seamlessly upload to CloudFront.</p>
<p>Here&#8217;s a sample polymorphic photo model that uses Paperclip:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Photo <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_attached_file <span style="color:#ff3333; font-weight:bold;">:data</span>,
    <span style="color:#ff3333; font-weight:bold;">:styles</span>         <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:lrg</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;300x300&amp;gt;&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:thumb</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;100x100&gt;&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:micro</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;50x50&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#ff3333; font-weight:bold;">:storage</span>        <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:s3</span>,
    <span style="color:#ff3333; font-weight:bold;">:s3_credentials</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">load_file</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{RAILS_ROOT}/config/amazon_s3.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,
    <span style="color:#ff3333; font-weight:bold;">:s3_host_alias</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;djoo9p6eu3n0g.cloudfront.net&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:bucket</span>         <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;listing-photos&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:url</span>            <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;:s3_alias_url&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:path</span>           <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;:class/:id/:style/:filename&quot;</span>
&nbsp;
  belongs_to <span style="color:#ff3333; font-weight:bold;">:attachable</span>, <span style="color:#ff3333; font-weight:bold;">:polymorphic</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The relevant configuration keys include :bucket, :storage, :s3_credentials, :s3_host_alias, and :url. The best thing is that your helper methods, e.g. photo.data.url(:lrg), continue to work the same way. Paperclip abstracts the file storage mechanism so that your views don&#8217;t need to have this responsibility.</p>
<h1>Getting Started</h1>
<p>1. Signup with CloudFront at <a href="http://aws.amazon.com/cloudfront/">http://aws.amazon.com/cloudfront/</a><br />
2. Once you&#8217;re in there, you can use the AWS Management Console to define and deploy your CloudFront distribution bucket.</p>
<p><img class="size-thumbnail wp-image-943 alignnone" title="aws-management-console-cloudfront" src="http://www.webficient.com/wp-content/uploads/2009/10/aws-management-console-cloudfront.png" alt="" width="743" height="269" /></p>
<p>3. Setup Paperclip to use CloudFront as described above.</p>
<p>You&#8217;re done!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=T2FBY0QMWU0:rKSwST47ZXM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=T2FBY0QMWU0:rKSwST47ZXM:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=T2FBY0QMWU0:rKSwST47ZXM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=T2FBY0QMWU0:rKSwST47ZXM:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/T2FBY0QMWU0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/10/21/using-amazon-cloudfront-with-paperclip/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/10/21/using-amazon-cloudfront-with-paperclip</feedburner:origLink></item>
		<item>
		<title>Rails with_options in your config file</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/sTq7tGWL80g/rails-with-options-in-your-config-file</link>
		<comments>http://www.webficient.com/2009/10/17/rails-with-options-in-your-config-file#comments</comments>
		<pubDate>Sun, 18 Oct 2009 02:05:54 +0000</pubDate>
		<dc:creator>Phil Misiowiec</dc:creator>
		
		<category><![CDATA[Tips]]></category>

		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=921</guid>
		<description><![CDATA[You&#8217;ve probably seen the with_options method (part of ActiveSupport) used in the context of routing maps such as:

map.with_options :controller =&#62; &#34;people&#34; do &#124;people&#124;
    people.connect &#34;/people&#34;,     :action =&#62; &#34;index&#34;
    people.connect &#34;/people/:id&#34;, :action =&#62; &#34;show&#34;
end

But realize that with_options extends the Ruby Object class (source code) so it [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve probably seen the <em>with_options</em> method (part of <a href="http://api.rubyonrails.org/files/vendor/rails/activesupport/README.html">ActiveSupport</a>) used in the context of routing maps such as:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">map.<span style="color:#9900CC;">with_options</span> <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;people&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |people|
    people.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">&quot;/people&quot;</span>,     <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;index&quot;</span>
    people.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">&quot;/people/:id&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;show&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>But realize that <em>with_options</em> extends the Ruby Object class (<a href="http://github.com/rails/rails/blob/78f2c19ae7f9236591c261eecdf0c4b570e3ea1e/activesupport/lib/active_support/core_ext/object/misc.rb#L77">source code</a>) so it can be used practically in any context.</p>
<p>I&#8217;d like to keep my configuration file leaner when specifying gem dependencies.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">with_options</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:source</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'http://gemcutter.org'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |c|
  c.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'acts_as_fu'</span>
  c.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'factory_girl'</span> 
  c.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'mocha'</span> 
  c.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'redgreen'</span>
  c.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'shoulda'</span>
  c.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'test_benchmark'</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># not yet in gemcutter </span>
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">'stubble'</span></pre></div></div>

<p>However, the gotcha is the <em>Rails::Configuration</em> class is loaded before <em>ActiveSupport</em>. So if you&#8217;d like to use <em>with_options</em> in your config, be sure you load the module environment.rb before the <em>Rails::Initializer</em> block is hit.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'active_support'</span></pre></div></div>

<p>The above is moot with the new gem bundling features in Rails 3, <a href="http://litanyagainstfear.com/blog/2009/10/14/gem-bundler-is-the-future/">read more</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=sTq7tGWL80g:kxJppNXYrBE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=sTq7tGWL80g:kxJppNXYrBE:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=sTq7tGWL80g:kxJppNXYrBE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=sTq7tGWL80g:kxJppNXYrBE:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/sTq7tGWL80g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/10/17/rails-with-options-in-your-config-file/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/10/17/rails-with-options-in-your-config-file</feedburner:origLink></item>
		<item>
		<title>Easy Stubbing in Rails Functional Tests</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/EAmEv6JQTmU/easy-stubbing-in-rails-functional-tests</link>
		<comments>http://www.webficient.com/2009/10/14/easy-stubbing-in-rails-functional-tests#comments</comments>
		<pubDate>Thu, 15 Oct 2009 05:58:24 +0000</pubDate>
		<dc:creator>Phil Misiowiec</dc:creator>
		
		<category><![CDATA[Solutions]]></category>

		<category><![CDATA[ruby on rails]]></category>

		<category><![CDATA[shoulda]]></category>

		<category><![CDATA[stubbing]]></category>

		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=908</guid>
		<description><![CDATA[I discovered a simple, concise approach for stubbing models inside Ruby on Rails controller functional tests.  It relies on David Chelimsky&#8217;s excellent Stubble gem. It&#8217;s a shame it took me 5 months to discover it, even though I sat in on his RailsConf presentation in May, &#8220;Don&#8217;t Mock Yourself Out.&#8221; (doh!). First the why&#8230;
The [...]]]></description>
			<content:encoded><![CDATA[<p>I discovered a simple, concise approach for stubbing models inside Ruby on Rails controller functional tests.  It relies on David Chelimsky&#8217;s excellent <a href="http://github.com/dchelimsky/stubble/">Stubble gem</a>. It&#8217;s a shame it took me 5 months to discover it, even though I sat in on his RailsConf presentation in May, &#8220;<a href="http://www.scribd.com/doc/15010054/Dont-Mock-Yourself-Out">Don&#8217;t Mock Yourself Out.</a>&#8221; (doh!). First the <em>why</em>&#8230;</p>
<p>The key goal of your functional tests should be isolating what controllers are expected to do. Don&#8217;t let pseudo unit tests leak into this realm. Don&#8217;t waste your time writing controller tests in which a post containing specific attributes fails because of a validation rule on the instance. Instead focus on what the controller <em>should</em> do if the post resulted in an invalid object state.</p>
<p>Stubble makes things super easy by automatically stubbing out the key Active Record class and instance methods. This means less code in your tests. </p>
<p>Here&#8217;s an example that mixes Stubble and Shoulda:</p>
<p><script src="http://gist.github.com/210655.js"></script></p>
<p>Notice I did not need to set any attributes as part of the post. Again, Stubble automatically stubs the most common AR methods so the attributes would be ignored in the first place. Functionally, all I care about is what happens when a post results in a valid or invalid condition. Either I&#8217;ll be redirected (to the photos page) or back to the form itself (the :new action). I&#8217;ve completely decoupled the implementation details of my model from my controller test. So when my model changes in the near future, I most likely won&#8217;t have to update my functional tests.</p>
<p>Check out the <a href="http://github.com/dchelimsky/stubble/">Stubble GitHub project page</a> for more examples.  </p>
<p>After installing the Stubble gem, you&#8217;ll need to update test_helper.rb to leverage it:</p>
<p><script src="http://gist.github.com/210712.js"></script></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=EAmEv6JQTmU:qWJ_K2JCkBc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=EAmEv6JQTmU:qWJ_K2JCkBc:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=EAmEv6JQTmU:qWJ_K2JCkBc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=EAmEv6JQTmU:qWJ_K2JCkBc:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/EAmEv6JQTmU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/10/14/easy-stubbing-in-rails-functional-tests/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/10/14/easy-stubbing-in-rails-functional-tests</feedburner:origLink></item>
		<item>
		<title>Working backwards when creating Rails helpers</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/9ntJtZYulps/working-backwards-when-creating-rails-helpers</link>
		<comments>http://www.webficient.com/2009/07/10/working-backwards-when-creating-rails-helpers#comments</comments>
		<pubDate>Sat, 11 Jul 2009 05:29:25 +0000</pubDate>
		<dc:creator>Phil Misiowiec</dc:creator>
		
		<category><![CDATA[Tips]]></category>

		<category><![CDATA[helper]]></category>

		<category><![CDATA[markup]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=880</guid>
		<description><![CDATA[I often find myself working backwards, meaning, I start with the end result first and then decompose it into a view helper. For example, let&#8217;s say I need to create a CSS-based grid as show below:

I could definitely see myself re-using this layout in other places (and I can extract it into a Rails plugin [...]]]></description>
			<content:encoded><![CDATA[<p>I often find myself working backwards, meaning, I start with the end result first and then decompose it into a view helper. For example, let&#8217;s say I need to create a CSS-based grid as show below:</p>
<p><img class="alignnone size-full wp-image-881" title="products" src="http://www.webficient.com/wp-content/uploads/2009/07/products.jpg" alt="" width="500" height="230" /></p>
<p>I could definitely see myself re-using this layout in other places (and I can extract it into a Rails plugin if I start needing it across multiple apps). So let&#8217;s think about how to turn this into a Rails view helper (I&#8217;ll assume we&#8217;re using ERb).</p>
<p>What I&#8217;m aiming for is the following markup:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;span class=&quot;category&quot;&gt;
  &lt;p&gt;Great Deals&lt;/p&gt;
  &lt;p&gt;Breads &amp;amp; Grains&lt;/p&gt;
  &lt;p&gt;Dried Beans &amp;amp; Legumes&lt;/p&gt;
  &lt;p&gt;Dried Fruits &amp;amp; Nuts&lt;/p&gt;
  &lt;p&gt;Eggs &amp;amp; Dairy&lt;/p&gt;
&lt;/span&gt;
&lt;span class=&quot;category&quot;&gt;
  &lt;p&gt;Fish &amp;amp; Seafood&lt;/p&gt;
  &lt;p&gt;Flowers&lt;/p&gt;
  &lt;p&gt;Fruits&lt;/p&gt;
  &lt;p&gt;Herbs &amp;amp; Spices&lt;/p&gt;
  &lt;p&gt;Juices &amp;amp; Drinks&lt;/p&gt;
&lt;/span&gt;
&lt;span class=&quot;category&quot;&gt;
  &lt;p&gt;Meat &amp;amp; Poultry&lt;/p&gt;
  &lt;p&gt;Oil &amp;amp; Vinegar&lt;/p&gt;
  &lt;p&gt;Plants &amp;amp; Seeds&lt;/p&gt;
  &lt;p&gt;Specialty Items&lt;/p&gt;
  &lt;p&gt;Vegetables&lt;/p&gt;
&lt;/span&gt;</pre></div></div>

<p>The above would likely be generated by iterating through an instance variable representing an array of products from the database. A rigid implementation in Ruby could take this form:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">&lt;span class=&quot;category&quot;&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@products</span><span style="color:#006600; font-weight:bold;">&#91;</span>0..4<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |p| <span style="color:#006600; font-weight:bold;">-%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span> 
&lt;/span&gt;
&lt;span class=&quot;category&quot;&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@products</span><span style="color:#006600; font-weight:bold;">&#91;</span>5..9<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |p| <span style="color:#006600; font-weight:bold;">-%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span> 
&lt;/span&gt;
&lt;span class=&quot;category&quot;&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@products</span><span style="color:#006600; font-weight:bold;">&#91;</span>10..14<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |p| <span style="color:#006600; font-weight:bold;">-%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span> 
&lt;/span&gt;</pre></div></div>

<p>And you could ditch plain old HTML completely and adopt this form:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> content_tag <span style="color:#ff3333; font-weight:bold;">:span</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'category'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@products</span><span style="color:#006600; font-weight:bold;">&#91;</span>0..4<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |p| <span style="color:#006600; font-weight:bold;">-%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>  
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> content_tag <span style="color:#ff3333; font-weight:bold;">:span</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'category'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@products</span><span style="color:#006600; font-weight:bold;">&#91;</span>5..9<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |p| <span style="color:#006600; font-weight:bold;">-%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>  
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> content_tag <span style="color:#ff3333; font-weight:bold;">:span</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'category'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@products</span><span style="color:#006600; font-weight:bold;">&#91;</span>10..14<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |p| <span style="color:#006600; font-weight:bold;">-%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span>  
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">-%&gt;</span></pre></div></div>

<p>No matter what, the above is just ugly. We&#8217;ve got duplication of code and assumptions about the size of the collection that translate into the need for ongoing code maintenance. </p>
<p>So to consolidate the above into less code, we have to think of a clever way to wrap the markup into a helper method. Ideally, I&#8217;d love to be able to do something as simple as:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> display_as_columns<span style="color:#006600; font-weight:bold;">&#40;</span>@products, <span style="color:#006600; font-weight:bold;">&#123;</span>:cols <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span>, <span style="color:#ff3333; font-weight:bold;">:tag</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'span'</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'category'</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> |v| <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>&#8230;or better yet:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> display_as_columns<span style="color:#006600; font-weight:bold;">&#40;</span>@products, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'category'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> |v| <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>You may be thinking, how do I handle a block (of markup) like that within a helper method? Easy - you can define a method signature as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> display_as_columns<span style="color:#006600; font-weight:bold;">&#40;</span>object_array, options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>markup<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>&#8230;where the ampersand in <em>&#038;markup</em> denotes this is a block. But the beautiful thing about Ruby is that methods support an optional, implicit block as the last parameter. So unless you need to do something special with the block after it&#8217;s passed into the method, you don&#8217;t need the last bit. Instead you have:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> display_as_columns<span style="color:#006600; font-weight:bold;">&#40;</span>object_array, options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Our helper method implementation looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> display_as_columns<span style="color:#006600; font-weight:bold;">&#40;</span>object_array, options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  cols = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:cols</span><span style="color:#006600; font-weight:bold;">&#93;</span> || <span style="color:#006666;">3</span>
  tag = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:tag</span><span style="color:#006600; font-weight:bold;">&#93;</span> || <span style="color:#996600;">'span'</span>
  css_class = options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:class</span><span style="color:#006600; font-weight:bold;">&#93;</span> || <span style="color:#996600;">'column'</span>
&nbsp;
  rows = <span style="color:#006600; font-weight:bold;">&#40;</span>object_array.<span style="color:#9900CC;">size</span>.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">/</span>cols.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">ceil</span>
  cols.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> |i|
    concat<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;#{tag} class='#{css_class}'&gt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      object_array<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#40;</span>rows<span style="color:#006600; font-weight:bold;">*</span>i<span style="color:#006600; font-weight:bold;">&#41;</span>..<span style="color:#006600; font-weight:bold;">&#40;</span>rows<span style="color:#006600; font-weight:bold;">*</span>i<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#40;</span>rows<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |item| <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>item<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    concat<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;/#{tag}&gt;&quot;</span><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></pre></div></div>

<p>The first 3 lines initialize a series of parameter defaults, if not set already. So if you want your columns to be wrapped into div&#8217;s, you&#8217;d pass in the :tag key in the options hash. Otherwise leaving it out will default to a span tag. Same thing goes for the number of columns. I arbitrarily default to 3 columns but you may want this to be an explicit parameter, unless a 3 column layout is standard across your views.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">rows = <span style="color:#006600; font-weight:bold;">&#40;</span>object_array.<span style="color:#9900CC;">size</span>.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">/</span>cols.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">ceil</span></pre></div></div>

<p>The above line is simply calculating the number of rows but applying the Ruby &#8220;ceiling&#8221; numeric transformation to ensure nothing gets cut out in case your numbers don&#8217;t divide equally.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">cols.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> |i|
  concat<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;#{tag} class='#{css_class}'&gt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    object_array<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#40;</span>rows<span style="color:#006600; font-weight:bold;">*</span>i<span style="color:#006600; font-weight:bold;">&#41;</span>..<span style="color:#006600; font-weight:bold;">&#40;</span>rows<span style="color:#006600; font-weight:bold;">*</span>i<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#40;</span>rows<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |item| <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>item<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  concat<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;&lt;/#{tag}&gt;&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Finally, the last section is where the magic occurs. The <em>concat</em> method, part of Rails&#8217; ActionView Helpers, adds your string to the output buffer. The <em>collect</em> method is part of the Ruby enumerable mixin. The interesting part is the <em>yield</em> call. It is passing the current &#8216;item&#8217; instance to the markup we defined as part of the original block passed into the helper:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;&lt;%= p.name %&gt;&lt;/p&gt;</pre></div></div>

<p>Enjoy.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=9ntJtZYulps:zi1DChpUsD0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=9ntJtZYulps:zi1DChpUsD0:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=9ntJtZYulps:zi1DChpUsD0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=9ntJtZYulps:zi1DChpUsD0:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/9ntJtZYulps" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/07/10/working-backwards-when-creating-rails-helpers/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/07/10/working-backwards-when-creating-rails-helpers</feedburner:origLink></item>
		<item>
		<title>Google Mail Servers and False Positive Spam</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/XU8Ehaylxl4/google-mail-servers-and-false-positive-spam</link>
		<comments>http://www.webficient.com/2009/06/23/google-mail-servers-and-false-positive-spam#comments</comments>
		<pubDate>Tue, 23 Jun 2009 20:24:15 +0000</pubDate>
		<dc:creator>Phil Misiowiec</dc:creator>
		
		<category><![CDATA[Solutions]]></category>

		<category><![CDATA[Tips]]></category>

		<category><![CDATA[dns]]></category>

		<category><![CDATA[google apps]]></category>

		<category><![CDATA[sender policy framework]]></category>

		<category><![CDATA[spam]]></category>

		<category><![CDATA[spf record]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=871</guid>
		<description><![CDATA[Last year, we posted an article about using DNS SPF records to help reduce the incidence of spam false positives. I wanted to update the info with a recent discovery that can help any of you using Google Apps as your email service for your personal or corporate domain name.
We have a client whose application [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, we posted <a href="http://www.webficient.com/2008/08/19/spf-records-spam-false-positives">an article</a> about using DNS SPF records to help reduce the incidence of spam false positives. I wanted to update the info with a recent discovery that can help any of you using <a href="http://www.google.com/apps">Google Apps</a> as your email service for your personal or corporate domain name.</p>
<p>We have a client whose application was subject to false positives when emails were sent to other users with Gmail or Google Apps-hosted accounts. The client is using the Google Apps email service for both sending and receiving emails. Per spec, they have a valid SPF record that includes an &#8220;MX&#8221; definition for Google&#8217;s mail servers, authorizing Google to send emails on their behalf. However, we noticed that emails sent from the domain to other Google emails were &#8220;soft failing&#8221; &#8212; in other words, Google was failing emails originating from their own servers.</p>
<p>The fix is easy &#8212; be sure your SPF record has &#8220;include:_spf.google.com&#8221;; example:</p>
<p>v=spf1 a include:_spf.google.com ~all</p>
<p>Hope that helps!</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=XU8Ehaylxl4:1go4q29esXw:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=XU8Ehaylxl4:1go4q29esXw:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=XU8Ehaylxl4:1go4q29esXw:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=XU8Ehaylxl4:1go4q29esXw:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/XU8Ehaylxl4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/06/23/google-mail-servers-and-false-positive-spam/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/06/23/google-mail-servers-and-false-positive-spam</feedburner:origLink></item>
		<item>
		<title>Hello, This is Your Rails App Calling You</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/x0dLrQbtTpk/hello-this-is-your-rails-app-calling-you</link>
		<comments>http://www.webficient.com/2009/06/22/hello-this-is-your-rails-app-calling-you#comments</comments>
		<pubDate>Mon, 22 Jun 2009 11:53:46 +0000</pubDate>
		<dc:creator>Phil Misiowiec</dc:creator>
		
		<category><![CDATA[Solutions]]></category>

		<category><![CDATA[ruby on rails]]></category>

		<category><![CDATA[telephony]]></category>

		<category><![CDATA[twilio]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=845</guid>
		<description><![CDATA[Editor&#8217;s note: we absolutely love open source and the free spirit of contribution. We&#8217;ve simplified our latest version based on some great suggestions from Jonas Nicklas. Thanks!
Telephony applications can be developed easier than ever before with Twilio, a cloud-based API for voice services. During the first half of this year, we&#8217;ve been playing, building, and [...]]]></description>
			<content:encoded><![CDATA[<p><em>Editor&#8217;s note: we absolutely love open source and the free spirit of contribution. We&#8217;ve simplified our latest version based on some great suggestions from Jonas Nicklas. Thanks!</em></p>
<p>Telephony applications can be developed easier than ever before with <a href="http://www.twilio.com">Twilio</a>, a cloud-based API for voice services. During the first half of this year, we&#8217;ve been playing, building, and now wrapping in the form of a <a href="http://github.com/webficient/twilio/tree/master">Ruby gem</a> we&#8217;ve finally polished off. I&#8217;d like to take you through an example to show off their amazing service offering.</p>
<h2>Twilio: How it Works</h2>
<p>Twilio&#8217;s REST API enables your application to make calls, playback audio, transcribe calls, and much more. With most operations such as outgoing and incoming calls, Twilio communicates with your application by pinging a URL that you designate. Your application then needs to respond according to the Twilio Markup XML (TwiML) specification, which is comprised of Twilio &#8220;verbs&#8221; such as dial, gather, play, record, and say. These verbs instruct Twilio what to do during the call.</p>
<h2>Example Use Case: Phone-Based Identity Verification</h2>
<p>In two recent customer Web applications, we were required to add functionality to be able to verify the ownership of business phone numbers. The idea was simple: during the user registration process, the system phones the listed business number, user answers phone and hears a 4-digit PIN number, and then is required to enter this code in the Web form to continue with registration. With Twilio&#8217;s service and our <a href="http://github.com/webficient/twilio/tree/master">Twilio Gem</a>, this is ridiculously easy, especially within a Ruby on Rails application.</p>
<p>Inside your Rails user registration controller, you&#8217;ll need to generate the 4-digit random code and stick it inside a session object:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@random_code</span> = <span style="color:#CC0066; font-weight:bold;">rand</span>.<span style="color:#9900CC;">to_s</span><span style="color:#006600; font-weight:bold;">&#91;</span>2..5<span style="color:#006600; font-weight:bold;">&#93;</span>
session<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:random_code</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#0066ff; font-weight:bold;">@random_code</span></pre></div></div>

<p>Now, set your Twilio credentials by calling the connect method. Because these get cached in the Twilio module, you don&#8217;t have to deal with connection instances.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">Twilio.<span style="color:#9900CC;">connect</span><span style="color:#006600; font-weight:bold;">&#40;</span>TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Finally, tell your app to call Twilio&#8217;s service. We&#8217;ve wrapped all of the Twilio REST resources into Ruby classes, each with their own methods that wrap the entire interface. In this example, we&#8217;re set the &#8220;from&#8221; number to a constant representing the phone number associated with our Twilio account. However, Twilio allows you to use other registered Twilio numbers, thus this value can be dynamic. The last parameter is where it gets interesting and represents the callback URL that Twilio should contact to communicate with your application.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Twilio::Call</span>.<span style="color:#9900CC;">make</span><span style="color:#006600; font-weight:bold;">&#40;</span>CALLER_ID, user.<span style="color:#9900CC;">phone</span>, 
  verify_phone_url<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:pin</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@random_code</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The above command instructs Twilio to phone the user&#8217;s number, showing your registered number as the caller id. As soon as Twilio connects to the number, it will also ping your URL.  </p>
<p>Now we need to handle the response. In our example, we need to &#8220;say&#8221; the PIN code to the person on the phone. Before we created our gem, we would use XML Builder to render a response inside a .builder template. But the gem simplifies things greatly. Inside another controller action, just add:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">random_code = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:pin</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<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>\d<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|s| s <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#125;</span>
render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#6666ff; font-weight:bold;">Twilio::Verb</span>.<span style="color:#9900CC;">say</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Your pin code is #{random_code}&quot;</span>, 
  <span style="color:#ff3333; font-weight:bold;">:loop</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span>, 
  <span style="color:#ff3333; font-weight:bold;">:pause</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Remember that the callback URL we initially sent to Twilio included a &#8216;pin&#8217; parameter, so Twilio sends this back to us. The reason we add spaces between the digits is that Twilio speech module will speak &#8220;1234&#8243; as &#8220;one thousand two hundred thirty four&#8221; while &#8220;1 2 3 4&#8243; will be spoken as &#8220;one two three four.&#8221; The loop and pause options result in a better experience for the person on the phone. Other options include the ability to set the voice to female, as well as use other languages.</p>
<p>The last thing remaining is to handle the user&#8217;s response. At this point, they hear the PIN code over the phone and enter them into the Web registration form. All that you need to do is confirm that their input matches the random_code session value.</p>
<h2>Testing Strategies</h2>
<p>With functional testing, you can resort to mock interfaces to guide you along. If you want to emulate the XML response handlers in your controllers, then take a look at the <a href="http://fakeweb.rubyforge.org/">FakeWeb gem</a>, which makes things very easy. In fact, most of Twilio gem&#8217;s tests leverage it.</p>
<p>But at some point, you&#8217;ll want to play around with real network connectivity. What I recommend is creating an SSH tunnel between a server you may be operating and your own development machine. This is painless if you manage with a rake task. First, create tunnel.yml and place in your Rails application&#8217;s <em>config</em> folder.</p>

<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;">development:
  public_host_username: sugar
  public_host: staging.webficient.com
  public_port: 8868
  local_port: 3000</pre></div></div>

<p>Now, save this rake task as lib/tasks/tunnel.rake:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">namespace <span style="color:#ff3333; font-weight:bold;">:tunnel</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
  desc <span style="color:#996600;">&quot;Start SSH tunnel to remote host&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:start</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    SSH_TUNNEL = <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">load_file</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{RAILS_ROOT}/config/tunnel.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span>RAILS_ENV<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    public_host_username = SSH_TUNNEL<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'public_host_username'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    public_host          = SSH_TUNNEL<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'public_host'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    public_port          = SSH_TUNNEL<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'public_port'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    local_port           = SSH_TUNNEL<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'local_port'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Starting tunnel #{public_host}:#{public_port} to 0.0.0.0:#{local_port}&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">system</span> <span style="color:#996600;">&quot;ssh -nNT -g -R *:#{public_port}:0.0.0.0:#{local_port} #{public_host_username}@#{public_host}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then, it&#8217;s as easy as typing <em>rake tunnel:start</em> in your command line. If it doesn&#8217;t work, be sure your server has the following values set in the sshd_config file:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">AllowTcpForwarding yes
GatewayPorts clientspecified</pre></div></div>

<p>Once the tunnel is running, point your browser to the public URL, and you&#8217;ll be actually browsing your local machine. This setup is very handy when debugging Twilio interactions.</p>
<h2>Endless Possibilities</h2>
<p>Kyle, one of our lead engineers, created an app that calls you when a server is down. In another project, we implemented click-to-call functionality, in which a user can enter their phone number and the application will connect them to the business they are calling. You may have seen this in action on sites like YellowPages.com &#8212; same idea and wonderfully easy with Twilio. In the near future, I&#8217;ll share a few more technical examples and other capabilities of the <a href="http://github.com/webficient/twilio/tree/master">Twilio gem</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=x0dLrQbtTpk:6yqcjNUf6Lc:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=x0dLrQbtTpk:6yqcjNUf6Lc:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=x0dLrQbtTpk:6yqcjNUf6Lc:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=x0dLrQbtTpk:6yqcjNUf6Lc:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/x0dLrQbtTpk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/06/22/hello-this-is-your-rails-app-calling-you/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/06/22/hello-this-is-your-rails-app-calling-you</feedburner:origLink></item>
		<item>
		<title>New Relic and Webficient Deliver Application Management Ready Images in Amazon Cloud</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/dOQNs56y8rY/new-relic-and-webficient-deliver-application-management-ready-images-in-amazon-cloud</link>
		<comments>http://www.webficient.com/2009/06/04/new-relic-and-webficient-deliver-application-management-ready-images-in-amazon-cloud#comments</comments>
		<pubDate>Thu, 04 Jun 2009 16:05:07 +0000</pubDate>
		<dc:creator>Phil Misiowiec</dc:creator>
		
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=821</guid>
		<description><![CDATA[Integration of New Relic RPM agent within the Webficient Amazon Machine Image (AMI) enables instant monitoring for Rails applications in the cloud.
FOLSOM and SAN FRANCISCO, CALIFORNIA – June 4, 2009 –Webficient, a leading web application development firm, and New Relic, the leading provider of software as a service application performance management solutions, today announced the [...]]]></description>
			<content:encoded><![CDATA[<p><em>Integration of New Relic RPM agent within the Webficient Amazon Machine Image (AMI) enables instant monitoring for Rails applications in the cloud.</em></p>
<p>FOLSOM and SAN FRANCISCO, CALIFORNIA – June 4, 2009 –Webficient, a leading web application development firm, and New Relic, the leading provider of software as a service application performance management solutions, today announced the integration of New Relic&#8217;s RPM™ Rails application performance management software with an Amazon Machine Image (AMI) created by Webficient. The AMI created by Webficient is optimized for deployment of Ruby on Rails applications running in the Amazon Elastic Computing Cloud (EC2.) </p>
<p>&#8220;We have been impressed with New Relic’s RPM for monitoring and troubleshooting applications for our clients. They provide the ideal tool for managing applications in the cloud,” said Phil Misiowiec, Founder of Webficient. “We concluded others using our AMI would find RPM as valuable as we do. So we included the RPM agent plugin in the AMI and enabled automatic account creation. This will make our AMI even better.”</p>
<p>&#8220;When we looked at AMI’s for Rails, we found that the Webficient AMI was rated 5 Stars (highest) by EC2 customers who had used it,” said Bill Lapcevic, vice president of business development at New Relic. “So we are very pleased that Webficient has included RPM in the AMI to provide instant application management for their users.”</p>
<p><strong>Availability</strong><br />
The Webficient AMI is <a href="http://www.webficient.com/amazon-ec2-amis">available immediately</a>. AMI users who already have an RPM account can enter their license key when prompted. New customers can create a free RPM Lite account. </p>
<p><strong>About Webficient</strong><br />
Webficient enables businesses to lower the costs of developing and maintaining their technology platform through “on demand” programming services and by utilizing agile practices and open source technologies. They build, maintain, and improve Web sites and applications for customers such as ad agencies, sole proprietors, and start-ups. They are located in Folsom, California and can be found on the web at <a href="http://www.webficient.com">www.webficient.com</a>.</p>
<p><strong>About New Relic</strong><br />
New Relic, Inc. offers RPM™, a Rails Performance Management solution that enables developers to quickly and cost effectively detect, diagnose, and fix application performance problems in real time. More than 1,800 top Rails organizations use New Relic RPM to monitor and optimize 18,000 production application instances, including customers 37signals, AboutUs.org, iOffer, Getty Images, GitHub, LightHouse, Liz Claiborne, Intuit, Starwood Hotels, Butterball Turkey, Ourstage, and Shopify; and partners Engine Yard, Pivotal Labs, Blue Box, RightScale and Sun. Funded by Benchmark Capital and Trinity Ventures, New Relic is a private company headquartered in San Francisco, California, USA. To learn more about New Relic, visit <a href="http://www.newrelic.com">www.newrelic.com</a>.</p>
<p>Press Contacts:</p>
<p>Phil Misiowiec<br />
Webficient<br />
<a href="mailto:phil@webficient.com">phil@webficient.com</a></p>
<p>Stephanie Breslin for New Relic<br />
650-248-9590<br />
<a href="mailto:stephanie@newrelic.com">stephanie@newrelic.com</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=dOQNs56y8rY:sj6OEqt_ox4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=dOQNs56y8rY:sj6OEqt_ox4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=dOQNs56y8rY:sj6OEqt_ox4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=dOQNs56y8rY:sj6OEqt_ox4:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/dOQNs56y8rY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/06/04/new-relic-and-webficient-deliver-application-management-ready-images-in-amazon-cloud/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/06/04/new-relic-and-webficient-deliver-application-management-ready-images-in-amazon-cloud</feedburner:origLink></item>
		<item>
		<title>Named Routes Can Be Used for Static Content Too</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/QDY-xjjoJwA/named-routes-can-be-used-for-static-content-too</link>
		<comments>http://www.webficient.com/2009/06/02/named-routes-can-be-used-for-static-content-too#comments</comments>
		<pubDate>Tue, 02 Jun 2009 22:16:26 +0000</pubDate>
		<dc:creator>Phil Misiowiec</dc:creator>
		
		<category><![CDATA[Tips]]></category>

		<category><![CDATA[absolute paths]]></category>

		<category><![CDATA[static content]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=804</guid>
		<description><![CDATA[Sometimes you have content under /public that might need to be referenced directly instead of through a controller. Or you may need to expose an asset using an absolute path (let&#8217;s say you&#8217;re using an external service that points to these assets). The last thing you want to be doing is embedding absolute paths in [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you have content under /public that might need to be referenced directly instead of through a controller. Or you may need to expose an asset using an absolute path (let&#8217;s say you&#8217;re using an external service that points to these assets). The last thing you want to be doing is embedding absolute paths in your code. </p>
<p>Just add this mapping in routes.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">map.<span style="color:#9900CC;">audio</span> <span style="color:#996600;">'/audio/:item'</span>, <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span></pre></div></div>

<p>And now you get Rails&#8217; automatic path helpers:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">audio_path<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'greeting.mp3'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># &quot;/audio/greeting.mp3&quot;</span>
audio_url<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'greeting.mp3'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># &quot;http://www.example.com/audio/greeting.mp3&quot;</span></pre></div></div>

<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=QDY-xjjoJwA:tY__CVUQhp8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=QDY-xjjoJwA:tY__CVUQhp8:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=QDY-xjjoJwA:tY__CVUQhp8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=QDY-xjjoJwA:tY__CVUQhp8:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/QDY-xjjoJwA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/06/02/named-routes-can-be-used-for-static-content-too/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/06/02/named-routes-can-be-used-for-static-content-too</feedburner:origLink></item>
		<item>
		<title>Your Javascript is Slow</title>
		<link>http://feedproxy.google.com/~r/Webficient/~3/r23CwzLPCAI/slow-javascript</link>
		<comments>http://www.webficient.com/2009/05/04/slow-javascript#comments</comments>
		<pubDate>Tue, 05 May 2009 05:32:51 +0000</pubDate>
		<dc:creator>Dave Woodward</dc:creator>
		
		<category><![CDATA[Solutions]]></category>

		<category><![CDATA[guest blogger]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[jquery]]></category>

		<category><![CDATA[performance]]></category>

		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.webficient.com/?p=783</guid>
		<description><![CDATA[And I&#8217;m not saying that because you&#8217;re using Internet Explorer. Chances are pretty good that if you&#8217;re a Rails developer, your Javascript skills are not your strongest, rather your Ruby skills are. And you use Firefox or Safari while developing, so things seem fast enough to you.  Its going to be okay though, I&#8217;ll give [...]]]></description>
			<content:encoded><![CDATA[<p>And I&#8217;m not saying that because you&#8217;re using Internet Explorer. Chances are pretty good that if you&#8217;re a Rails developer, your Javascript skills are not your strongest, rather your Ruby skills are. And you use Firefox or Safari while developing, so things seem fast enough to you.  Its going to be okay though, I&#8217;ll give you a few pointers to speed it up.</p>
<p>You probably learned a lot of what you know about Javascript and how to write it through various examples on the blogs of your favorite Rails gurus, or maybe by copying examples from <a href="http://prototypejs.org">prototypejs.org</a>.</p>
<p>The problem with a lot of Javascript examples and snippets on the Internet are that they work fine for &#8220;proof of concept&#8221; samples with a dozen objects, but they fall flat on their face performance-wise once the code is in a real application with hundreds or thousands of elements.  Frequently this is a result of the code never being run with a sizeable amount of data.  Also keep in mind that the code you&#8217;ll find on blogs was written in a, &#8220;look what I just did,&#8221; moment and usually does not include the, &#8220;oh crap, sorry folks!&#8221; feedback after being put in production.</p>
<p>The Prototype.js library is a great library that has opened a lot of doors, but it can be very easy to write slow Javascript with it.  The problem isn&#8217;t the library itself, rather its how easy it makes it to write complicated code (kind of like Rails and SQL queries!).  You may start off with an example you found that iterates over some DOM elements and passes them to a Class to get instantiated (having a &#8220;good&#8221; Object Oriented Design).  Then you later come back to the code and add a few more things inside the loop and to the class, and before you know what happened, IE users are calling support complaining about their browser freezing for 60s after a page load!</p>
<p>So here is a basic skeleton of something you might see:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Create a nice self-contained class for our menu behaviors</span>
<span style="color: #003366; font-weight: bold;">var</span> SlowMenu <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  initialize<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>menu_el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">menu</span> <span style="color: #339933;">=</span> menu_el;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">menu</span>.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #006600; font-style: italic;">//click behavior});</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// Add the behaviors to all DOM elements</span>
<span style="color: #003366; font-weight: bold;">var</span> flyouts <span style="color: #339933;">=</span> $$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li.flyout_menu'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> SlowMenu<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>A project I worked on had a user directory with hundreds of elements on the page that the Javascript touched.  Just a simple decision such as creating a new object with attached behaviors for each element brought the browser to a crawl.  Speeding it up required leveraging Javascript&#8217;s functional nature and keeping the design going with the grain of Javascript&#8217;s out-of-the-box features.</p>
<p>It is pretty common to say, &#8220;Ok, I need a flyout menu&#8230; I&#8217;m going to make a class that contains the menu behaviors, then grab all the links on the page and make new objects with them.&#8221;  This results in Javascript fail.  In the example above, if you decide to show 100 menus on the page, and each flyout menu has, say, a dozen links in it that all need behaviors, you&#8217;re up to thousands of objects for simple functionality!</p>
<p>A seemingly better way is to have one loop that just attaches one behavior to each element.  That way you&#8217;re at least not instantiating new classes inside the loop, right?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li.flyout_menu'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  el.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #006600; font-style: italic;">//click behavior})</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Actually, the best way is to not even mess with the elements themselves!  Instead just let any events you&#8217;re interested in bubble up the DOM and only make one top-level function that knows what to do by inspecting the target of an event.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'menu_container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">observe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">element</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hasClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'flyout_menu'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Menu behaviors go here.</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This sample code only observes click events on the parent element, and inspects the incoming elements on the fly.  Instead of performing all of the necessary computation when the Javascript is first executed by the browser, this will delay any computation until the last possible moment the user needs it.  This will make page loads feel snappy for the user, and save their CPU if they&#8217;re unfortunate enough to be using IE.  Your browser is already downloading and interpreting the script so you want to avoid doing any more work while this is being done.</p>
<p>These design choices don&#8217;t apply to just Prototype.js.  This style is just as effective when using jQuery.  Here is how the above snippet would look using jQuery:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#menu_container'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'flyout_menu'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Menu behaviors here</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>So when you&#8217;re faced with some slow Javascript, chance are that you&#8217;ll deny it.  I responded that way the first few times this happened to me.  I use Firefox for most of my development, and the latest Firefoxes (and Safaris, and now Chrome) have really fast Javascript interpreters.  You may not even notice a delay, whereas someone running IE will experience the same code taking a second or more to run.</p>
<p>While developing and benchmarking Javascript, I&#8217;ve found IE to generally be 10-100x slower than Firefox running the same code.  Make sure to open up VirtualBox and feel the pain of IE every once in awhile when developing.</p>
<p>Ultimately, the best way to speed up your Javascript is to write less Javascript! A majority of the time you don&#8217;t need classes and inheritance in your Javascript code like you are used to with languages that have more traditional object systems.  Keep in mind first of all that events bubble up the DOM (if you&#8217;re using a library), and consider turning your code&#8217;s design inside-out.  If you&#8217;re attaching the same behavior to every element, try instead to have one instance of the behavior that can be parameterized with an element.</p>
<p>The other thing to remember is that modern applications dealing with thousands and millions of objects are going to be running on IE6 (or IE7 if you&#8217;re sane) so try give those users a decent enough experience.  Though if you can afford to tell them to take a hike, have a beer for me!</p>
<p><em>Our guest blogger series recognizes talented developers who&#8217;ve been around the block or two and offer practical advice on today&#8217;s Web dev challenges. They are also available for hire as part of the Webficient project team. <a href="/contact">Contact Webficient Now</a></em></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Webficient?a=r23CwzLPCAI:tS_pNSHAXHE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=r23CwzLPCAI:tS_pNSHAXHE:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/Webficient?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Webficient?a=r23CwzLPCAI:tS_pNSHAXHE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Webficient?i=r23CwzLPCAI:tS_pNSHAXHE:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Webficient/~4/r23CwzLPCAI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.webficient.com/2009/05/04/slow-javascript/feed</wfw:commentRss>
		<feedburner:origLink>http://www.webficient.com/2009/05/04/slow-javascript</feedburner:origLink></item>
	</channel>
</rss>
