<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
  <title>it's a ruby thing - Home</title>
  <id>tag:its.arubything.com,2009:mephisto/</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  
  <link href="http://its.arubything.com/" rel="alternate" type="text/html" />
  <updated>2009-03-05T01:38:38Z</updated>
  <link rel="self" href="http://feeds.feedburner.com/ItsARubyThing" type="application/atom+xml" /><entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-03-05:211</id>
    <published>2009-03-05T01:34:00Z</published>
    <updated>2009-03-05T01:38:38Z</updated>
    <category term="Announcements" />
    <category term="blog" />
    <link href="http://its.arubything.com/2009/3/5/moving-to-brandon-dimcheff-com" rel="alternate" type="text/html" />
    <title>Moving to brandon.dimcheff.com</title>
<content type="html">
            &lt;p&gt;I’ve drank the static site generation kool-aid and decided to move my blog to Jekyll-powered &lt;a href="http://brandon.dimcheff.com"&gt;brandon.dimcheff.com&lt;/a&gt;.  My new blog has all the content of this one, but this one will no longer receive new posts.  This site will stay up for a while, but will eventually redirect to the new site.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-03-01:209</id>
    <published>2009-03-01T06:01:00Z</published>
    <updated>2009-03-01T06:11:50Z</updated>
    <category term="Announcements" />
    <category term="Projects" />
    <category term="merb" />
    <category term="tasteb.in" />
    <link href="http://its.arubything.com/2009/3/1/tasteb-in-is-up" rel="alternate" type="text/html" />
    <title>tasteb.in is up!</title>
<content type="html">
            &lt;p&gt;I’ve been working on a Merb-based recipe paste site for the past couple weeks called &lt;a href="http://tasteb.in"&gt;tasteb.in&lt;/a&gt;.  It’s extremely simple for now.  It lets you paste recipes in a very simple format and they show up on the site.  You can access your recipes and all recipes on the site.  And that’s about it.&lt;/p&gt;

&lt;p&gt;I’ll be going into more details about how tasteb.in is built and where I’d like to take the site, but for now, just play with it if you like.  Drop me a line with any problems you have, or features you would like to see.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-02-14:206</id>
    <published>2009-02-14T06:48:00Z</published>
    <updated>2009-02-14T16:23:52Z</updated>
    <category term="Projects" />
    <category term="filesystem" />
    <category term="ruby" />
    <category term="sandbox" />
    <category term="test" />
    <link href="http://its.arubything.com/2009/2/14/play-in-the-sandbox" rel="alternate" type="text/html" />
    <title>Play in the Sandbox</title>
<content type="html">
            &lt;p&gt;I needed to test some code that reads and writes files on the filesystem.  I got sick of manually setting up a scratch area for my test files and cleaning them up when I was done, so I created &lt;a href="http://github.com/bdimcheff/sandbox/tree/master"&gt;Sandbox&lt;/a&gt; to keep my tests DRY.&lt;/p&gt;

