Undefined Range : Category ruby-on-rails, everything about ruby-on-rails http://undefinedrange.com/category/ruby-on-rails.rss en-us 40 RSpec + Rails 2.2 = Neglected Rescue Handlers <p>Upgrading a Rails project to version 2.2 broke controller specs and I couldn&#8217;t figure out why. The problem i was facing was that the default Rails exception rescuing would not work in the test environment.</p> <p>I&#8217;m seeing alot of something like the following with &#8216;rake spec&#8217;:</p> <div class="typocode"><pre><code class="typocode_default ">ActiveRecord::RecordNotFound in 'PagesController responding to GET show should not find the record' ActiveRecord::RecordNotFound</code></pre></div> <p>An exception is raised during the page request as expected but it prematurely ends the spec instead of going through the rescue handlers I&#8217;ve setup through rescue_from(). I had no idea if this new behavior is expected or a regression. Sure I could do&#8230;</p> <div class="typocode"><pre><code class="typocode_default ">lambda{do_get}.should raise_error(ActiveRecord::RecordNotFound)</code></pre></div> <p>...but I wasn&#8217;t looking forward to updating the spec suite to conform. Also, it wouldn&#8217;t be testing the rescue handler behaviors. Initially, searching via the almighty Google yield no answers. Stepping through code execution using rdebug revealed that RSpec <a href="http://github.com/dchelimsky/rspec-rails/tree/master/lib/spec/rails/extensions/action_controller/rescue.rb">modifies Rails to not rescue exceptions</a> as it normally would. However, the modification also includes a simple way to revert to the default behavior. Score! So now I&#8217;m doing:</p> <div class="typocode"><pre><code class="typocode_default ">before(:each) do controller.use_rails_error_handling! end</code></pre></div> <p>With that solved, I now know which search terms to use and, lo and behold, Google reveals that <a href="http://www.mail-archive.com/rspec-users@rubyforge.org/msg05965.html">somebody had the same problem and already figured it out</a>. Google, why had you forsaken me earlier?</p> <p>Also, there is <a href="http://rspec.lighthouseapp.com/projects/5645/tickets/85-11818-have-mode-for-rails-error-handling">more context</a> if you&#8217;re interested.</p> Thu, 04 Dec 2008 17:01:00 -0800 urn:uuid:46c1385b-d559-49a0-b74e-24d13573184c blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2008/12/04/rspec-rails-2-2-neglected-rescue-handlers#comments Ruby on Rails rspec rails rescue exception http://www.undefinedrange.com/trackbacks?article_id=rspec-rails-2-2-neglected-rescue-handlers&day=04&month=12&year=2008 http://www.undefinedrange.com/2008/12/04/rspec-rails-2-2-neglected-rescue-handlers Git Submodules, External Repositories, and Deployment <p>Even though I switched a few of my projects from Subversion to Git a couple of months ago, I still had lots to learn. So I dedicated last weekend to use submodules for Rails plugins. Unfortunately, it wasn&#8217;t the quick conversion I thought it would be but it turned out for the best.</p> <h3>Git Submodules</h3> External repository references was handled so elegantly by <a href="http://piston.rubyforge.org/">Piston</a> for Subversion repositories that I was willing to wait for its Git support. But I came across <a href="http://daniel.collectiveidea.com/blog/2008/4/12/git-submodules-part-2">this blog post</a> that dispelled misconceptions and it turns out submodules act just like Piston. In short, submodules will keep track of which revision it is on and won&#8217;t hit external resources unless it has to. Sweet! Submodules are great and simple to work with on the development side. Do: <div class="typocode"><pre><code class="typocode_default "> git submodule add git://github.com/rails/acts_as_list.git vendor/plugins/acts_as_list git submodule init git submodule update</code></pre></div> <p>And you&#8217;re golden. But there is still much to do. In order to deploy efficiently with Capistrano, I&#8217;d needed to do a &#8220;set :deploy_via, :remote_cache&#8221;. Onwards in search of a Internet accessible Git repository that I can call my own.</p> <h3>Git Repository Hosting</h3> <p>Enter <a href="http://railsplayground.com">Railsplayground.com Ruby on Rails Hosting</a>. They have been hosting this blog for several years and have always been great for shared hosting. Bonus points for <a href="http://railsplayground.com/sub_trac.html">free Subversion and Trac hosting</a>, up to 1GB disk space, with any hosting account.</p> <p>Little did I know that they have been doing Git repository hosting for the past several months. Their homepage doesn&#8217;t mention it although their forum as well as their <a href="http://svnrepository.com/">sister site</a> do.</p> <p>If you&#8217;re thinking about their git repository hosting solution, there are a couple of things to keep in mind.</p> <p><span class="caps">PRO</span>: Custom control panel: easy to create/delete git repositories and manage user accounts that has access to them</p> <p><span class="caps">PRO</span>: Trac integration is underway</p> <p><strike><span class="caps">CON</span>: The only protocols they support are <span class="caps">HTTP</span> and <span class="caps">HTTPS</span>.</strike> Update: Railsplayground will support the Git protocol very soon. See comments below.</p> <p><strike><span class="caps">CON</span>: It requires creating a ~/.netrc with my login and password. I don&#8217;t want to store any passwords in plain text on my laptop so I symlinked the file to an <a href="http://www.knoxformac.com/">encrypted vault</a> where my project files are anyways.</strike> Update: Railsplayground will support the Git protocol over ssh very soon. See comments below.</p> <h3>MacPorts Pain</h3> I&#8217;m using MacPorts on my Macbook Pro to compile git. This setup worked fine before but I couldn&#8217;t access the newly created git repository. <div class="typocode"><pre><code class="typocode_default "> $ git push upload master fatal: git-push is not available for http/https repository when not compiled with USE_CURL_MULTI error: failed to push some refs to 'https://secure2.svnrepository.com/g_username/repository_name/'</code></pre></div> Unfortunately, MacPorts added in a patch to remove the <span class="caps">USE</span>_CURL_MULTI flag for <a href="https://trac.macports.org/ticket/15100">stability reasons</a>. Since the bug is triggered by a very large checkout on a powerpc mac, not applicable in my case, I removed the patch by editing the git Portfile. <div class="typocode"><pre><code class="typocode_default "> #/opt/local/var/macports/sources/rsync.macports.org/release/ports/devel/git-core/Portfile patchfiles patch-Makefile.diff patch-http.h.diff # Before patchfiles patch-Makefile.diff # After</code></pre></div> I&#8217;m not sure how to tell MacPorts to recompile git. I cheapened out and added a variant to force a recompile. <div class="typocode"><pre><code class="typocode_default "> sudo port deactivate git-core @1.6.0_1+doc+svn sudo port install git-core +doc +svn +bash_completion</code></pre></div> Once that was resolved, I encountered yet another error. <div class="typocode"><pre><code class="typocode_default "> $ git push upload master error: Cannot access URL https://secure2.svnrepository.com/username/repository_name/, return code 1 error: failed to push some refs to 'https://secure2.svnrepository.com/username/repository_name/'</code></pre></div> Turned out that curl needs to be compiled with the ssl variant. Normally, this should work: <div class="typocode"><pre><code class="typocode_default "> $ sudo port install curl +ssl ---&gt; Fetching curl-ca-bundle ---&gt; Attempting to fetch certdata-1.48.txt from http://lxr.mozilla.org/seamonkey/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1 ---&gt; Verifying checksum(s) for curl-ca-bundle Error: Checksum (md5) mismatch for certdata-1.48.txt Error: Checksum (sha1) mismatch for certdata-1.48.txt Error: Checksum (rmd160) mismatch for certdata-1.48.txt Error: Target org.macports.checksum returned: Unable to verify file checksums Error: The following dependencies failed to build: curl-ca-bundle Error: Status 1 encountered during processing.</code></pre></div> MacPorts should already have <a href="https://trac.macports.org/ticket/16386">this fixed</a> but just in case, I got around the error by skipping checksums. <div class="typocode"><pre><code class="typocode_default "> sudo port install curl +ssl checksum.skip=yes</code></pre></div> <h3>Deployment Issues</h3> Here&#8217;s the git related entries in my Capistrano deployment script: <div class="typocode"><pre><code class="typocode_default "> set :repository, &quot;https://secure2.svnrepository.com/g_username/project_name&quot; set :scm, 'git' set :deploy_via, :remote_cache set :branch, &quot;master&quot; set :git_shallow_clone, 1 set :git_enable_submodules, 1 default_environment['GIT_SSL_NO_VERIFY'] = 'true'</code></pre></div> But deployment failed midway: <div class="typocode"><pre><code class="typocode_default "> $ cap staging deploy Initialized empty Git repository in /home/username/project_name/shared/cached-copy/vendor/plugins/acts_as_list/.git/ github.com[0: 65.74.177.129]: errno=Connection timed out ** fatal: unable to connect a socket (Connection timed out) ** Clone of 'git://github.com/rails/acts_as_list.git' into submodule path 'vendor/plugins/acts_as_list' failed</code></pre></div> The server firewall was getting in the way so I punched a hole for the git protocol: <div class="typocode"><pre><code class="typocode_default "> iptables -I OUTPUT -p tcp --dport 9418 -j ACCEPT</code></pre></div> <h3>Sweet Oblivion</h3> With that all out of the way, I can now successfully deploy to the server the way I want to. Now I can get back to work :) Tue, 26 Aug 2008 00:41:00 -0700 urn:uuid:427af239-c39c-443d-b4c2-905452cf990d blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2008/08/26/git-submodules-external-repositories-and-deployment#comments Ruby on Rails Web iptables capistrano deployment curl macports hosting repository subversion piston railsplayground submodules git http://www.undefinedrange.com/trackbacks?article_id=git-submodules-external-repositories-and-deployment&day=26&month=08&year=2008 http://www.undefinedrange.com/2008/08/26/git-submodules-external-repositories-and-deployment RSpec Tips <p>Since I&#8217;ve been following <a href="http://weblog.jamisbuck.org/2007/1/29/testing-your-views">Jamis Buck&#8217;s advice</a> to limit the brittleness of view tests, development has been much more productive. Test the things that matter most else you&#8217;ll end up with diminishing returns and the week passed by without any productive work done.</p> <p>I&#8217;m taking one step further and I&#8217;ve decided not to write RSpec view specifications.</p> <p>I will still continue to occasionally test important view elements. It just isn&#8217;t worth the extra baggage of writing and maintaining another set of specs for every single view. I&#8217;m feeling the pain there and I&#8217;m taking steps to eliminating it.</p> <p>But it is also painful to sprinkle &#8220;integrate_views&#8221; syntactic vinegar throughout controller specifications. I don&#8217;t want to do without that since I would still get notified of template errors. Unfortunately, this is not the recommended behavior and not much was written on the subject.</p> <p><a href="http://www.marklunds.com/articles/one/367">Peter Marklund</a> has a solution but it didn&#8217;t work for me. Maybe because RSpec was updated since his blog post was written but it is more likely that I&#8217;m missing something obvious. However, I dug around the plugin and was happy to discover a simple method override that worked. Add to spec_helper.rb:</p> <div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">module </span><span class="module">Spec</span> <span class="keyword">module </span><span class="module">Rails</span> <span class="keyword">module </span><span class="module">Example</span> <span class="keyword">class </span><span class="class">ControllerExampleGroup</span> <span class="keyword">module </span><span class="module">ControllerInstanceMethods</span> <span class="keyword">def </span><span class="method">integrate_views?</span> <span class="constant">true</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="keyword">end</span></code></pre></div> <p>btw, while browsing the RSpec mailing list, I came across a post that explains <a href="http://rubyforge.org/pipermail/rspec-users/2008-March/006509.html">a way to slightly speed up test runs</a>.</p> Tue, 15 Apr 2008 14:53:00 -0700 urn:uuid:d7f8c48b-fb7c-4daf-bebb-6b14e901447d blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2008/04/15/rspec-tips#comments Ruby on Rails rspec rails tests override http://www.undefinedrange.com/trackbacks?article_id=rspec-tips&day=15&month=04&year=2008 http://www.undefinedrange.com/2008/04/15/rspec-tips Rake task runs both Test::Unit and RSpec tests <p>I&#8217;m in the middle of converting tests from Test::Unit to RSpec in an older Rails project. So far so good although I&#8217;m sure that would change as I dive into more complicated test cases. Still, I&#8217;m hoping the pain is worth it for the multiple contexts and lessened fixture reliance.</p> <p>A nifty feature that I accidentally stumbled upon is that the default rake task would run the test in both test and spec directories. It&#8217;s not like I have to exclusively choose one or the other.</p> <p>That opens up lots of test conversion options. I could convert the tests all at once or a little at a time. Or I could forget about conversion completely and merely write new tests in RSpec.</p> <p>It&#8217;s little surprises like this that brightens up my workday.</p> <p><span class="caps">UPDATE</span> (FEB8,08): RSpec tests won&#8217;t run unless all Test::Unit tests passes.</p> Fri, 18 Jan 2008 19:04:00 -0800 urn:uuid:7abea194-30e6-48ac-a4dd-42f8100a8a87 blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2008/01/18/rake-task-runs-both-test-unit-and-rspec-tests#comments Ruby on Rails rake rails test bdd rspec http://www.undefinedrange.com/trackbacks?article_id=rake-task-runs-both-test-unit-and-rspec-tests&day=18&month=01&year=2008 http://www.undefinedrange.com/2008/01/18/rake-task-runs-both-test-unit-and-rspec-tests Early Christmas Gift: Rails 2.0 Release Imminent <p>Rejoice, for Rails <a href="http://dev.rubyonrails.org/changeset/8321">Changeset 8321</a> indicates today as the day Rails 2.0 will be released.</p> Thu, 06 Dec 2007 13:53:00 -0800 urn:uuid:3dbbf7a0-fe26-4a9f-96b5-4f7c13317ec2 blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2007/12/06/early-christmas-gift-rails-2-0-release-imminent#comments Ruby on Rails http://www.undefinedrange.com/trackbacks?article_id=early-christmas-gift-rails-2-0-release-imminent&day=06&month=12&year=2007 http://www.undefinedrange.com/2007/12/06/early-christmas-gift-rails-2-0-release-imminent Rails String Core Extensions Cheatsheet <p><a href="http://undefinedrange.com/files/strings_cheatsheet.png"><img src="http://undefinedrange.com/files/strings_cheatsheet_thumbnail.png" /></a></p> <p><a href="http://undefinedrange.com/files/strings_cheatsheet.png"><span class="caps">PNG</span></a> <a href="http://undefinedrange.com/files/strings_cheatsheet.pdf"><span class="caps">PDF</span></a></p> <p><br /> <br /> References<br /> <a href ="http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Access.html">/CoreExtensions/String/Access.html</a><br /> <a href ="http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Conversions.html">/CoreExtensions/String/Conversions.html</a><br /> <a href="http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html">/CoreExtensions/String/Inflections.html</a><br /> <a href="http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Iterators.html">/CoreExtensions/String/Iterators.html</a><br /> <a href="http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/StartsEndsWith.html">/CoreExtensions/String/StartsEndsWith.html</a><br /> <a href="http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Unicode.html">/CoreExtensions/String/Unicode.html</a><br /> <a href="http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Time/Conversions.html">/CoreExtensions/Time/Conversions.html</a><br /> <a href="http://www.ruby-forum.com/topic/57923">Changing default date format in Rails @ Ruby Forum</a><br /> <a href="http://errtheblog.com/post/44">Rails Rubyisms Advent @ errtheblog.com</p> Mon, 16 Jul 2007 15:43:00 -0700 urn:uuid:3242ac3a-d6a6-4724-ae0a-183838fe02c4 blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2007/07/16/rails-string-core-extensions-cheatsheet#comments Ruby on Rails Portfolio cheatsheet rails string extensions inflections unicode http://www.undefinedrange.com/trackbacks?article_id=rails-string-core-extensions-cheatsheet&day=16&month=07&year=2007 http://www.undefinedrange.com/2007/07/16/rails-string-core-extensions-cheatsheet Revisory Project Reflection <p><a href="http://undefinedrange.com/files/revisory.mov"><img src="http://undefinedrange.com/files/revisory.jpg" /></a></p> <p><a href="http://undefinedrange.com/files/revisory.mov">Demo movie [1MB].</a> Revisory: Summer 2006. <br /></p> <h3>Description</h3> <p>Online discussion space on graphical assets or unpolished concepts through message threads and image manipulation tools. Revisory facilitates non-linear design processes between people in distributed teams to better explore brainstormed ideas.</p> <h3>Development</h3> The primary goal of this project to begin an introductory exploration of various web scripting technologies: <ul> <li>Ruby on Rails</li> <li><span class="caps">HTML5</span> canvas element</li> <li>Prototype and script.aculo.us frameworks</li> <li>Javascript</li> </ul> <p>The initial technical feasibility assessment concluded that the project was not commercially viable. A crucial factor was that the canvas element lacked current and future support by Internet Explorer. However, it was included in the latest version of Firefox, Safari, and Opera at the time. A decision was made to build a simple conceptional tech prototype as a learning experience through a project lifecycle.</p> <p>Athestics is based on the simplicity of a blank canvas. The low key design encourages quick freeform sketches regardless of artistic ability. With its monochrome nature, the site design should fall back into the background and stand in contrast to the color possibilities of the user generated content.</p> <h3>End Results</h3> <p>Some testing was conducted near the end of this experiment. Revisory was utilized in highlighting the various design considerations given to a dynamic <span class="caps">PDF</span> document for review by a client. We found that keeping images updated and in context as per discussion point through appropriate zooming, panning, and highlighting held great value. The web app was relevant when visual communication was desired.</p> <p>One area needing of improvement is in the generalized drawing tools. Most marks were kept simple to merely augment the existing image. Also, tools do not suggest efficient uses. Instead, future iterations should involve testing for more common behaviors to automate before any additional development starts.</p> <hr /> Image used in demo is <a href="http://www.deviantart.com/deviation/214726/">&#8220;More Fun Than Tekken&#8221;</a> by Sevenflow and Kultdesign. Sun, 08 Jul 2007 16:59:00 -0700 urn:uuid:48472c82-2e0e-406a-8dd9-0ebdaba4734b blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2007/07/08/revisory-project-reflection#comments Ruby on Rails Design Portfolio concepts canvas draw http://www.undefinedrange.com/trackbacks?article_id=revisory-project-reflection&day=08&month=07&year=2007 http://www.undefinedrange.com/2007/07/08/revisory-project-reflection Automating Credentials in Capistrano <p>Here&#8217;s a small tip that&#8217;s working well for me. If the user and password variables are set in the capistrano deployment scripts, then capistrano won&#8217;t prompt for the credentials anymore. It&#8217;s best to store them in ~/.caprc so you don&#8217;t commit your credentials to your <span class="caps">SCM</span> for all to see.</p> <p>I&#8217;ve accumulated several different sets of credentials now that I&#8217;ve been working on several rails projects with different teams with their own login naming conventions. The simple assignment in .caprc just won&#8217;t do anymore.</p> <p>Well, .caprc is just ruby code. Additionally, <a href="http://manuals.rubyonrails.com/read/chapter/102">proc&#8217;s can be assigned to values to defer code execution until the value is first accessed</a>. That way, the proc will have access to the application value in it&#8217;s local scope. Then we could do something like the following in .caprc:</p> <div class="typocode"><pre> set :user, Proc.new { case application when 'project1': 'user1' when 'project2': 'user2' end } set :password, Proc.new { case application when 'project1': 'pass1' when 'project2': 'pass2' end } set :svn_username, Proc.new { case application when 'project1': 'svn_user1' when 'project2': 'svn_user2' end } </pre></div> Wed, 13 Jun 2007 13:40:00 -0700 urn:uuid:d27519f7-e4cc-4e1f-9c61-31502f946630 blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2007/06/13/automating-credentials-in-capistrano#comments Ruby on Rails capistrano login password caprc http://www.undefinedrange.com/trackbacks?article_id=automating-credentials-in-capistrano&day=13&month=06&year=2007 http://www.undefinedrange.com/2007/06/13/automating-credentials-in-capistrano Active Resource Cheatsheet <p><a href="http://undefinedrange.com/files/active_resource.png"><img src="http://undefinedrange.com/files/active_resource_thumbnail.png" ></a><br /> <a href="http://undefinedrange.com/files/active_resource.png"><span class="caps">PNG</span></a> | <a href="http://undefinedrange.com/files/active_resource.pdf"><span class="caps">PDF</span></a> <br /> <br /> <br /></p> <p>References<br /> <a href="http://www.ryandaigle.com/articles/2006/06/30/whats-new-in-edge-rails-activeresource-is-here">The Ins and Outs of ActiveResource</a><br /> <a href="http://www.ryandaigle.com/articles/2007/4/26/what-s-new-in-edge-rails-activeresource-gets-custom-methods">ActiveResource Gets Custom Methods</a><br /> <a href="http://www.eribium.org/?p=71">Active Resource Tutorial</a><br /> <a href="http://weblog.techno-weenie.net/2006/12/12/taking-ares-out-for-a-test-drive">Taking ARes Out For a Test Drive</a><br /> active resource rdoc</p> Wed, 16 May 2007 20:50:00 -0700 urn:uuid:72dcae9f-b0f6-48b4-bc99-2bb966a39554 blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2007/05/16/active-resource-cheatsheet#comments Ruby on Rails Portfolio record restful edge cheatsheet resource active http://www.undefinedrange.com/trackbacks?article_id=active-resource-cheatsheet&day=16&month=05&year=2007 http://www.undefinedrange.com/2007/05/16/active-resource-cheatsheet Goodbye rhtml <p>What&#8217;s the one thing that&#8217;s more sacred to a man than anything else in the world? <a href="http://haml.hamptoncatlin.com/"><span class="caps">HAML</span></a>. I&#8217;ve converted one template over and I&#8217;m sold. Just a few simple rules was enough to get started and half of them are based on standard <span class="caps">CSS</span> selectors.</p> <p>With <span class="caps">HAML</span>, &lt; and &gt;&#8217;s are refreshingly gone but at the same time I felt like I lost a thumb. Angle bracket clusters, as cluttered as they are, were useful for highlighting the various elements on the page for easy scanning. Using a <a href="http://haml.lucky-dip.net/">syntax highlighting plugin</a> should help ease the transition.</p> <p>Regardless, it just looks so clean. I&#8217;m looking forward to working more with <span class="caps">HAML</span>.</p> Sat, 28 Apr 2007 21:57:00 -0700 urn:uuid:8cd8a185-c626-4852-9aac-c009598faaee blog@undefinedrange.com (Vincent Woo) http://www.undefinedrange.com/2007/04/28/goodbye-rhtml#comments Ruby on Rails HAML templates view html http://www.undefinedrange.com/trackbacks?article_id=goodbye-rhtml&day=28&month=04&year=2007 http://www.undefinedrange.com/2007/04/28/goodbye-rhtml