&lt;p&gt;See the Sandbox readme for more information, but the basic idea is this:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;10&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span class="c"&gt;# inside a test somewhere&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class="co"&gt;Sandbox&lt;/span&gt;.play &lt;span class="r"&gt;do&lt;/span&gt; |path|&lt;tt&gt;
&lt;/tt&gt;  file = &lt;span class="co"&gt;File&lt;/span&gt;.join(path, &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;  &lt;span class="co"&gt;FileUtils&lt;/span&gt;.touch(file)  &lt;span class="c"&gt;# build a sand castle&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;  assert &lt;span class="co"&gt;File&lt;/span&gt;.exists?(file)  &lt;span class="c"&gt;# test whatever you want in here&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class="r"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;assert !&lt;span class="co"&gt;File&lt;/span&gt;.exists?(file)  &lt;span class="c"&gt;# path has been deleted&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;The project is currently only on &lt;a href="http://github.com/bdimcheff/sandbox/tree/master"&gt;github&lt;/a&gt;, but it will be on RubyForge once approved.  If you want to use it now, you have to use the &lt;a href="http://gems.github.com/"&gt;github gems source&lt;/a&gt; or install it from source.&lt;/p&gt;

&lt;h2&gt;Future plans&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Optionally &lt;code&gt;Dir.chdir&lt;/code&gt; to the temporary directory for even easier usage&lt;/li&gt;
&lt;li&gt;Supply a template directory/archive to be used for the base state of the sandbox&lt;/li&gt;
&lt;/ul&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-02-05:198</id>
    <published>2009-02-05T23:40:00Z</published>
    <updated>2009-02-12T18:08:53Z</updated>
    <category term="Howto" />
    <category term="howto" />
    <category term="java" />
    <category term="koans" />
    <category term="ruby" />
    <category term="tutorial" />
    <link href="http://its.arubything.com/2009/2/5/how-to-learn-ruby" rel="alternate" type="text/html" />
    <title>How to learn Ruby</title>
<content type="html">
            &lt;p&gt;Several people have asked me for tutorials, books, and other resources to help them learn Ruby, so I’ve attempted to compile a list of resources I feel are particularly valuable for beginning Rubyists.  None of these resources are Rails-specific, but being a good Ruby programmer will obviously help you use Rails more effectively.  If there is interest, I will compile a similar (and likely longer) list of Rails resources.&lt;/p&gt;

&lt;p&gt;Also, if I’ve missed anything that helped you learn or teach Ruby, please leave a comment and I will add it to the page.  I am especially interested in resources that beginning Rubyists have found useful.&lt;/p&gt;

&lt;p&gt;For these resources, I assume you already have programming experience and are attempting to pick up Ruby to widen your skillset.  If you’re new to programming entirely, check out Chris Pine’s &lt;a href="http://pine.fm/LearnToProgram/"&gt;Learn to Program&lt;/a&gt; online tutorial.&lt;/p&gt;

&lt;h2&gt;Online Resources&lt;/h2&gt;

&lt;p&gt;The following is a list of freely available online resources that I’ve found particularly helpful for new Rubyists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://github.com/edgecase/ruby_koans/tree/master"&gt;Ruby Koans&lt;/a&gt;:&lt;/strong&gt; The newest kid on the block (no pun intended) that’s taking github by storm is EdgeCase’s &lt;a href="http://github.com/edgecase/ruby_koans/tree/master"&gt;Ruby Koans&lt;/a&gt;.  The &lt;a href="http://en.wikipedia.org/wiki/Koan"&gt;Koans&lt;/a&gt; are essentially a series of failing test cases that you need to figure out how to fix in order to move on to the next step.  The best way to learn anything is by doing, and this tutorial will get you programming in no time flat.&lt;/p&gt;

&lt;p&gt;Note that the point in the exercise is &lt;strong&gt;not&lt;/strong&gt; to fix all the tests as fast as possible.  In order to get the benefit of this exercise, you must read and understand what the code is showing you.  Complete each Koan one by one.  Run your tests after each time you change your code.&lt;/p&gt;

&lt;p&gt;I think the best way to use these Koans is to get in a small (2-3 person) group and take turns solving the problems.  Discuss what you are doing with each other.  Don’t take anything for granted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://poignantguide.net/ruby/"&gt;Why’s Poignant Guide&lt;/a&gt;:&lt;/strong&gt; Why the Lucky Stiff put together an extremely silly tutorial on Ruby.  While it may seem a bit campy, it really is a good tutorial.  And it’s a lot of fun!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://rubyquiz.com/"&gt;Ruby Quiz&lt;/a&gt;:&lt;/strong&gt; Once you get your feet wet, you might want to check out the Ruby Quiz archives.  There are over 150 small programming challenges that you can try to solve using Ruby.  Don’t cheat and look at the answers before you try it for yourself!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://onestepback.org/articles/10things/"&gt;10 Things Every Java Programmer Should Know About Ruby&lt;/a&gt;:&lt;/strong&gt; If you’re a Java programmer trying to learn ruby, you should check out Jim Weirich’s 10 things presentation.  He goes over many important differences between Java and Ruby, and this can really help you avoid mistakes that many Java programmers make when transitioning to Ruby.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://humblelittlerubybook.com/"&gt;The Humble Little Ruby Book&lt;/a&gt;:&lt;/strong&gt; Jeremy McAnally’s free introductory online Ruby book.  Covers the basics about a lot of stuff that every Ruby programmer needs to know.  Also available in print.&lt;/p&gt;

&lt;h2&gt;Books&lt;/h2&gt;

&lt;p&gt;The following books are, in my opinion, the best books for aspiring Rubists to check out.  None of them are Rails specific.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.amazon.com/gp/product/1590597664?ie=UTF8&amp;amp;tag=itsaruth-20&amp;amp;link_code=as3&amp;amp;camp=211189&amp;amp;creative=373489&amp;amp;creativeASIN=1590597664"&gt;Beginning Ruby: From Novice to Professional&lt;/a&gt;:&lt;/strong&gt; &lt;a href="http://www.petercooper.co.uk/"&gt;Peter Cooper&lt;/a&gt;’s Beginning Ruby book has received rave reviews, and is specifically targeted at beginning Rubyists.  I’m a bit ashamed that I didn’t pick it up on the first pass at this article.  Sorry Peter!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.amazon.com/gp/product/0974514055?ie=UTF8&amp;amp;tag=itsaruth-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0974514055"&gt;Programming Ruby: The Pragmatic Programmers’ Guide, Second Edition&lt;/a&gt; (aka. The Pickaxe):&lt;/strong&gt;
This book has been the gold standard of Ruby books for years, and is a must have for ruby programmers.  It has both a descriptive section and a reference section.  The 2nd edition is from 2004, however, and does not cover Ruby 1.9 at all.  The 3rd edition is available in &lt;a href="http://www.pragprog.com/titles/ruby3/programming-ruby-1-9"&gt;beta&lt;/a&gt;, and the final version will be available in March.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.amazon.com/gp/product/0672328844?ie=UTF8&amp;amp;tag=itsaruth-20&amp;amp;link_code=as3&amp;amp;camp=211189&amp;amp;creative=373489&amp;amp;creativeASIN=0672328844"&gt;The Ruby Way&lt;/a&gt;:&lt;/strong&gt; Hal Fulton’s book is full of solutions to common problems, plus it really helps you learn many of the Ruby idioms that are used by experienced Ruby programmers.  Ruby 1.8 only.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.amazon.com/gp/product/0596516177?ie=UTF8&amp;amp;tag=itsaruth-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0596516177"&gt;The Ruby Programming Language&lt;/a&gt;:&lt;/strong&gt;
Written by the creator of Ruby himself, this book covers ruby 1.8 and 1.9.  It’s not a tutorial-style book, nor a complete reference, but more of description of the important parts of the Ruby programming language.  It’s a great second book (after the Pickaxe) to help you understand more details about how Ruby works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.manning.com/black2/"&gt;The Well Grounded Rubyist&lt;/a&gt;:&lt;/strong&gt;
David Black’s newest book &lt;em&gt;The Well Grounded Rubyist&lt;/em&gt; is available in pre-release form.  It’s in tutorial form, which is generally good for beginners.  I have not had a chance to read it yet, but his last book was great and I hear this one’s even better.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;  This is a work in progress.  I will be updating it over the next few days as I get feedback and do more research.  I also don’t necessarily want an exhaustive list of every ruby resource.  I’m looking for resources that are helpful for beginners.  Too many choices may be overwhelming.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-02-04:196</id>
    <published>2009-02-04T05:58:00Z</published>
    <updated>2009-02-04T06:17:42Z</updated>
    <category term="block" />
    <category term="parameters" />
    <category term="patch" />
    <category term="sinatra" />
    <link href="http://its.arubything.com/2009/2/4/sinatra-block-parameters" rel="alternate" type="text/html" />
    <title>Sinatra block parameters!</title>
<content type="html">
            &lt;p&gt;The latest master &lt;a href="http://www.sinatrarb.com/"&gt;Sinatra&lt;/a&gt; now supports optional block parameters.  It captures any parameters in the URL and passes them into the block that defines the action:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;get &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;/hello/:name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |n|&lt;tt&gt;
&lt;/tt&gt;  &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Hello &lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;n&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class="r"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;The &lt;code&gt;params&lt;/code&gt; hash is still available as before, so this should not break any existing applications.  It will also work with regular expressions.  Any captures are yielded in order:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;get &lt;span class="rx"&gt;&lt;span class="dl"&gt;%r{&lt;/span&gt;&lt;span class="k"&gt;/hello/([&lt;/span&gt;&lt;span class="ch"&gt;\w&lt;/span&gt;&lt;span class="k"&gt;]+)/([&lt;/span&gt;&lt;span class="ch"&gt;\w&lt;/span&gt;&lt;span class="k"&gt;]+)&lt;/span&gt;&lt;span class="dl"&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |a, b|&lt;tt&gt;
&lt;/tt&gt;  &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Hello, &lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;a&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt; and &lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;b&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class="r"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Big thanks to &lt;a href="http://tomayko.com"&gt;rtomayko&lt;/a&gt; for accepting my patch, and working with me to get this solid enough to put into Sinatra!&lt;/p&gt;

&lt;p&gt;One final note: this feature behaves slightly differently in ruby 1.8 and 1.9.  Ruby 1.9 is much stricter about block arity than 1.8 is.  If the number of captures in the URL and the arity of the block do not match, 1.9 will raise an exception.  1.8 will likely just ignore the extra parameters and throw up a warning.  If you write code that is compatible with 1.9, it will also work with 1.8, as ruby 1.9 is simply more strict.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-01-26:194</id>
    <published>2009-01-26T18:21:00Z</published>
    <updated>2009-01-26T18:36:16Z</updated>
    <category term="Tips" />
    <category term="autospec" />
    <category term="autotest" />
    <category term="rspec" />
    <link href="http://its.arubything.com/2009/1/26/run-specs-with-autotest-er-autospec" rel="alternate" type="text/html" />
    <title>Run specs with autotest, er, autospec!</title>
<content type="html">
            &lt;p&gt;It seems that it’s not well publicized, but if you want to run your RSpec files with &lt;code&gt;autotest&lt;/code&gt; try &lt;code&gt;autospec&lt;/code&gt; instead.  It looks inside the spec/ directory for files named “*_spec.rb” by default for specs.  &lt;code&gt;autospec&lt;/code&gt; is part of the RSpec gem, and basically just sets up the proper environment and then shells out to autotest.&lt;/p&gt;

&lt;p&gt;Also, if your files in lib/ match up with specs in spec/, &lt;code&gt;autospec&lt;/code&gt; will automatically re-run your specs for changed files in lib/ on save.  So if you have lib/foo/bar.rb, &lt;code&gt;autospec&lt;/code&gt; will re-run lib/foo/bar_spec.rb when you save bar.rb.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-01-06:192</id>
    <published>2009-01-06T19:40:00Z</published>
    <updated>2009-01-06T19:53:54Z</updated>
    <category term="codemash" />
    <category term="conference" />
    <link href="http://its.arubything.com/2009/1/6/gearing-up-for-codemash" rel="alternate" type="text/html" />
    <title>Gearing up for CodeMash</title>
<content type="html">
            &lt;p&gt;I’m getting all packed for CodeMash!  Hope too see a lot of you there.  I’m a little bit disappointed by the dearth of Ruby sessions, but there are enough of us that the open sessions will be rockin’.  And the ever popular scotch track always helps.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://codemash.org"&gt;&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-01-04:190</id>
    <published>2009-01-04T06:26:00Z</published>
    <updated>2009-01-23T15:36:22Z</updated>
    <category term="Howto" />
    <category term="cherry-pick" />
    <category term="git" />
    <category term="rebase" />
    <category term="subversion" />
    <link href="http://its.arubything.com/2009/1/4/commit-a-linear-git-history-to-subversion" rel="alternate" type="text/html" />
    <title>Commit a linear git history to subversion</title>
<content type="html">
            &lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; Deskin points out that there is a &lt;a href="http://git.kernel.org/?p=git/git.git;a=commit;h=190f53232d1e92a57843df90e889bcfea39620d3"&gt;commit&lt;/a&gt; to git that fixes this problem by adding a &lt;code&gt;--root&lt;/code&gt; option to &lt;code&gt;rebase --onto&lt;/code&gt;.  So when you get a new build of git, you can probably ignore everything here.&lt;/p&gt;

&lt;p&gt;The other day I needed to commit a plain old (git-svn free) git repository to subversion.  Why, you ask?  I had been working on a small project at work in my own little git repository and needed to get it into our official version control.  I could, of course, just commit the latest version that I had, but that would not record any of my git commit history in subversion.  I needed a better option.  git-svn can save each individual commit of a linear commit history to subversion, so I figured I could just apply git-svn metadata to my repository and go from there.&lt;/p&gt;

&lt;p&gt;This post is a bit long, as I explain the whole process for the solution.  If you’re interested in just the solution, scroll to the bottom.&lt;/p&gt;

&lt;p&gt;I’m going to create a small git repository for illustrative purposes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    $ git init
    Initialized empty Git repository in /Users/bdimchef/testgit/.git/
    $ touch foo
    $ git add foo
    $ git commit -m "added foo"
    Created initial commit 95d1297: added foo
     0 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 foo
    $ touch bar
    $ git add bar
    $ git commit -m "added bar"
    Created commit 202b5c7: added bar
     0 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 bar
    $ touch baz
    $ git add baz
    $ git commit -m "added baz"
    Created commit 58d6331: added baz
     0 files changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 baz
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For this howto, I’m using a blank subversion repository, but you can use this method with any subversion repository that has an empty directory to put your new project.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    $ svnadmin create /Users/bdimchef/testsvn
    $ svn mkdir file:///Users/bdimchef/testsvn/trunk
    $ svn mkdir file:///Users/bdimchef/testsvn/branches
    $ svn mkdir file:///Users/bdimchef/testsvn/tags
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, I added my svn metadata to my git repository and fetched the contents of svn:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    $ git svn init -s file:///Users/bdimchef/testsvn
    $ git svn fetch
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now I was ready to commit:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    $ git svn dcommit
    Can't call method "full_url" on an undefined value at /opt/local/libexec/git-core/git-svn line 425.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What this error means (in our case) is that it can’t figure out where to dcommit.  If we try to rebase, it’s clearer what the problem is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    $ git svn rebase
    Unable to determine upstream SVN information from working tree history
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Our problem is that we have two disjoint histories in our git repository:  The history that we made in git, and the history from our svn repository.  Since they share no common ancestors, git svn can’t figure out where to commit its changes.  Take a look at &lt;code&gt;gitk&lt;/code&gt; to see what’s going on here.&lt;/p&gt;

&lt;p&gt;It turns out that this problem is pretty easy to fix with &lt;code&gt;svn rebase&lt;/code&gt;.  But there is one little trick that will get you if you’re not careful.  And before we get carried away doing too many rebases, lets just make a backup branch of master just in case.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    $ git branch master.bak master
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Lets try one approach.  &lt;code&gt;git rebase --onto A B C&lt;/code&gt; takes the commit range B..C and puts it onto A.  In this case, we want to take all our commits and put them on to trunk:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    $ git rebase --onto trunk master~2 master
    Applying: added bar
    Applying: added baz
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Oops!  That only applied bar and baz.  It forgot about our first commit, foo.  Lets go back one more.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    $ git rebase --onto trunk master~3 master
    fatal: Needed a single revision
    invalid upstream master~3
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Well, that didn’t work either.  The problem is that &lt;code&gt;git rebase --onto&lt;/code&gt; rebases the range beginning &lt;em&gt;after&lt;/em&gt; commit B.  In this case, the commit after foo, which is bar.  And there is no commit before foo, so there’s no way to rebase a range starting with foo included.  The simple solution to this problem is to &lt;code&gt;git cherry-pick&lt;/code&gt; foo first, then do the rebase.  First we’ll create a named branch for our rebase.&lt;/p&gt;

&lt;h2&gt;The Solution&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;    $ git co -b svnrebase trunk                    # create a temporary branch
    $ git cherry-pick master~2                     # cherry pick the first commit
    $ git rebase --onto svnrebase master~2 master  # rebase the 2nd through current commit
    $ git svn dcommit                              # finally commit the results to svn
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And that worked!  So, generally, the secret to joining two separate histories together is to cherry-pick the first commit, and then rebase the rest on top of it.&lt;/p&gt;

&lt;p&gt;Thanks to charon on #git for the cherry-pick idea.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-01-03:189</id>
    <published>2009-01-03T00:56:00Z</published>
    <updated>2009-01-03T06:28:41Z</updated>
    <category term="Tips" />
    <category term="capistrano" />
    <category term="database" />
    <category term="deploy" />
    <category term="password" />
    <link href="http://its.arubything.com/2009/1/3/use-capistrano-to-set-your-production-database-password" rel="alternate" type="text/html" />
    <title>Use Capistrano to set your production database password</title>
<content type="html">
            &lt;p&gt;This is a Capistrano recipe I use to configure my production database.yml.  Before &lt;code&gt;deploy:setup&lt;/code&gt; executes, this recipe will prompt for a database password and save a new database.yml file to the shared directory on your server.  After each deploy, the database.yml file will be symlinked from the shared directory.  You can reconfigure your database by executing &lt;code&gt;cap db:configure&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here are the goods.  I’m pretty sure I got this from &lt;a href="http://archive.jvoorhis.com/articles/2006/07/07/managing-database-yml-with-capistrano"&gt;Jeremy Voorhis&lt;/a&gt; a while back, and modified it to work with Capistrano 2.&lt;/p&gt;


          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2009-01-02:188</id>
    <published>2009-01-02T16:45:00Z</published>
    <updated>2009-01-02T16:47:40Z</updated>
    <category term="admin" />
    <category term="blog" />
    <category term="resolutions" />
    <link href="http://its.arubything.com/2009/1/2/resolutions" rel="alternate" type="text/html" />
    <title>Resolutions</title>
<content type="html">
            &lt;p&gt;I generally think that new year resolutions are silly, but since it’s the new year and I have some goals I’d like to set, I guess I might as well call them resolutions.&lt;/p&gt;

&lt;p&gt;Since goals, like technical requirements, are meaningless if you can’t measure them, I’ll be as specific as possible even if it means that I’ll have to adjust them later.  So here we go:&lt;/p&gt;

&lt;p&gt;Resolved:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I will blog at least once a week. Corollary: I will blog within one day of saying to myself  “Hey, I should blog about this”&lt;/li&gt;
&lt;li&gt;I will contribute a patch to an open source project at least once a month&lt;/li&gt;
&lt;li&gt;I will start a useful open source project by April&lt;/li&gt;
&lt;li&gt;I will write my blog from scratch by July so I can do it just the way I want&lt;/li&gt;
&lt;li&gt;I will do &lt;a href="http://photojojo.com/content/tutorials/project-365-take-a-photo-a-day/"&gt;Project 365&lt;/a&gt;.  Seriously this time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and perhaps most importantly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I will have my next form of gainful employment arranged by January 23rd.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This blog post does not count towards resolution #1, and I’ve got a decent backlog of items to write about, so you’ll be hearing from me soon.&lt;/p&gt;

&lt;p&gt;Happy new year!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2008-08-22:186</id>
    <published>2008-08-22T13:50:00Z</published>
    <updated>2008-08-22T15:43:41Z</updated>
    <category term="hoedown" />
    <category term="ruby" />
    <category term="torrent" />
    <category term="video" />
    <link href="http://its.arubything.com/2008/8/22/ruby-hoedown-videos-online" rel="alternate" type="text/html" />
    <title>Ruby Hoedown Videos Online</title>
<content type="html">
            &lt;p&gt;Ruby Hoedown 2008 &lt;a href="http://rubyhoedown2008.confreaks.com/"&gt;videos&lt;/a&gt; are now live.  There are a lot of good talks.  I highly recommend checking them out.  I’ve set up a &lt;a href="http://thepiratebay.org/torrent/4355220"&gt;torrent&lt;/a&gt; of all the videos for your convenience.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2008-06-18:175</id>
    <published>2008-06-18T15:53:00Z</published>
    <updated>2008-06-18T17:54:26Z</updated>
    <category term="Microblog" />
    <category term="Tips" />
    <category term="macports" />
    <category term="rubymate" />
    <category term="textmate" />
    <category term="tip" />
    <link href="http://its.arubything.com/2008/6/18/make-rubymate-work-with-macports-ruby" rel="alternate" type="text/html" />
    <title>Make RubyMate work with MacPorts Ruby</title>
<content type="html">
            &lt;p&gt;To make TextMate’s RubyMate work with MacPorts (or any other non-default ruby install) just set TM_RUBY environment variable to be the path to your custom ruby interpreter in TextMate’s preferences.  Custom environmental variable settings are located under ‘Advanced’-&gt;’Shell Variables’ in TM’s prefs.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2007-09-21:8</id>
    <published>2007-09-21T03:00:00Z</published>
    <updated>2007-11-05T23:07:48Z</updated>
    <category term="Tips" />
    <category term="acts_as_solr" />
    <category term="associations" />
    <category term="bdd" />
    <category term="has_many" />
    <category term="rails" />
    <category term="testing" />
    <category term="tip" />
    <link href="http://its.arubything.com/2007/9/21/test-behavior-not-implementation" rel="alternate" type="text/html" />
    <title>Test behavior, not implementation!</title>
<content type="html">
            &lt;p&gt;This is a based on a comment I made on a &lt;a href="http://giantrobots.thoughtbot.com/2007/9/19/ruby-on-rails-testing-strategies"&gt;post&lt;/a&gt; on thoughtbot’s blog.  I suggest you read the original post for some background on what I’m talking about.&lt;/p&gt;

&lt;p&gt;If you’re too lazy to read, here’s the basic gist:  You could potentially test your associations or plugins (such as acts_as_solr) by simply checking whether or not your object responds to the messages that the plugins generate when their class methods are called.&lt;/p&gt;

&lt;p&gt;The problem is, we should be trying to test &lt;em&gt;behavior&lt;/em&gt;, not simply whether or not acts_as_solr or has_many are being called.  The problem with the approach described above is that it assumes too much about implementation details and doesn’t actually make sure your app is doing what it’s supposed to be doing.  &lt;/p&gt;

&lt;p&gt;In this case, I think that find_by_solr should be called find_by_content or something, since it doesn’t really matter that it’s solr that’s doing the lookup.  All the developer using the API cares about is that when they pass a particular query into the method, the proper results are returned.  And that is what our tests should test.&lt;/p&gt;

&lt;p&gt;I am not convinced that (as many test/rspec examples show) simply checking for association methods (has_many, belongs_to, etc.) or plugin methods (acts_as_solr) are sufficient, or even a good idea at all.  Nor do I think that those sorts of tests qualify as BDD.  For instance, I have something resembling the following in an application per someone’s suggestion:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span class="co"&gt;Person&lt;/span&gt;.reflect_on_association(&lt;span class="sy"&gt;:addresses&lt;/span&gt;).should_not be_nil&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;I really don’t like this, though.  I don’t care one bit that there’s an association called “addresses” on my Person object.  What I care about is that Person responds to addresses and that addresses returns an array of the &lt;em&gt;proper&lt;/em&gt; addresses.  This is the whole point in BDD.  Care about the behavior of your objects, not their implementation.&lt;/p&gt;

&lt;p&gt;To explore this further, I’ll use a slightly more complicated example.  Say I have the following in my Person class:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span class="c"&gt;# app/models/person.rb&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;has_many &lt;span class="sy"&gt;:addresses&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;has_many &lt;span class="sy"&gt;:cool_addresses&lt;/span&gt;, &lt;span class="sy"&gt;:foreign_key&lt;/span&gt; =&amp;gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;address_id&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="sy"&gt;:conditions&lt;/span&gt; =&amp;gt; [&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;foo = ? AND bar = ?&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, foo, bar], &lt;span class="sy"&gt;:order&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:zipcode&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;And the corresponding test case:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span class="co"&gt;Person&lt;/span&gt;.reflect_on_association(&lt;span class="sy"&gt;:cool_addresses&lt;/span&gt;).should_not be_nil&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Well, guess what.  This association exists.  Our tests pass.  But it’s &lt;em&gt;wrong&lt;/em&gt;.  That :foreign_key is supposed to be person_id and not address_id.  Well, we can solve that!  Just test to make sure the has_many receives the appropriate parameters.  Something like this (made up) helper would work:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span class="co"&gt;Person&lt;/span&gt;.reflect_on_association(&lt;span class="sy"&gt;:cool_addresses&lt;/span&gt;).should have_foreign_key(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;person_id&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;))&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;And we could go about our business, basically duplicating all the parameters supplied to has_many in our tests.  But in the end, this is just going to make our tests horribly brittle and is not actually testing anything useful.  It’s not testing behavior &lt;em&gt;at all&lt;/em&gt;.  &lt;/p&gt;

&lt;p&gt;The whole point in BDD is to make our tests poke and prod our application in a certain way and have them spit back the correct output.  Yes, the plugins/associations are well tested and I shouldn’t be testing them again.  I know that if my has_many call supplies the correct parameters, I will get the objects I expect to get.  But I still need to make sure that I’m calling has_many properly.  It’s simply not sufficient for me to know &lt;em&gt;that&lt;/em&gt; has_many is called, I need to know that &lt;em&gt;when&lt;/em&gt; it’s called, the proper “stuff” happens.  I need to make sure the association does what I expect it to do.  Here’s what I think my tests should do to ensure cool_addresses is working properly (no real code this time):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a few objects to the Addresses table, either using fixtures or in some kind of before callback.  (Yes, fixtures suck, etc.)&lt;/li&gt;
&lt;li&gt;Make sure cool_addresses returns addresses that correspond to the ‘foo’ and ‘bar’ conditions above.&lt;/li&gt;
&lt;li&gt;Make sure that cool_addresses returns the addresses ordered by zipcode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that’s it.  Yes, it will take the tests slightly longer to run, since they’re using the database (and maybe fixtures).  Yes, I’m partially testing ActiveRecord.  But I’m testing that my object &lt;em&gt;behaves&lt;/em&gt; like I want it to.  That’s the point in BDD, right?&lt;/p&gt;

&lt;p&gt;As an added benefit, the tests are &lt;em&gt;much&lt;/em&gt; more flexible now.  Check it out:&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span class="c"&gt;# app/models/person.rb&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;has_many &lt;span class="sy"&gt;:addresses&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class="c"&gt;#has_many :cool_addresses, :foreign_key =&amp;gt; &amp;quot;address_id&amp;quot;, :conditions =&amp;gt; [&amp;quot;foo = ? AND bar = ?&amp;quot;, foo, bar], :order =&amp;gt; :zipcode&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;cool_addresses&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  addresses.find_all &lt;span class="r"&gt;do&lt;/span&gt; {|a| a.foo == foo &amp;amp;&amp;amp; a.bar == bar}.sort(&amp;amp;&lt;span class="sy"&gt;:zipcode&lt;/span&gt;)&lt;tt&gt;
&lt;/tt&gt;&lt;span class="r"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;That passes my tests, too.  And it should.  But my previous example where I used association introspection would fail miserably.&lt;/p&gt;

&lt;p&gt;This is a major complaint about a lot of the test code examples I see floating around.  Everybody seems to be mocking and stubbing and introspecting to their heart’s content, but all they seem to be doing in the end is writing the same code twice: once in their implementation, and once in their tests. And so when they change the way their application is implemented (NOTE: I did &lt;em&gt;not&lt;/em&gt; say their application’s behavior) their tests break.  &lt;/p&gt;

&lt;p&gt;There are two sides to this BDD thing.  First, your tests need to ensure that if you change the behavior of your code, they will fail.  Second, your tests need to still work when your application still behaves the right way, even if you change every single line of code in your application.  Of course, this is nearly impossible to achieve, but at least we can try.&lt;/p&gt;

&lt;p&gt;Thoughts?&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2007-08-31:3</id>
    <published>2007-08-31T01:50:00Z</published>
    <updated>2007-09-10T22:09:32Z</updated>
    <category term="Tips" />
    <category term="controller" />
    <category term="has_many :through" />
    <category term="model" />
    <category term="rails" />
    <category term="rest" />
    <category term="tip" />
    <link href="http://its.arubything.com/2007/8/31/predefined-models-no-different-than-any-others" rel="alternate" type="text/html" />
    <title>Predefined models?  No different than any others.</title>
<content type="html">
            &lt;p&gt;Earlier today I was trying to figure out how to make a list of potential models to be created.  For example:  I have some &lt;code&gt;User&lt;/code&gt;s and some &lt;code&gt;Group&lt;/code&gt;s.  I have a many-to-many &lt;code&gt;Membership&lt;/code&gt; join model with some &lt;code&gt;has_many :through&lt;/code&gt;s.  The fundamental goal is to provide the user an interface to add Membership models that link a &lt;code&gt;User&lt;/code&gt; to a &lt;code&gt;Group&lt;/code&gt;.  I &lt;em&gt;could&lt;/em&gt; use a bunch of checkboxes, but I’d rather pull up a list of possible &lt;code&gt;Group&lt;/code&gt;s with little “add” links next to them.&lt;/p&gt;

&lt;p&gt;Since I’m using RESTful controllers, we have a &lt;code&gt;MembershipsController&lt;/code&gt; that implements all the standard methods.  We’re also nesting our routes such that &lt;code&gt;MembershipsController&lt;/code&gt; nested beneath the &lt;code&gt;UsersController&lt;/code&gt;.  In order to create the Membership, we need to POST to /users/1/memberships.  But how do we get a list of &lt;code&gt;Membership&lt;/code&gt;s that we can quickly add?&lt;/p&gt;

&lt;p&gt;How about modifying &lt;code&gt;MembershipsController.new&lt;/code&gt;?  We don’t need the normal definition of &lt;code&gt;new&lt;/code&gt;, since we’re never going to be manually creating a new Membership.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;&lt;span class="c"&gt;# controllers/memberships_controller.rb&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;new&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span class="iv"&gt;@memberships&lt;/span&gt; = &lt;span class="co"&gt;Groups&lt;/span&gt;.find(&lt;span class="sy"&gt;:all&lt;/span&gt;).collect &lt;span class="r"&gt;do&lt;/span&gt; |group|&lt;tt&gt;
&lt;/tt&gt;    &lt;span class="co"&gt;Membership&lt;/span&gt;.new(&lt;span class="sy"&gt;:user_id&lt;/span&gt; =&amp;gt; params[&lt;span class="sy"&gt;:user_id&lt;/span&gt;], &lt;span class="sy"&gt;:group_id&lt;/span&gt; =&amp;gt; group.id)&lt;tt&gt;
&lt;/tt&gt;  &lt;span class="r"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class="r"&gt;end&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Now we have a list of potential &lt;code&gt;Membership&lt;/code&gt; objects available to our view.  Remember, the &lt;code&gt;Membership&lt;/code&gt;s haven’t been saved yet.  They’re just there for convenience for holding attributes.  We are doing object-oriented programming after all.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td title="click to toggle" class="line_numbers"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre&gt;# views/memberships/new.html.erb&lt;tt&gt;
&lt;/tt&gt;&lt;span class="il"&gt;&lt;span class="dl"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="iv"&gt;@memberships&lt;/span&gt;.each &lt;span class="r"&gt;do&lt;/span&gt; |membership| &lt;span class="dl"&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span class="il"&gt;&lt;span class="dl"&gt;&amp;lt;%&lt;/span&gt; form_for &lt;span class="sy"&gt;:membership&lt;/span&gt;, membership, &lt;span class="sy"&gt;:url&lt;/span&gt; =&amp;gt; memberships_path &lt;span class="r"&gt;do&lt;/span&gt; |f| &lt;span class="dl"&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span class="il"&gt;&lt;span class="dl"&gt;&amp;lt;%=&lt;/span&gt; f.hidden_field &lt;span class="sy"&gt;:user_id&lt;/span&gt; &lt;span class="dl"&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span class="il"&gt;&lt;span class="dl"&gt;&amp;lt;%=&lt;/span&gt; f.hidden_field &lt;span class="sy"&gt;:group_id&lt;/span&gt; &lt;span class="dl"&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;    &lt;span class="il"&gt;&lt;span class="dl"&gt;&amp;lt;%=&lt;/span&gt; f.submit membership.group.name &lt;span class="dl"&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;  &lt;span class="il"&gt;&lt;span class="dl"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="r"&gt;end&lt;/span&gt; &lt;span class="dl"&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;span class="il"&gt;&lt;span class="dl"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="r"&gt;end&lt;/span&gt; &lt;span class="dl"&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;Basically what we have here is a big list of predefined join models wrapped up in form tags.  When you click on one of them, you’ll end up submitting the form that actually creates the model.  Eventually, we could make this into an AJAX widget that uses &lt;code&gt;form_remote_for&lt;/code&gt; with very little effort.&lt;/p&gt;

&lt;p&gt;Nothing I have described here is particularly revolutionary.  Rather than just returning a single new &lt;code&gt;Membership&lt;/code&gt; in the &lt;code&gt;new&lt;/code&gt; method, we return a list of objects.  Rather than rendering one form with editable fields, we render multiple forms with predefined, hidden fields.  Whichever you submit creates the corresponding object.  These two very simple changes to the standard REST actions allow us to easily and elegantly create our join models.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://its.arubything.com/">
    <author>
      <name>brandon.dimcheff</name>
    </author>
    <id>tag:its.arubything.com,2007-08-28:2</id>
    <published>2007-08-28T03:52:00Z</published>
    <updated>2007-08-30T04:27:02Z</updated>
    <category term="Howto" />
    <category term="gem" />
    <category term="install" />
    <category term="macports" />
    <category term="mysql" />
    <category term="rails" />
    <link href="http://its.arubything.com/2007/8/28/installing-a-rails-stack-on-mac-os-x-with-macports" rel="alternate" type="text/html" />
    <title>Installing a Rails stack on Mac OS X with MacPorts</title>
<content type="html">
            &lt;p&gt;I have a clean install of OS X on which I need to install Rails.  I’ve done this maybe 3 or 4 times in the past year with minor variations each time which inevitably come back to bite me later.  This post is an effort to document and standardize all the details to get a Rails stack going with MacPorts.&lt;/p&gt;

&lt;h2&gt;Installing Xcode Tools&lt;/h2&gt;

&lt;p&gt;You need the latest version of &lt;a href="http://developer.apple.com/tools/xcode/"&gt;Xcode tools&lt;/a&gt; to be able to compile all the packages necessary for Ruby and Rails.  They’re a free download from Apple.&lt;/p&gt;

&lt;h2&gt;Installing MacPorts&lt;/h2&gt;

&lt;p&gt;First thing’s first: Install &lt;a href="http://trac.macosforge.org/projects/macports/wiki/InstallingMacPorts"&gt;MacPorts&lt;/a&gt;! Be sure to follow the directions closely.  Especially the bit about setting your &lt;code&gt;PATH&lt;/code&gt; appropriately.  The /opt paths must be before your standard path so when you type &lt;code&gt;ruby&lt;/code&gt;, you use the ruby interpreter from MacPorts rather than the old one that came with OS X.&lt;/p&gt;

&lt;h2&gt;Installing Rails Prerequisites with MacPorts&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;$ sudo port install mysql5 +server
$ sudo port install ruby
$ sudo port install rb-rubygems
$ sudo port install rb-mysql
$ sudo port install rb-termios
$ sudo port install subversion +tools
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Configure MySQL&lt;/h2&gt;

&lt;p&gt;First, add &lt;code&gt;/opt/local/lib/mysql5/bin/&lt;/code&gt; to your &lt;code&gt;PATH&lt;/code&gt; as you did in the instructions to install MacPorts.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ sudo mysql_install_db5 --user=mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Install Gems&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;$ sudo gem install -y rake
$ sudo gem install -y rails
$ sudo gem install -y capistrano
$ sudo gem install -y mongrel
$ sudo gem install -y mongrel_cluster
$ sudo gem install -y piston
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Install Optional Packages&lt;/h2&gt;

&lt;p&gt;The above ports and gems are the minimum you’ll need to get rails running successfully.  The ones below, while not required, are often useful when developing rails apps.&lt;/p&gt;

&lt;h3&gt;Other Databases&lt;/h3&gt;

&lt;p&gt;Try postgres or sqlite3 for some database variety (&lt;code&gt;port install postgresql82-server &amp;amp;&amp;amp; port install rb-postgres&lt;/code&gt; or &lt;code&gt;port install rb-sqlite3&lt;/code&gt;, respectively).&lt;/p&gt;

&lt;h3&gt;Testing&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://rspec.rubyforge.org/"&gt;RSpec&lt;/a&gt; - a powerful &lt;a href="http://en.wikipedia.org/wiki/Behavior_driven_development"&gt;BDD&lt;/a&gt; framework&lt;/li&gt;
&lt;li&gt;&lt;a href="http://flexmock.rubyforge.org/"&gt;Flexmock&lt;/a&gt; and/or &lt;a href="http://mocha.rubyforge.org/"&gt;Mocha&lt;/a&gt; - for mocking/stubbing&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Misc&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://tzinfo.rubyforge.org/"&gt;tzinfo&lt;/a&gt; - Time Zone handling&lt;/li&gt;
&lt;/ul&gt;
          </content>  </entry>
</feed>
