<?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 version="2.0"><channel><description>Hi, I’m Jeremy McAnally.  I work at Intridea and tweet at @jm and get my open source on at http://github.com/jm and write run-on sentences.</description><title>omgbloglol</title><generator>Tumblr (3.0; @omgbloglol)</generator><link>http://omgbloglol.com/</link><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/omgbloglol" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="omgbloglol" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://tumblr.superfeedr.com/" /><item><title>Ruby Hoedown MMX is open for business (and talk submission!)</title><description>&lt;p&gt;Maybe one of these I’ll actually, seriously regularly blog.  I had a few entries almost finished for Rails 3, but they now require some revision, so I’ll try to polish those up soon.  I also have a few in the hopper related to tech writing, which should prove fun and interesting for all parties involved (OK, they’ll at least be informative!).&lt;/p&gt;

&lt;p&gt;But my lack of blogging isn’t the reason that I am, in fact, blogging today.  No, my friends, today I am announcing that &lt;a href="http://rubyhoedown.com" target="_blank"&gt;Ruby Hoedown MMX&lt;/a&gt; (that’s 2010 for those of you who don’t follow Roman Numerals™ very closely or may still be running on a Pentium II) is open for &lt;a href="http://rubyhoedown.eventbrite.com" target="_blank"&gt;registration&lt;/a&gt; and &lt;a href="http://bit.ly/9jn1ec" target="_blank"&gt;talk submission&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://img.skitch.com/20100628-b49ppagjscn1x229ebcy4i6wtg.jpg" alt="Ruby Hoedown 2010"/&gt;&lt;/p&gt;

&lt;p&gt;We’re holding the conference in Nashville at the &lt;a href="http://www1.hilton.com/en_US/hi/hotel/BNANSHF-Hilton-Nashville-Downtown-Tennessee/index.do" target="_blank"&gt;Downtown Hilton&lt;/a&gt; on September 3-4, 2010, and the price will be a wallet-shredding, budget-busting, GDP-increasing, third-world-country-enriching &lt;strong&gt;$0&lt;/strong&gt; (that’s right, &lt;strong&gt;FREE&lt;/strong&gt;).  We had so much fun last year going free, we decided to go ahead and do it again this year.  But we’re not just throwing old hat around this year; you’ll have to keep your eyes and ears tuned for some new things we’re adding and some old things that are coming back!&lt;/p&gt;

&lt;p&gt;So why not mosy on over to the &lt;a href="http://rubyhoedown.com" target="_blank"&gt;conference website&lt;/a&gt; and &lt;a href="http://rubyhoedown.eventbrite.com" target="_blank"&gt;register up&lt;/a&gt; or &lt;a href="http://bit.ly/9jn1ec" target="_blank"&gt;submit a talk&lt;/a&gt;?  &lt;strong&gt;The CFP closes on July 2, 2010, and talk selections will (probably) be made that weekend!&lt;/strong&gt;  And if you register now (and get one of the next few slots), you’ll be able to have your say about the talks that we select (more on that soon).&lt;/p&gt;</description><link>http://omgbloglol.com/post/745751259</link><guid>http://omgbloglol.com/post/745751259</guid><pubDate>Mon, 28 Jun 2010 10:18:10 -0400</pubDate></item><item><title>The Rails Upgrade Handbook is now available</title><description>&lt;p&gt;The eBook I previously mentioned is now available!  &lt;strong&gt;It’s only $12 at &lt;a href="http://railsupgradehandbook.com" target="_blank"&gt;&lt;a href="http://railsupgradehandbook.com" target="_blank"&gt;http://railsupgradehandbook.com&lt;/a&gt;&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside you’ll find…&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Almost 120 pages of upgrade information&lt;/li&gt;
&lt;li&gt;A step-by-step guide to upgrading your app to Rails 3&lt;/li&gt;
&lt;li&gt;High-level discussion of what’s new in Rails 3&lt;/li&gt;
&lt;li&gt;Practical tips on using Rails 3’s new features to improve your code&lt;/li&gt;
&lt;li&gt;Real case studies of upgrading apps and plugins&lt;/li&gt;
&lt;li&gt;Detailed checklists for upgrading&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="http://railsupgradehandbook.com/" target="_blank"&gt;&lt;img src="http://img.skitch.com/20100222-b9gh1r5171ejh2mqscuyt88n7k.jpg" alt="The website"/&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://omgbloglol.com/post/404633761</link><guid>http://omgbloglol.com/post/404633761</guid><pubDate>Mon, 22 Feb 2010 04:28:31 -0500</pubDate></item><item><title>Content from my Rails 3 presentation today, and a note about RailsConf 2010</title><description>&lt;p&gt;Some people asked for the links and slides to be placed on my blog from my presentation upgrading to Rails 3, so here they are.&lt;/p&gt;

&lt;h2&gt;&lt;a href="http://jeremymcanally.com/rails3_why_and_how.pdf" target="_blank"&gt;Download the slides&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://jeremymcanally.com/rails3_why_and_how.pdf" target="_blank"&gt;&lt;img src="http://media.tumblr.com/tumblr_ky1wxr07Fz1qz4tvk.png" alt="Rails 3: The Why and How"/&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Links&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Rails 3 Release Notes and Guides: &lt;a href="http://guides.rails.info/" target="_blank"&gt;&lt;a href="http://guides.rails.info/" target="_blank"&gt;http://guides.rails.info/&lt;/a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;EdgeRails.info &lt;a href="http://edgerails.info/" target="_blank"&gt;&lt;a href="http://edgerails.info/" target="_blank"&gt;http://edgerails.info/&lt;/a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;List of links from railslove.com: &lt;a href="http://is.gd/8CZN2" target="_blank"&gt;&lt;a href="http://is.gd/8CZN2" target="_blank"&gt;http://is.gd/8CZN2&lt;/a&gt;&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;List of links from mediumexposure.com: &lt;a href="http://is.gd/8CZWn" target="_blank"&gt;&lt;a href="http://is.gd/8CZWn" target="_blank"&gt;http://is.gd/8CZWn&lt;/a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Yehudaʼs blog: &lt;a href="http://yehudakatz.com" target="_blank"&gt;&lt;a href="http://yehudakatz.com" target="_blank"&gt;http://yehudakatz.com&lt;/a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Mike Lindsaarʼs blog: &lt;a href="http://lindsaar.net" target="_blank"&gt;&lt;a href="http://lindsaar.net" target="_blank"&gt;http://lindsaar.net&lt;/a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;This blog (redundant, I know): &lt;a href="http://omgbloglol.com" target="_blank"&gt;&lt;a href="http://omgbloglol.com" target="_blank"&gt;http://omgbloglol.com&lt;/a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Rails Upgrade Handbook: &lt;a href="http://railsupgradehandbook.com" target="_blank"&gt;&lt;a href="http://railsupgradehandbook.com" target="_blank"&gt;http://railsupgradehandbook.com&lt;/a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;They’re also posting the slides and eventually audio over on the &lt;a href="http://railsconf.com/olc" target="_blank"&gt;conference website&lt;/a&gt;; if you missed it, they’ve also posted some information on &lt;a href="http://railsconf.org" target="_blank"&gt;RailsConf 2010&lt;/a&gt;, including &lt;a href="http://en.oreilly.com/rails2010/public/schedule/detail/13631" target="_blank"&gt;a little something about the tutorial I’ll be giving&lt;/a&gt; on advanced Rails 3 features!&lt;/p&gt;</description><link>http://omgbloglol.com/post/396980681</link><guid>http://omgbloglol.com/post/396980681</guid><pubDate>Thu, 18 Feb 2010 14:09:18 -0500</pubDate></item><item><title>Coming very soon: The Rails Upgrade Handbook</title><description>&lt;p&gt;I’ve been enjoying you guys’ feedback on my Rails 3 posts.  I’ve gotten feedback both good and bad, and it’s been really helpful in the big project I’ve been working on, which I’m really glad to finally be able to talk about it a little bit.  Most of the content I’ve posted about Rails 3 has been excerpted from my new eBook: &lt;a href="http://railsupgradehandbook.com" target="_blank"&gt;The Rails 3 Upgrade Handbook&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_ky0xr8yGbA1qz4tvk.png" alt="The cover"/&gt;&lt;/p&gt;

&lt;p&gt;It’s a hair over 100 pages of information on upgrading and improving your Rails 2.x applications with Rails 3.  It covers how to upgrade, what new features can improve your existing code, a few case studies of upgrades, and extremely detailed checklists for upgrading and fixing your code.&lt;/p&gt;

&lt;p&gt;The release of this project is imminent, so if you’re interested head over to &lt;a href="http://railsupgradehandbook.com" target="_blank"&gt;&lt;a href="http://railsupgradehandbook.com" target="_blank"&gt;http://railsupgradehandbook.com&lt;/a&gt;&lt;/a&gt; and sign up to be notified of when it’s released.  It will be priced at $12 and kept updated as things shift and change on the path to Rails 3 Final.&lt;/p&gt;

&lt;p&gt;Next week’s posts will be more consistent.  I plan on discussing upgrading to Rails 3, one of my favorite new features in Rails 3, and my toolchain for writing eBooks (it’ll make hackers happy I think).&lt;/p&gt;</description><link>http://omgbloglol.com/post/396159385</link><guid>http://omgbloglol.com/post/396159385</guid><pubDate>Thu, 18 Feb 2010 01:29:24 -0500</pubDate></item><item><title>Improved validations in Rails 3</title><description>&lt;p&gt;Quite sorry about not getting another post up sooner; I’ve been very busy lately with a few different things (some of which shall be revealed very soon!).  Today I want to talk about validations.  Validations received quite a bit of love in Rails 3, but if you don’t go looking for the new shiny features, you won’t find them.  The old API is still around, so nothing will break if you try them, but there is also a variation on that theme:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;validates :login, :presence =&gt; true, :length =&gt; {:minimum =&gt; 4},
          :uniqueness =&gt; true, :format =&gt; { :with =&gt; /[A-Za-z0-9]+/ }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This new form is excellent since you can compress what would have previously been 4 lines of code into 1, making it dead simple to see all the validations related to a single attribute all in one place.  The valid keys/value types for this form are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;:presence =&gt; true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:uniqueness =&gt; true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:numericality =&gt; true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:length =&gt; { :minimum =&gt; 0, maximum =&gt; 2000 }&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:format =&gt; { :with =&gt; /.*/ }&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:inclusion =&gt; { :in =&gt; [1,2,3] }&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:exclusion =&gt; { :in =&gt; [1,2,3] }&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:acceptance =&gt; true&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:confirmation =&gt; true&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As I mentioned previously, you can still use the old API, but it makes sense to switch to this form since, when scanning your code, you’re rarely looking for what sort of validation it is rather than the attribute that’s being validated.&lt;/p&gt;

&lt;p&gt;Another great new validation feature is the ability to have a custom validation class.  It’s fairly common for Rails developers to develop their own validation methods that look something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def validates_has_proper_category
  validates_each :category_id do |record, attr, value|
    unless record.user.category_ids.include?(value)
      record.errors.add attr, 'has bad category.'
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These methods are really useful, especially if you use this validation in a lot of different classes, but they often add a bit of ugly code.  Fortunately, in Rails a lot of that nastiness can go away.  Those old methods should still work, but you could make them look like this instead:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class ProperCategoryValidator &lt; ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless record.user.category_ids.include?(value)
      record.errors.add attribute, 'has bad category.'
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Basically, create a class that inherits from &lt;code&gt;ActiveModel::EachValidator&lt;/code&gt; and implements a &lt;code&gt;validate_each&lt;/code&gt; method; inheriting from this class will make it available to all Active Record classes.  Not only is the code a bit cleaner, since you can spread it out a bit more if it’s hairy without polluting the model class, it also makes these validations easily testable without much hassle, and you can also integrate them into the short form validations like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;validate :category_id, :proper_category =&gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that the key name is taken from the class name (i.e., &lt;code&gt;ProperCategoryValidator&lt;/code&gt; becomes &lt;code&gt;:proper_category&lt;/code&gt;).  A similar new feature is the ability to have validator classes that bundle validations into a single object.  If you have a lot of classes that need some very complex validation logic, you can create a class like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class ReallyComplexValidator &lt; ActiveModel::Validator
  def validate(record)
    record.errors[:base] &lt;&lt; "This check failed!" unless thing(record)
    record.errors[:base] &lt;&lt; "This failed!" unless other(record)
    record.errors[:base] &lt;&lt; "FAIL!" unless fail(record)
  end

private
  def thing(record)
    # Complex validation here...
  end

  def other(record)
    # Complex validation here...
  end

  def fail(record)
    # Complex validation here...
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The API is basically to inherit from &lt;code&gt;ActiveModel::Validator&lt;/code&gt; and implement a &lt;code&gt;validate&lt;/code&gt; method that takes a record as its only argument.  Then in your model classes, use it like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class NewsPost &lt; ActiveRecord::Base
  validates_with ReallyComplexValidator
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This pattern is nice for wrapping up a lot of unruly validation code, but a more interesting variation on it will be building class factories (o noez not a factory!) based on parameters that build these validation classes (i.e., a class that generates validation classes).  You can find a little more information these and other Active Model validation features in its API documentation at &lt;a href="http://api.rails.info/classes/ActiveModel/Validator.html" target="_blank"&gt;http://api.rails.info/classes/ActiveModel/Validator.html&lt;/a&gt; .&lt;/p&gt;</description><link>http://omgbloglol.com/post/392895742</link><guid>http://omgbloglol.com/post/392895742</guid><pubDate>Tue, 16 Feb 2010 10:44:42 -0500</pubDate><category>rails3</category><category>validations</category><category>ruby</category><category>rails</category></item><item><title>The Path to Rails 3: Greenfielding new apps with the Rails 3 beta</title><description>&lt;p&gt;Upgrading applications is good sport and all, but everyone knows that greenfielding is where the real fun is.  At least, I love greenfielding stuff  a lot more than dealing with old ghetto cruft that has 1,900 test failures (and 300 errors), 20,000 line controllers, and code that I’m pretty sure is actually a demon-brand of PHP.&lt;/p&gt;

&lt;p&gt;Building a totally new app in Rails 3 is relatively simple (especially if you’ve done it in previous Rails versions), but there a few changes that can trip you up.  In the interest of not missing a step someone may need, this post is a simple walkthrough of building a new app with Rails 3.  I would have simply posted about the &lt;a href="http://guides.rails.info/getting_started.html" target="_blank"&gt;Rails 3 version of the Getting Started guide&lt;/a&gt;, but it’s actually a bit out of date now.  I’ve committed each step in its own commit on Github so you can step through it (the repository is here: &lt;a href="http://github.com/jm/rails3_blog" target="_blank"&gt;&lt;a href="http://github.com/jm/rails3_blog" target="_blank"&gt;http://github.com/jm/rails3_blog&lt;/a&gt;&lt;/a&gt;)&lt;/p&gt;

&lt;h2&gt;An aside: Installing the Rails 3 beta&lt;/h2&gt;

&lt;p&gt;Installing the Rails 3 beta can be sort of tricky since there are dependencies, it’s a prerelease gem, and &lt;a href="http://twitter.com/bitsweat/status/8662035599" target="_blank"&gt;RubyGems basically poops the bed when those two scenarios collide&lt;/a&gt;.  Hopefully that’ll be fixed soon, but the mean time, install Rails’ dependencies like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install rails3b
gem install arel --pre

# or if that gives you hassle...

gem install i18n tzinfo builder memcache-client rack \
            rack-test rack-mount erubis mail text-format thor bundler
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once all those lovely gems are installed (add &lt;code&gt;--no-ri&lt;/code&gt; and &lt;code&gt;--no-rdoc&lt;/code&gt; if you want to skip those/speed up your install), then install the prerelease version of Rails:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install rails --pre
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you’re ready to roll on with the Rails beta!&lt;/p&gt;

&lt;h2&gt;Using the new generator&lt;/h2&gt;

&lt;p&gt;The application generator is basically the same with two key differences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The parameter that was formerly the app name is now the app path.  You can still give it a “name,” and it will create the folder like normal.  But you can also give it a full path (e.g., &lt;code&gt;~code/my_application&lt;/code&gt; rather than just &lt;code&gt;my_application&lt;/code&gt;) and it will create the application there.&lt;/li&gt;
&lt;li&gt;All parameters for the generator must go after the app path. So, previously one could do &lt;code&gt;rails -d mysql test_app&lt;/code&gt;, but now that has to be &lt;code&gt;rails test_app -d mysql&lt;/code&gt;.  This change is largely due to the major refactoring of the Rails generators, so even though it’s somewhat of a temporary annoyance, it’s definitely worth it for the flexibility and power that the new generators bring (more on that soon).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, let’s generate a blog application (really original, I know, right?):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails rails3_blog -d mysql
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you get an error like “no value provided for required arguments ‘app_path’”, then you’ve gotten your parameters out of order.  If you’d like to use another database driver, you can provide &lt;code&gt;postgresql&lt;/code&gt; or &lt;code&gt;sqlite&lt;/code&gt; (or nothing, since &lt;code&gt;sqlite&lt;/code&gt; is the default).  You’ll see a lot of text scroll by, and now we have a nice, fresh Rails 3 application to play with [&lt;a href="http://github.com/jm/rails3_blog/commit/4b6b763ac9378c6cde95b0815d2a4c2619a0e403" target="_blank"&gt;4b6b763ac9378c6cde95b0815d2a4c2619a0e403&lt;/a&gt;].&lt;/p&gt;

&lt;p&gt;Let’s crank up the server (note that it’s different now!)…&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails server
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Rails went the “Merb way” and has consolidated its many &lt;code&gt;script/*&lt;/code&gt; commands into the &lt;code&gt;rails&lt;/code&gt; binscript.  So things like &lt;code&gt;generate&lt;/code&gt;, &lt;code&gt;server&lt;/code&gt;, &lt;code&gt;plugin&lt;/code&gt;, etc. are now &lt;code&gt;rails generate&lt;/code&gt; and so on.  Once the server’s booted, navigate over to &lt;code&gt;http://localhost:3000&lt;/code&gt; and you should see a familiar friend:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_kxctzqe4Su1qz4tvk.jpg" alt="You're on board!"/&gt;&lt;/p&gt;

&lt;p&gt;Click on “About your application’s environment” to see more information about the app you’ve generated.&lt;/p&gt;

&lt;h2&gt;Configuring an app&lt;/h2&gt;

&lt;p&gt;Now comes the task of configuration.  Again, not a whole ton of changes from previous versions, but navigating them can trip up the novice and journey(wo)man alike.  First, setup all your database settings in &lt;code&gt;database.yml&lt;/code&gt;; it’s just like previous versions of Rails, so no surprises there (and plenty of information abounds if you’re new to it).&lt;/p&gt;

&lt;p&gt;Next, pop open &lt;code&gt;config/application.rb&lt;/code&gt;.  This is where much of the configuration information that once lived in &lt;code&gt;config/environment.rb&lt;/code&gt; now lives.  The portion you probably want to pay attention to most when making a new application is the block that defines your options for ORM, template engine, etc.  Here’s the default:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;config.generators do |g|
  g.orm             :active_record
  g.template_engine :erb
  g.test_framework  :test_unit, :fixture =&gt; true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I’m going to stick with the defaults, but you could substitute in something like &lt;code&gt;:datamapper&lt;/code&gt; or &lt;code&gt;:sequel&lt;/code&gt; for &lt;code&gt;:active_record&lt;/code&gt;, &lt;code&gt;:haml&lt;/code&gt; for &lt;code&gt;:erb&lt;/code&gt;, or &lt;code&gt;:rspec&lt;/code&gt; for &lt;code&gt;:test_unit&lt;/code&gt; (once they get it working with Rails 3).  Doing so will set the generators for models, views, etc. to use your tool of choice (remember that whole technology agnosticism thing?); I don’t know if all these generators are available yet, but there are some available here.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;config/application.rb&lt;/code&gt; file also houses some configuration for other things.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you need to configure internationalization, it’s been moved to &lt;code&gt;application.rb&lt;/code&gt;.  Rails 3 comes equipped with a really powerful i18n toolkit; if you haven’t seen it, you can learn a little more about it here.  The defaults that Rails sets up will work for most people (default locale is &lt;code&gt;en&lt;/code&gt; and all translations in the default directory are automatically imported), so you may not need to touch anything, but if you need to customize, this is the place to do it.&lt;/li&gt;
&lt;li&gt;You may want to set a default timezone.  I usually stick with UTC since it’s easy to convert on a per-user basis to their desired timezone, but you might want to set it your timezone or the server’s timezone.&lt;/li&gt;
&lt;li&gt;Your favorite old haunts from &lt;code&gt;config/environment.rb&lt;/code&gt; such as &lt;code&gt;config.plugins&lt;/code&gt;, &lt;code&gt;config.load_paths&lt;/code&gt;, etc. are still there (even though &lt;code&gt;config.gems&lt;/code&gt; is not).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Other configuration bits like custom inflections, mime types, and so on have been moved out into their own initializers that you can find under &lt;code&gt;config/initializers&lt;/code&gt;. [&lt;a href="http://github.com/jm/rails3_blog/commit/b613cef6f92ff7d3304da84dba530196ba51371d" target="_blank"&gt;b613cef6f92ff7d3304da84dba530196ba51371d&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;The last big piece of configuration you’ll need to add is a &lt;code&gt;Gemfile&lt;/code&gt; for &lt;code&gt;bundler&lt;/code&gt; (get more information on &lt;code&gt;Gemfiles&lt;/code&gt; and &lt;code&gt;bundler&lt;/code&gt; here and here).  We already have a basic &lt;code&gt;Gemfile&lt;/code&gt; that has the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Edit this Gemfile to bundle your application's dependencies.
source 'http://gemcutter.org'

gem "rails", "3.0.0.beta"

## Bundle edge rails:
# gem "rails", :git =&gt; "git://github.com/rails/rails.git"

gem "mysql"

## Bundle the gems you use:
# gem "bj"
# gem "hpricot", "0.6"
# gem "sqlite3-ruby", :require =&gt; "sqlite3"
# gem "aws-s3", :require =&gt; "aws/s3"

## Bundle gems used only in certain environments:
# gem "rspec", :group =&gt; :test
# group :test do
#   gem "webrat"
# end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice that it has added &lt;code&gt;mysql&lt;/code&gt; as a dependency since that’s what we set as the database (or whatever driver you selected, for example, &lt;code&gt;pg&lt;/code&gt; or &lt;code&gt;sqlite&lt;/code&gt;).  Since I want to write blog entries in Markdown, I’m going to add &lt;code&gt;rdiscount&lt;/code&gt; as a dependency.  To do so, I simply have to add this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem "rdiscount"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As I’ve said before, &lt;code&gt;bundler&lt;/code&gt; is much more powerful than &lt;code&gt;config.gem&lt;/code&gt;, and one of the great features it adds is the concept of a gem “group.”  For example, let’s say I want to use &lt;code&gt;mocha&lt;/code&gt;, but only when testing (obviously).  You would add this to your &lt;code&gt;Gemfile&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;group :test do
  gem "mocha"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now this gem will only be added in when testing.  This will also be useful for production only gems related to caching and what not. [&lt;a href="http://github.com/jm/rails3_blog/commit/598652fa49634eaa9d23ab8df652faf73dfd07f4" target="_blank"&gt;598652fa49634eaa9d23ab8df652faf73dfd07f4&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;Next, run &lt;code&gt;bundle pack&lt;/code&gt; if you want to vendor everything or &lt;code&gt;bundle install&lt;/code&gt; to install the gems to system gems.  After you’ve combed through this stuff and set whatever you need, you’re done configuring your application.  Now on to actually building something.&lt;/p&gt;

&lt;h2&gt;Building it out&lt;/h2&gt;

&lt;p&gt;So, we’re going to build a very simple blog (and expand it later).  First, let’s generate a scaffold for posts, since that’ll generate a lot of boilerplate code that we’ll go back and tweak:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails generate scaffold post title:string body:text
      invoke  active_record
      create    db/migrate/20100202054755_create_posts.rb
      create    app/models/post.rb
      invoke    test_unit
      create      test/unit/post_test.rb
      create      test/fixtures/posts.yml
       route  resources :posts
      invoke  scaffold_controller
      create    app/controllers/posts_controller.rb
      invoke    erb
      create      app/views/posts
      create      app/views/posts/index.html.erb
      create      app/views/posts/edit.html.erb
      create      app/views/posts/show.html.erb
      create      app/views/posts/new.html.erb
      create      app/views/posts/_form.html.erb
      create      app/views/layouts/posts.html.erb
      invoke    test_unit
      create      test/functional/posts_controller_test.rb
      invoke    helper
      create      app/helpers/posts_helper.rb
      invoke      test_unit
      create        test/unit/helpers/posts_helper_test.rb
      invoke  stylesheets
      create    public/stylesheets/scaffold.css
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next, run &lt;code&gt;rake db:migrate&lt;/code&gt; to create the database table for &lt;code&gt;Post&lt;/code&gt;.  Now if you go to &lt;code&gt;http://localhost:3000/posts&lt;/code&gt;, you should see the standard scaffold interface. [&lt;a href="http://github.com/jm/rails3_blog/commit/8f27fe53282de70343afadaedd583ecc279d535d" target="_blank"&gt;8f27fe53282de70343afadaedd583ecc279d535d&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;Let’s a take a look at the controller code; you’ll see a lot of actions that look sort of like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def show
  @post = Post.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml =&gt; @post }
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That’s some clean code, but in Rails 3, we can compress down even further with the &lt;a href="http://weblog.rubyonrails.org/2009/8/31/three-reasons-love-responder" target="_blank"&gt;&lt;code&gt;Responder&lt;/code&gt;&lt;/a&gt;.  This class wraps very common rendering logic up into some really clean helpers.  To use it, you’ll need to add what formats your actions respond with to the class:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class PostsController &lt; ApplicationController
  respond_to :html, :xml

  .
  .
  .
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So your &lt;code&gt;show&lt;/code&gt; action goes from the above to this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def show
  @post = Post.find(params[:id])

  respond_with(@post)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now the action will automatically look at the state of the object, the format requested, and respond accordingly.  So, for example, if you successfully create an object in &lt;code&gt;create&lt;/code&gt;, it will redirect to &lt;code&gt;show&lt;/code&gt;; if it fails, it will render &lt;code&gt;new&lt;/code&gt; (this is assuming, of course, you’re requesting HTML).  Of course, if you need custom logic, you’ll want to do something else, but these helpers make already clean, RESTful code even easier and cleaner.  Make sure to &lt;code&gt;rake&lt;/code&gt; to make sure you refactored it right! [&lt;a href="http://github.com/jm/rails3_blog/commit/53846f92393e10146fbf2d9b43b530a244d0137e" target="_blank"&gt;53846f92393e10146fbf2d9b43b530a244d0137e&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;Next, open up &lt;code&gt;config/routes.rb&lt;/code&gt;.  It should look something like this (with oodles of extra commented out routes):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Rails3Blog::Application.routes.draw do |map|
  resources :posts
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To set &lt;code&gt;PostController&lt;/code&gt;’s &lt;code&gt;index&lt;/code&gt; action to the root, we need to do two things.  First, remove &lt;code&gt;public/index.html&lt;/code&gt; otherwise it’ll always overtake any root route you set.  Next, add a &lt;code&gt;root&lt;/code&gt; route to &lt;code&gt;config/routes.rb&lt;/code&gt; like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Rails3Blog::Application.routes.draw do |map|
  resources :posts

  root :to =&gt; "posts#index"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now going to &lt;code&gt;http://localhost:3000&lt;/code&gt; should show the posts index page. [&lt;a href="http://github.com/jm/rails3_blog/commit/120c377c8ec1c138d600f9b9bc39bedf1d43afd4" target="_blank"&gt;120c377c8ec1c138d600f9b9bc39bedf1d43afd4&lt;/a&gt;]  OK, so now that most of the functionality is in place, let’s make it look presentable; here’s my version of the index template:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;% @posts.each do |post| %&gt;
  &lt;h2&gt;&lt;%= link_to post.title, post %&gt;&lt;/h2&gt;
  &lt;p&gt;posted at &lt;%= post.created_at.strftime('%D') %&gt;&lt;/p&gt;
  &lt;p&gt;&lt;%= post.body %&gt;&lt;/p&gt;
&lt;% end %&gt;

&lt;%= link_to 'New post', new_post_path %&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can see what other design edits I made in this commit [&lt;a href="http://github.com/jm/rails3_blog/commit/03b2c39d65331f7dfeb4ada89cf65604f7130e2d" target="_blank"&gt;03b2c39d65331f7dfeb4ada89cf65604f7130e2d&lt;/a&gt;].&lt;/p&gt;

&lt;p&gt;Now we need to add the Markdown functionality to the &lt;code&gt;Post&lt;/code&gt; model.  First, let’s generate a migration [&lt;a href="http://github.com/jm/rails3_blog/commit/2cbb0b04411ac1712a4f5039ed93bdad0cb6e76e" target="_blank"&gt;2cbb0b04411ac1712a4f5039ed93bdad0cb6e76e&lt;/a&gt;]:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails generate migration AddRenderedBodyToPosts rendered_body:text
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Migrate your database, and now we’re ready to move on to testing.  Write a simple test to make sure it renders the body after a save [&lt;a href="http://github.com/jm/rails3_blog/commit/af83a5a2e85a1679896e989f6828d1f5ee4aa7d3" target="_blank"&gt;af83a5a2e85a1679896e989f6828d1f5ee4aa7d3&lt;/a&gt;]:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'test_helper'

class PostTest &lt; ActiveSupport::TestCase
  test "renders Markdown after save" do
    post = Post.create(:title =&gt; "This post rocks.", :body =&gt; "Now *this* is an awesome post.")

    assert_equal "&lt;p&gt;Now &lt;em&gt;this&lt;/em&gt; is an awesome post.&lt;/p&gt;", post.rendered_body.chomp
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you &lt;code&gt;rake&lt;/code&gt; now, that test should fail.  So, let’s make it pass:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Post &lt; ActiveRecord::Base
  before_save :render_body

  def render_body
    self.rendered_body = RDiscount.new(self.body).to_html
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You should be all green [&lt;a href="http://github.com/jm/rails3_blog/commit/4730cd4e8601c74a05b9763d307b462f76e44b26" target="_blank"&gt;4730cd4e8601c74a05b9763d307b462f76e44b26&lt;/a&gt;]!  Now we’ll need to go back and change the instances of &lt;code&gt;body&lt;/code&gt; to &lt;code&gt;rendered_body&lt;/code&gt; on the &lt;code&gt;index&lt;/code&gt; and &lt;code&gt;show&lt;/code&gt; views.&lt;/p&gt;

&lt;p&gt;That’s pretty standard Rails stuff, so let’s do something Rails 3-specific now.  First, let’s add some &lt;a href="http://lindsaar.net/2010/1/31/validates_rails_3_awesome_is_true" target="_blank"&gt;validations&lt;/a&gt;; we’ll want to make sure that every post has a title and a body.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;test "requires title" do
  post = Post.create(:body =&gt; "Now *this* is an awesome post.")
  assert !post.valid?
  assert post.errors[:title]
end

test "requires body" do
  post = Post.create(:title =&gt; "This post rocks.")
  assert !post.valid?
  assert post.errors[:body]
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note the new API for Active Record errors (i.e., &lt;code&gt;[]&lt;/code&gt; rather then &lt;code&gt;on&lt;/code&gt;) [&lt;a href="http://github.com/jm/rails3_blog/commit/930e8868b0e4d8904d6f5090f6b445b0c428f71f" target="_blank"&gt;930e8868b0e4d8904d6f5090f6b445b0c428f71f&lt;/a&gt;].  Now, of course, we have to make them pass…&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Post &lt; ActiveRecord::Base
  before_save :render_body

  validates :title, :presence =&gt; true
  validates :body, :presence =&gt; true

  def render_body
    self.rendered_body = RDiscount.new(self.body).to_html
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you probably noticed, the API for Active Record validations I’ve used here is different (the validations shown are equivalent to a &lt;code&gt;validates_presence_of&lt;/code&gt; validation, which are still around) [&lt;a href="http://github.com/jm/rails3_blog/commit/178bb06839bb44978f42c922c9348bfe783da8b1" target="_blank"&gt;178bb06839bb44978f42c922c9348bfe783da8b1&lt;/a&gt;].  You can read a little more about the new style of validations &lt;a href="http://lindsaar.net/2010/1/31/validates_rails_3_awesome_is_true" target="_blank"&gt;here&lt;/a&gt;.  So, now if you try to create a post without a title or body, it’ll reject it.&lt;/p&gt;

&lt;h2&gt;More later…&lt;/h2&gt;

&lt;p&gt;I realize this introduction is extremely simple, but I’ll expand on it very soon (including authentication, commenting, post drafts, an API, spam protection, feeds, caching, etc. with a separate entry after it on deployment).  I’ll get to that sort of stuff very soon, but my next post is going to be a walkthrough of upgrading an app step by step (very similar to this entry).  Look for it in a few days!&lt;/p&gt;</description><link>http://omgbloglol.com/post/371893012</link><guid>http://omgbloglol.com/post/371893012</guid><pubDate>Fri, 05 Feb 2010 01:03:00 -0500</pubDate><category>rails3</category><category>ruby</category></item><item><title>rails-upgrade is now an official plugin</title><description>&lt;p&gt;I apologize for not getting another &lt;a href="http://omgbloglol.com/post/344792822/the-path-to-rails-3-introduction" target="_blank"&gt;Rails 3 upgrade post&lt;/a&gt; up this weekend, but I spent this weekend working on a few things.  First, I contributed a few little pieces to the Rails 3 release notes, which should be showing up on the Rails blog soon (&lt;strong&gt;edit:&lt;/strong&gt; or view them &lt;a href="http://guides.rails.info/3_0_release_notes.html" target="_blank"&gt;here&lt;/a&gt; right now), but most of my time was devoted to a bigger project.&lt;/p&gt;

&lt;p&gt;My little gem &lt;a href="http://github.com/jm/rails-upgrade" target="_blank"&gt;&lt;code&gt;rails-upgrade&lt;/code&gt;&lt;/a&gt; is now &lt;a href="http://github.com/rails/rails_upgrade" target="_blank"&gt;&lt;code&gt;rails_upgrade&lt;/code&gt;&lt;/a&gt;, an officially blessed upgrade tool that will be maintained by myself and the Rails team.  You can get it from here: &lt;a href="http://github.com/rails/rails_upgrade" target="_blank"&gt;&lt;a href="http://github.com/rails/rails_upgrade" target="_blank"&gt;http://github.com/rails/rails_upgrade&lt;/a&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To use it now, simply install the plugin:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;script/plugin install git://github.com/rails/rails_upgrade.git
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The plugin adds the following tasks:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rake rails:upgrade:check      # Runs a battery of checks on your Rails 2.x app
                              # and generates a report on required upgrades for Rails 3
rake rails:upgrade:gems       # Generates a Gemfile for your Rails 3 app out of your config.gem directives
rake rails:upgrade:routes     # Create a new, upgraded route file from your current routes.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Simply run those tasks in the same way you ran the commands with the &lt;code&gt;rails-upgrade&lt;/code&gt; gem.  In the near future, I plan on expanding the checks for deprecated pieces to handle some of the less obvious changes, adding some generators for other changes (like &lt;code&gt;config/application.rb&lt;/code&gt;), and adding some extra tools (ideas/suggestions certainly welcome).&lt;/p&gt;

&lt;p&gt;Anyhow, I’m really looking forward to seeing this project become a dependable upgrade tool.  If you have any ideas or find any bugs, please contact me via e-mail or &lt;a href="http://twitter.com/jm" target="_blank"&gt;Twitter&lt;/a&gt; or, even better, fork it and handle it yourself!&lt;/p&gt;</description><link>http://omgbloglol.com/post/364624593</link><guid>http://omgbloglol.com/post/364624593</guid><pubDate>Mon, 01 Feb 2010 00:52:00 -0500</pubDate></item><item><title>rails-upgrade: Automating a portion of the Rails 3 upgrade process</title><description>&lt;p&gt;&lt;em&gt;If you’re looking for more info on upgrading, don’t miss out on my other posts on Rails 3 starting &lt;a href="http://omgbloglol.com/post/344792822/the-path-to-rails-3-introduction" target="_blank"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: This is now an official, blessed plugin, so use that rather than this gem.  &lt;a href="http://omgbloglol.com/post/364624593/rails-upgrade-is-now-an-official-plugin" target="_blank"&gt;More info here&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’ve been playing with upgrading some apps to Rails 3 (some open-source, some not), and I’ve sort of gotten some of the process down to a science.  So what does a developer do when something is down to a process?  Automate!&lt;/p&gt;

&lt;p&gt;I’ve created a (pretty hacky) gem named &lt;a href="http://github.com/jm/rails-upgrade" target="_blank"&gt;rails-upgrade&lt;/a&gt; (installable by a simple &lt;code&gt;gem install rails-upgrade&lt;/code&gt;) to automate some of the more annoying parts of the upgrade from Rails 2.x to Rails 3.  So far, it has three parts…&lt;/p&gt;

&lt;h2&gt;Find out what parts need to be upgraded&lt;/h2&gt;

&lt;p&gt;I’ve assembled a &lt;a href="http://github.com/jm/rails-upgrade/blob/master/lib/rails-upgrade/upgraders/check.rb" target="_blank"&gt;battery of checks&lt;/a&gt; to run on your app for obvious things that need to be upgraded.  To get a report, simply run this in a Rails root:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails-upgrade check
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It checks over some things, then generates a report like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;named_scope is now just scope
The named_scope method has been renamed to just scope.
More information: &lt;a href="http://github.com/rails/rails/commit/d60bb0a9e4be2ac0a9de9a69041a4ddc2e0cc914" target="_blank"&gt;http://github.com/rails/rails/commit/d60bb0a9e4be2ac0a9de9a69041a4ddc2e0cc914&lt;/a&gt;

The culprits: 
    - app/models/group.rb
    - app/models/post.rb

Deprecated ActionMailer API
You're using the old ActionMailer API to send e-mails in a controller, model, or observer.
More information: &lt;a href="http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3" target="_blank"&gt;http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3&lt;/a&gt;

The culprits: 
    - app/controllers/application.rb
    - app/controllers/feedback_controller.rb

Old ActionMailer class API
You're using the old API in a mailer class.
More information: &lt;a href="http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3" target="_blank"&gt;http://lindsaar.net/2010/1/26/new-actionmailer-api-in-rails-3&lt;/a&gt;

The culprits: 
    - app/models/post.rb
    - app/models/user.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It shows and explains the issue, where to get more information on it, and which files the issue was found in.  It checks a lot more than that report shows (e.g., looks for old generators, busted plugins, environment.rb conversion requirements, old style routes, etc.).  It doesn’t cover everything 100% probably, but I’ve found it’s great for quickly identifying some low hanging fruit in upgrading.&lt;/p&gt;

&lt;h2&gt;Upgrading routes&lt;/h2&gt;

&lt;p&gt;The gem will also upgrade your routes as best it can.  To generate a new routes file, simply run this inside of a Rails application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails-upgrade routes
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I’ve tested it on some quite complicated routes files and it did fine, but it does have some minor quirks (i.e., it flattens &lt;code&gt;with_options&lt;/code&gt; blocks currently…that might change if I feel like putting the effort into it…or if one of you patches it :).  It takes a routes file like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ActionController::Routing::Routes.draw do |map|
  map.resources :posts, :collection =&gt; {:drafts =&gt; :get, :published =&gt; :get}

  map.resources(
    :users,
    :groups,
    :pictures
  )

  map.login  '/login',  :controller =&gt; 'sessions', :action =&gt; 'new'
  map.logout '/logout', :controller =&gt; 'sessions', :action =&gt; 'destroy'

  map.connect '/about', :controller =&gt; 'static', :action =&gt; 'about'

  map.connect ':controller/:action/:id.:format'
  map.connect ':controller/:action/:id'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And makes a new one like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;YourApp::Application.routes do
  resources :posts do
    collection do
      get :drafts
      get :published
    end  
  end

  resources :users
  resources :groups
  resources :pictures

  match '/login' =&gt; 'sessions#new', :as =&gt; :login
  match '/logout' =&gt; 'sessions#destroy', :as =&gt; :logout
  match '/about' =&gt; 'static#about'
  match '/:controller(/:action(/:id))'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The formatting isn’t quite this nice when it comes straight out of the script (I’m working on that), but you get the idea.  I’m still tweaking/adding things to this script, but as far as I know it supports every feature of the Rails 2.x router.  Fixing the formatting bugs are my first priority, simply because they’re really annoying.&lt;/p&gt;

&lt;h2&gt;Creating Gemfiles&lt;/h2&gt;

&lt;p&gt;The last piece is a Gemfile generator; it takes your &lt;code&gt;config.gem&lt;/code&gt; directives and generates a nice Gemfile (even including the required Rails stuff).  To run it, simply execute:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails-upgrade gems
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That will take an &lt;code&gt;environment.rb&lt;/code&gt; with these &lt;code&gt;config.gem&lt;/code&gt; calls:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;config.gem "bj"
config.gem "hpricot", :version =&gt; '0.6', :source =&gt; "http://code.whytheluckystiff.net"
config.gem "sqlite3-ruby", :lib =&gt; "sqlite3"
config.gem "aws-s3", :lib =&gt; "aws/s3"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And generate this &lt;code&gt;Gemfile&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Edit this Gemfile to bundle your application's dependencies.
# This preamble is the current preamble for Rails 3 apps; edit as needed.
directory "/path/to/rails", :glob =&gt; "{*/,}*.gemspec"
git "git://github.com/rails/arel.git"
git "git://github.com/rails/rack.git"
gem "rails", "3.0.pre"

gem 'bj', 
source 'http://code.whytheluckystiff.net'
gem 'hpricot', '0.6'
gem 'sqlite3-ruby', :require_as=&gt;"sqlite3"
gem 'aws-s3', :require_as=&gt;"aws/s3"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then it’s just as simple as &lt;code&gt;gem bundle&lt;/code&gt;.  Again, I’ve tested this on some fairly complex sets of gem requirements, so it should stand up to most sets.&lt;/p&gt;

&lt;p&gt;If you find a bug or want to expand the checks and upgrade scripts or, like, add some tests (please do!), then &lt;a href="http://github.com/jm/rails-upgrade" target="_blank"&gt;hit it up on Github&lt;/a&gt;, fork it, and send me a message.  If you want to simply install the gem and run it, then just run &lt;code&gt;gem install rails-upgrade&lt;/code&gt; then &lt;code&gt;rails-upgrade &lt;whatever&gt;&lt;/code&gt; inside the Rails application directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE: This is now an official, blessed plugin, so use that rather than this gem.  &lt;a href="http://omgbloglol.com/post/364624593/rails-upgrade-is-now-an-official-plugin" target="_blank"&gt;More info here&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you’re looking for more info on upgrading, don’t miss out on my other posts on Rails 3 starting &lt;a href="http://omgbloglol.com/post/344792822/the-path-to-rails-3-introduction" target="_blank"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;</description><link>http://omgbloglol.com/post/359147788</link><guid>http://omgbloglol.com/post/359147788</guid><pubDate>Fri, 29 Jan 2010 00:25:00 -0500</pubDate></item><item><title>The Path to Rails 3: Approaching the upgrade</title><description>&lt;p&gt;Now that we’ve looked at some of the core architecture, I’d like to shift my focus first to upgrading an application.  Originally I had planned on writing about upgrading plugins first, but &lt;a href="http://groups.google.com/group/rubyonrails-core/browse_thread/thread/00d8f6f406b96031#" target="_blank"&gt;apparently that API isn’t quite stable&lt;/a&gt;.  So, I figured rather than write a blog post that will be deprecated in 2 weeks, I’d rather write one that will be deprecated in 3-6 months instead.  So, this post will focus on getting your app bootable, and it will be followed by a succession of articles that contain tips and scripts to help you upgrade the various components (i.e., routes, models, etc. are topics I’m working on right now).&lt;/p&gt;

&lt;p&gt;The first step towards an upgraded app you need to take is to actually get Rails 3.  As noted in the previous post, you can follow &lt;a href="http://yehudakatz.com/2009/12/31/spinning-up-a-new-rails-app/" target="_blank"&gt;Yehuda’s directions&lt;/a&gt; or use &lt;a href="http://github.com/bry4n/rails3-install" target="_blank"&gt;Bryan Goines’s great little script&lt;/a&gt;.  Once you’ve got it up and running, I suggest you “generate a new app” on top of your current one (i.e., run the generator and point the app path to your current Rails 2.x app’s path).  Running the generator again will actually update the files you need to update, generate the new ones, and so on.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ruby /path/to/rails/railties/bin/rails ~/code/my_rails2_app/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that the argument is a &lt;em&gt;path&lt;/em&gt;, not a name as in previous Rails versions.  If you got an error about your Ruby version, upgrade it!  If you use &lt;a href="http://rvm.beginrescueend.com/" target="_blank"&gt;rvm&lt;/a&gt; it’ll be totally painless.  Now, be careful which files you let Rails replace since a lot of them can be edited much more simply (I’ll show you how here) than they can be reconstructed (unless you really like digging around in &lt;code&gt;git diff&lt;/code&gt; and previous revisions), but do take note of what they are since you will likely need to change something in them.  As a general list, it’s probably safe to let it update these files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Rakefile&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;README&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;config/boot.rb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;public/404.html&lt;/code&gt; (unless you’ve customized it)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;public/500.html&lt;/code&gt; (unless you’ve customized it)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;public/javascripts/*&lt;/code&gt; (if you don’t have a lot of version dependent custom JavaScript)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;script/*&lt;/code&gt; (they probably wouldn’t work with the new Rails 3 stuff in their old form anyhow) &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And, you probably don’t want to let it update these files since you’ve likely made modifications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.gitignore&lt;/code&gt; (unless you don’t really care; the new standard one is pretty good)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;app/helpers/application_helper.rb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;config/routes.rb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;config/environment.rb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;config/environments/*&lt;/code&gt; (unless you haven’t touched these as many people don’t)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;config/database.yml&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;doc/README_FOR_APP&lt;/code&gt; (you &lt;em&gt;do&lt;/em&gt; write this, don’t you?)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;test/test_helper.rb&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Of course, these lists won’t apply in every situation, but in general I think that’s how it’ll break down.  Now, on to the things you’ll need to change…&lt;/p&gt;

&lt;h2&gt;
&lt;code&gt;config.gem&lt;/code&gt; is dead, long live &lt;code&gt;bundler&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Everyone and their brother complained about Rails’ handling of vendored/bundled gems since &lt;code&gt;config.gem&lt;/code&gt; was added sometime ago (just search for “config.gem sucks” or “config.gem issues OR problems” and you’ll see).  Between issues with requiring the gems properly to problems with the gem detection (I can’t tell you how many times I nixed a gem from the list because it kept telling me to install it even though it was already installed), Rails seriously needed a replacement for such a vital piece of infrastructure.  These days we have &lt;a href="http://github.com/carlhuda/bundler" target="_blank"&gt;Yehuda Katz’s excellent bundler&lt;/a&gt;, which will be the standard way to do things in Rails 3.&lt;/p&gt;

&lt;p&gt;Essentially, bundler works off of &lt;code&gt;Gemfiles&lt;/code&gt; (kind of like &lt;code&gt;Rakefiles&lt;/code&gt; in concept) that contain a description of what gems to get and how to get them.  Moving your gem requirements to a &lt;code&gt;Gemfile&lt;/code&gt; isn’t as simple as copying them over, but it’s not terribly difficult:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# This gem requirement...
config.gem "aws-s3", :version =&gt; "0.5.1", 
           :lib =&gt; "aws/s3", :source =&gt; "http://gems.omgbloglol.com"

# ...becomes:
source "http://gems.omgbloglol.com"
gem "aws-s3", "0.5.1", :require_as =&gt; "aws/s3"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, it’s not too hard.  It’s basically just removing the &lt;code&gt;config&lt;/code&gt; object and moving some keys around.  Here’s a specific list of changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove the &lt;code&gt;config&lt;/code&gt; object&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:lib&lt;/code&gt; key becomes the &lt;code&gt;:require_as&lt;/code&gt; key&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;:version&lt;/code&gt; key becomes a second, optional string argument&lt;/li&gt;
&lt;li&gt;Move &lt;code&gt;:source&lt;/code&gt; arguments to a &lt;code&gt;source&lt;/code&gt; call to add it to the sources&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once you create a &lt;code&gt;Gemfile&lt;/code&gt;, you simply have to run &lt;code&gt;bundle pack&lt;/code&gt; and you’re done!&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bundler&lt;/code&gt; is much more powerful than &lt;code&gt;config.gem&lt;/code&gt;, and it helps you do more advanced tasks (e.g., bundle directly from a Git repository, specify granular paths, etc.).  So, once you move your &lt;code&gt;config.gem&lt;/code&gt; calls over, you may want to look into the &lt;a href="http://github.com/carlhuda/bundler/blob/master/README.markdown" target="_blank"&gt;new features&lt;/a&gt;; they may be something you had wished &lt;code&gt;config.gem&lt;/code&gt; had but didn’t!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: I’ve noticed some activity in Yehuda/Carl’s Githubs to do with a bundler replacement called gemfile; I’ll watch that closely to make sure there are no major breaking changes in the API/operation.  If there are, I’ll definitely post here!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Move to &lt;code&gt;Rails::Application&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In all previous Rails version, most configuration and initialization happened in &lt;code&gt;config/environment.rb&lt;/code&gt;, but in Rails 3, most of this logic is moved to &lt;code&gt;config/application.rb&lt;/code&gt; and a host of special initializers in &lt;code&gt;config/initializers&lt;/code&gt;.  The &lt;code&gt;config/environment.rb&lt;/code&gt; file basically looks like this now:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
YourApp::Application.initialize!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Simple: the &lt;code&gt;application.rb&lt;/code&gt; file is required and then the &lt;code&gt;Application&lt;/code&gt; is initialized.  The &lt;code&gt;YourApp&lt;/code&gt; constant is generated based on the folder name for your app (i.e., &lt;code&gt;rails ~/code/my_super_app&lt;/code&gt; would make it &lt;code&gt;MySuperApp&lt;/code&gt;), so name it wisely!  It doesn’t have any special relationship to the folder the app lives in so you can rename it at will (so long as you do it everywhere it’s used), but you’ll be using this constant in a few places so make it something useful.&lt;/p&gt;

&lt;p&gt;Now you need an &lt;code&gt;application.rb&lt;/code&gt;; if you generated the files using the Rails 3 generator, you should have one that looks something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module TestDevApp
  class Application &lt; Rails::Application
    # ...Insert lots of example comments here...

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters &lt;&lt; :password
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For the most part, your &lt;code&gt;config.*&lt;/code&gt; calls should transfer straight over: just copy and paste them inside the class body.  There are a few new ones that I’ll be covering later on in this series that you might want to take advantage of.  If you run into a &lt;code&gt;config.*&lt;/code&gt; method that &lt;em&gt;doesn’t&lt;/em&gt; work (other than &lt;code&gt;config.gem&lt;/code&gt; which obviously won’t work), then please post in the comments, and I’ll add it into a list here.&lt;/p&gt;

&lt;p&gt;You’ll also notice that many things that were once in &lt;code&gt;environment.rb&lt;/code&gt; have been moved out into new initializers (such as custom inflections).  You’ll probably want to/have to move these things out of &lt;code&gt;application.rb&lt;/code&gt; and into the proper initializer.  If you opted to keep any custom initializers or specialized environment file during the generation process, you’ll probably need to go in there and update the syntax.  Many of these (especially the environment files) now requires a new block syntax:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Rails 2.x
config.cache_classes = false
config.action_controller.perform_caching = true

# Rails 3.x
YourApp::Application.configure do
  config.cache_classes = false
  config.action_controller.perform_caching = true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All configuration happens inside the &lt;code&gt;Application&lt;/code&gt; object for your Rails app, so these, too, need to be executed inside of it.  As I said previously, most things in there should still work fine once wrapped in the block, but if they don’t please comment so I can post about it/figure out the issue.&lt;/p&gt;

&lt;h2&gt;Ch-ch-chaaange in the router&lt;/h2&gt;

&lt;p&gt;You’ve probably heard a &lt;a href="http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/" target="_blank"&gt;lot of talk&lt;/a&gt; about &lt;a href="http://rizwanreza.com/2009/12/20/revamped-routes-in-rails-3" target="_blank"&gt;Rails and routes&lt;/a&gt; and &lt;a href="http://github.com/jm/krauter" target="_blank"&gt;new&lt;/a&gt; &lt;a href="http://github.com/josh/rack-mount" target="_blank"&gt;implementations&lt;/a&gt; and this and that.  Let me tell you: the new router is pretty awesome.  The problem is that it’s not exactly easy to migrate existing routes over to the new hotness.  Fortunately (for now, at least) they have a legacy route mapper so your routes won’t break any time soon.  Of course, you should always try to update things like to this to keep up with the version you’re running (i.e., never depend on the benevolence of the maintainers to keep your ghetto legacy code going while using a new version for everything else).&lt;/p&gt;

&lt;p&gt;But don’t worry.  Upgrading your routes is fairly simple so long as you haven’t done anything complex; it’s just not as easy as copying and pasting.  Here are a few quick run-throughs (a detailed guide is coming later)…&lt;/p&gt;

&lt;p&gt;Upgrading a basic route looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Old style
map.connect '/posts/mine', :controller =&gt; 'posts', :action =&gt; 'index'

# New style
match '/posts/mine', :to =&gt; 'posts#index'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A named route upgrade would look like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Old style
map.login '/login', :controller =&gt; 'sessions', :action =&gt; 'new'

# New style
match '/login', :to =&gt; 'sessions#new', :as =&gt; 'login'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Upgrading a resource route looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Old style
map.resources :users, :member =&gt; {:ban =&gt; :post} do |users|
  users.resources :comments
end

# New style
resources :users do
  member do
    post :ban
  end

  resources :comments
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And upgrading things like the root path and so on looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Old style
map.root :controller =&gt; 'home', :action =&gt; 'index'

map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'

# New style
root :to =&gt; 'home#index'

match '/:controller(/:action(/:id))'
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I’ll be writing another entry later on about the router’s new DSL and looking at some common patterns from Rails 2 apps and how they can work in Rails 3.  Some of the new methods add some very interesting possibilities.&lt;/p&gt;

&lt;h2&gt;Some minor changes&lt;/h2&gt;

&lt;p&gt;There are a few minor changes that shouldn’t really mess with too much (except perhaps the first one here…).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constants are out, module methods are in&lt;/strong&gt; Ah, nostalgia.  Remember when &lt;code&gt;RAILS_ROOT&lt;/code&gt; and friends were cool?  Well, now they’re lame and are going away in a flare of fire and despair.  The new sexy way to do it: &lt;code&gt;Rails.root&lt;/code&gt; and &lt;a href="http://api.rubyonrails.org/classes/Rails.html" target="_blank"&gt;its module method pals&lt;/a&gt;.  So, remember.  Old and busted: &lt;code&gt;RAILS_ROOT&lt;/code&gt; and its depraved, constant brethren.  New hotness: &lt;code&gt;Rails.root&lt;/code&gt; and its ilk.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rack is Serious Business™&lt;/strong&gt;  You might have noticed that the Rails 3 generator gives you a &lt;code&gt;config.ru&lt;/code&gt; in your application root.  Rails 3 is going gung ho on &lt;a href="http://rack.rubyforge.org/" target="_blank"&gt;Rack&lt;/a&gt;, everyone’s favorite web server interface, and as such, a &lt;code&gt;config.ru&lt;/code&gt; is now required in your application to tell Rack how to mount it.  Like I said, the Rails 3 generator will spit one out for you, but if you’re doing a manual upgrade for some reason, then you’ll need to add one yourself.&lt;/p&gt;

&lt;p&gt;Interesting note: Remember that &lt;code&gt;YourApp::Application&lt;/code&gt; class you created earlier in &lt;code&gt;application.rb&lt;/code&gt;?  That’s your Rack endpoint; that’s why your &lt;code&gt;config.ru&lt;/code&gt; looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment',  __FILE__)
run YourApp::Application.instance
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Neat, eh?  That’s why I suggested you pick a meaningful name for that class: its touch runs quite deep in the stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.gitignore to the rescue&lt;/strong&gt; Rails also now automatically generates a &lt;code&gt;.gitignore&lt;/code&gt; file for you (you can tell it not to by providing the &lt;code&gt;--skit-git&lt;/code&gt; option to the generator).  It’s fairly simple, but covers the 95% case for most Rails developers, and it’s certainly a welcome addition to the toolbox.  It was always annoying having to create one every time and either dig up a previous one to copy or try to remember the syntax of how to make it ignore the same stuff.&lt;/p&gt;

&lt;p&gt;After upgrading this stuff, you probably have a booting application.  Of course, these are a lot of moving parts that could derail this plan: old and busted plugins, gem stupidity, weirdness in your config files, lib code that’s problematic, application code that needs upgrading (I can almost guarantee that), and so on.  In any event, these are a few steps in the right direction; subsequent posts will show you the rest.&lt;/p&gt;

&lt;h2&gt;Posts in this series&lt;/h2&gt;

&lt;p&gt;I’m posting a whole series on Rails 3; be sure to catch these other posts!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="http://omgbloglol.com/post/344792822/the-path-to-rails-3-introduction" target="_blank"&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Approaching the Upgrade&lt;/li&gt;
&lt;/ol&gt;</description><link>http://omgbloglol.com/post/353978923</link><guid>http://omgbloglol.com/post/353978923</guid><pubDate>Tue, 26 Jan 2010 01:40:00 -0500</pubDate></item><item><title>The Path to Rails 3: Introduction</title><description>&lt;p&gt;Wow, over half a year with no blog post.  That may be a new record for blog laziness for me, but fear not!  This bout of sloth shall not last, and the dearth of blog entries shall come to and end!  This cure should come partially because I’ve switched to &lt;a href="http://tumblr.com" target="_blank"&gt;Tumblr&lt;/a&gt; and can now compose my entries in Markdown, and partially because that’s part of my whole Get a Better Life New Year’s Resolution Package 2.0™ (coming to a burned out programmer near you in 2011!).  Let’s hope this pans out, or, at least, I don’t end up strung out in the gutter tapping out entries into Textmate.  Anyhow, to catch you up, here’s what’s happened since my last post:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://rubyhoedown.com" target="_blank"&gt;The Ruby Hoedown&lt;/a&gt; 2009 was a smashing success.  The whole free conference thing went off without a hitch, and I think everyone had a great time (at least I did).&lt;/li&gt;
&lt;li&gt;I’ve left &lt;a href="http://entp.com" target="_blank"&gt;entp&lt;/a&gt; and now work at &lt;a href="http://intridea.com" target="_blank"&gt;Intridea&lt;/a&gt;, which has been fabulous thus far.  I get to dabble in a few different technologies, and our office (which I don’t work out of but wish I did) is about 2 blocks north of the White House.  Epic win.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://books.slashdot.org/story/09/12/28/1424255/Ruby-In-Practice?art_pos=1" target="_blank"&gt;Ruby in Practice was featured on Slashdot&lt;/a&gt;, making it jump about 650,000 slots in the &lt;a href="http://www.amazon.com/Ruby-Practice-Jeremy-McAnally/dp/1933988479" target="_blank"&gt;Amazon&lt;/a&gt; sales ranks.  Unfortunately, it has once again floated back down into obscurity, below such fine volumes as “&lt;a href="http://www.amazon.com/Whats-Your-Poo-Telling-You/dp/0811857824/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1263825087&amp;sr=1-1" target="_blank"&gt;What’s Your Poo Telling You?&lt;/a&gt;” and just above vital classics like “&lt;a href="http://www.amazon.com/Much-Ado-About-Nothing-Restored/dp/158715501X/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1263825124&amp;sr=1-1" target="_blank"&gt;Much Ado About Nothing: The Restored Klingon Text&lt;/a&gt;”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But that’s not what this post is about.  This post is kicking off a series that I’m doing about moving your skills and migrating your code to Rails 3.  I’ll be sharing some practical insights and covering some pretty in-depth topics as we go along (I’ve got some notes for entries about upgrading plugins, taking advantage of new features like the agnosticism, migrating applications, and so on), but before I go into a lot of specifics, I thought it might be useful to go over some of the high-level philosophical and architectural changes that have gone on in the Rails code between versions 2 and 3.&lt;/p&gt;

&lt;h2&gt;The Big Picture&lt;/h2&gt;

&lt;p&gt;When the Merb/Rails merge was announced, I was worried that we were going to end up in some weird tangle of Merbilicity and Railsishness when the final product came around.  I don’t think anyone wants some Brangelina of the web framework world all up in their business.  Fortunately, the gents on the Rails core team are smart and classy and have navigated the waters of cooperation and compromise extremely well.  We’re getting the best of both world here, folks: the ease of use and packaging of Rails with the juicy technical bits of Merb.  Who can argue with that?&lt;/p&gt;

&lt;p&gt;But to make that Epic Code Merge of Awesome™ happen, of course there had to be some changes.  These big picture changes have concentrated on a few key areas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://yehudakatz.com/2009/07/19/rails-3-the-great-decoupling/" target="_blank"&gt;Decoupling Rails components from one another&lt;/a&gt; as much as possible, making things more modular and a la carte.&lt;/li&gt;
&lt;li&gt;Pulling in improvements from Merb and rewrite/refactor much of the internals to &lt;a href="http://www.engineyard.com/blog/2009/rails-and-merb-merge-performance-part-2-of-6/" target="_blank"&gt;improve performance&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Exposing explicit, documented API’s for &lt;a href="http://www.engineyard.com/blog/2010/rails-and-merb-merge-plugin-api-part-3-of-6" target="_blank"&gt;common tasks&lt;/a&gt; and integration of wider ecosystem components from testing, ORM, etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In order to hit these objectives, DHH, Yehuda, Josh, and the rest of the Rails team have extracted things into some new components, expanded others, and removed others to allow for agnosticism.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_kwkd2xNc0n1qz4tvk.png" alt="Overview"/&gt;&lt;/p&gt;

&lt;p&gt;The general movement seems to be from a monolithic, one-stop shop approach to a looser ecosystem of code that works together with a straightforward set of sensible defaults.  You’re no longer “locked in” to ActiveRecord or made to use code injection and hacks and such to get your testing framework integrated.  Instead, there are hooks all over the place to cover this sort of stuff (which I will cover later on in the series!) that let generators generate things for the various options or helpers include different modules.  It’s a great way to support an ecosystem with &lt;a href="http://paulbarry.com/articles/2010/01/13/customizing-generators-in-rails-3" target="_blank"&gt;an established API&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Lifecycle changes&lt;/h2&gt;

&lt;p&gt;One of the biggest movements in the codebase has been a shift towards using simple, composed components and a lot of Rack in the request chain rather than specialized, one-off classes.  This has affected a lot of things, but one of the major changes has been the addition of &lt;a href="http://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch.rb" target="_blank"&gt;Action Dispatch&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_kwkd3pi9Il1qz4tvk.png" alt="The request chain"/&gt;&lt;/p&gt;

&lt;p&gt;Action Dispatch is a “new” component in Action Pack (extracted and expanded from the previous logic) that handles a number of things related to requests and responses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Request handling and parameter parsing&lt;/li&gt;
&lt;li&gt;Sessions, Rails’ flash, and cookie storage&lt;/li&gt;
&lt;li&gt;File uploads  &lt;/li&gt;
&lt;li&gt;Routing, URL matching, and rescuing errors  &lt;/li&gt;
&lt;li&gt;HTTP conditional GETs&lt;/li&gt;
&lt;li&gt;Client response and HTTP status code  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Breaking this functionality out into its own component and decoupling much of it creates a much more flexible call stack for requests, meaning you can jack into the process easier with your own logic or improve the existing functionality.  I’m sure we’ll see a lot of plugins taking advantage of this to create &lt;a href="http://yehudakatz.com/2009/08/26/how-to-build-sinatra-on-rails-3/" target="_blank"&gt;interesting middleware hacks&lt;/a&gt;, improve callbacks and lifecycle methods, &lt;a href="http://www.viget.com/extend/rack-support-in-rails-why-it-matters/" target="_blank"&gt;hack in their own middlewares&lt;/a&gt; to handle specialized logic, or even plug in improved or application-specific routers.  This is one of the more interesting pieces I’m interested in seeing develop, since it opens a lot of possibilities that were previously much more difficult to reach.&lt;/p&gt;

&lt;h2&gt;Making controllers flexible&lt;/h2&gt;

&lt;p&gt;As a result of the changes in the request chain, the controller stack has also seen a significant overhaul.  Previously, every controller inherited from &lt;code&gt;ActionController::Base&lt;/code&gt; (either directly or by inheriting from &lt;code&gt;ApplicationController&lt;/code&gt;) and slimming down the call stack was accomplished by either (a) previous to Rails 2.3, building a smaller app with Sinatra or Rack to sit next to your main Rails application or (b) post-Rails 2.3, using Rack Metal/middlewares.&lt;/p&gt;

&lt;p&gt;In Rails 3.0, this concept of middleware plays an even more central role to how the controller hierarchy is arranged.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_kwkd4ngQ7j1qz4tvk.png" alt="The new controller stack"/&gt;&lt;/p&gt;

&lt;p&gt;The bottom of the stack is &lt;a href="http://yehudakatz.com/2009/03/20/another-dispatch-abstractcontroller/" target="_blank"&gt;&lt;code&gt;AbstractController&lt;/code&gt;&lt;/a&gt;, a very low level “controller.”  Rails uses this class to abstract away essentials like rendering, layouts, managing template paths, and so on, while leaving more concrete implementation details to its subclasses.  &lt;code&gt;AbstractController&lt;/code&gt; exists only to provide these facilities to subclasses.  That is, you should not use this class directly; if you want something super-slim, create a subclass and implement &lt;code&gt;render&lt;/code&gt; and a few other pieces).&lt;/p&gt;

&lt;p&gt;Each subsequent jump up the hierarchy is actually a class that inherits from the previous, each including modules to compose its behavior.  So, if you want to create something slim without implementing a lot of plumbing, use the next rung on the compositional ladder: &lt;a href="http://yardoc.org/docs/rails/ActionController/Metal" target="_blank"&gt;&lt;code&gt;ActionController::Metal&lt;/code&gt;&lt;/a&gt;.  &lt;code&gt;Metal&lt;/code&gt; essentially exposes super simple Rack endpoints that you can then include extra modules into to add more &lt;code&gt;ActionController&lt;/code&gt; functionality (check out an example &lt;a href="http://yehudakatz.com/2009/12/20/generic-actions-in-rails-3/" target="_blank"&gt;here&lt;/a&gt;).  These little classes are excellent for replacing those Rack/Sinatra apps for file uploads or what have you while still having the power to easily build out to rather rich controller objects.&lt;/p&gt;

&lt;p&gt;Finally, if you need the full monty (i.e., like a controller in Rails 2), then you’ll need to inherit from &lt;code&gt;ActionController::Base&lt;/code&gt;.  This class inherits from &lt;code&gt;ActionController::Metal&lt;/code&gt; and includes a slew of modules to handle things like redirecting the user, handling implicit rendering, and a number of helpers for other stuff like caching.&lt;/p&gt;

&lt;p&gt;The advantage of taking this approach is that you can take one of the base classes like &lt;code&gt;Metal&lt;/code&gt; and include your own modules to create specialized controllers.  I foresee someone using this to create a simple way to serve up resources (e.g., &lt;code&gt;PostsController &lt; ResourcesController(:posts)&lt;/code&gt; or something like that) much like people have done previously (José Valim’s &lt;a href="http://github.com/josevalim/inherited_resources" target="_blank"&gt;inherited_resources&lt;/a&gt; jumps to mind) or using it as a way to quickly build API backends.  This is the other piece of the major refactor that excites me, since we’re looking at a new way to construct reusable code and assemble it into usable applications.&lt;/p&gt;

&lt;h2&gt;Where models are concerned&lt;/h2&gt;

&lt;p&gt;Though the public API for models is generally the same (with a few additions and changes that I’ll cover in a subsequent post), Active Record is now powered by the brain-melting Active Relation, a powerful relational algebra layer.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_kwkd5gKBv81qz4tvk.png" alt="Intelligent SQL generation"/&gt;&lt;/p&gt;

&lt;p&gt;What does that mean for you?  Well, basically it means that Active Record will be smarter and more powerful.  Rather than fairly naïve SQL generation, it uses some fancy &lt;a href="http://db.grussell.org/section010.html" target="_blank"&gt;mathemagical&lt;/a&gt; approach that should generate smarter queries.  Frankly, I haven’t had a lot of time to research these features for myself, but when I do, I’ll be sure to post (or if you’ve posted about this stuff somewhere, then by all means let me know).&lt;/p&gt;

&lt;p&gt;The second big change in Model Land is the extraction of much of the rich logic in Active Record objects like callbacks, validations, serialization, and so on into the Active Model module.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_kwkd5uveWs1qz4tvk.png" alt="ActiveModel rawks."/&gt;&lt;/p&gt;

&lt;p&gt;You can use this module to &lt;a href="http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/" target="_blank"&gt;make any object behave like an Active Record object&lt;/a&gt;; for example, let’s say you wanted to add some validations to a PORO representing a host on a network:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Host
  include ActiveModel::Validations

  validates_presence_of :hostname

  attr_accessor :ip_address, :hostname, :operating_system
  def initialize(hostname, ip_address, operating_system)
    @hostname, @ip_address, @operating_system = host, ip_address, operating_system
  end
end

h  = Host.new("skull", "24.44.129.10", "Linux")
h.valid?    # =&gt; true
h.hostname = nil
h.valid?    # =&gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To get this functionality, simply include &lt;code&gt;ActiveModel::Validations&lt;/code&gt; and start implementing the methods.  It’s possible to exercise fine-grained control over how the validations operate, how the validator gets the object’s attributes, and so on.  To get the other functionality like observing or callbacks, just include the relevant module (e.g., &lt;code&gt;ActiveModel::Observing&lt;/code&gt;) and implement the required methods.  It’s fantastically clever.&lt;/p&gt;

&lt;h2&gt;Other pieces&lt;/h2&gt;

&lt;p&gt;ActionMailer is also getting some love in Rails 3.  A new API pointed out by DHH in &lt;a href="http://gist.github.com/281420" target="_blank"&gt;this gist&lt;/a&gt; is looking especially delicious; it’s much more like a controller with some excellent helpers mixed in just for mailing.&lt;/p&gt;

&lt;p&gt;Rails is also getting a rather robust &lt;a href="http://en.wikipedia.org/wiki/Instrumentation_(computer_programming)" target="_blank"&gt;instrumentation framework&lt;/a&gt;.  In essence, an instrumentation framework lets you subscribe to events inside of a system and respond to them in meaningful ways (e.g., an action renders and the logger logs its result).  Internally the framework is used for things like logging and debugging, but you could easily repurpose the code for other things.  For example, let’s say you want to log to the system logger when a particular e-mail is sent out:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Subscribe to the event...
ActiveSupport::Notifications.subscribe do |*args|
  @events &lt;&lt; ActiveSupport::Notifications::Event.new(*args)
end

# Fire the event...
ActiveSupport::Notifications.instrument(:system_mail, :at =&gt; Time.now) do
  #SystemMailer.important_email.deliver
  log "Important system mail sent!"
end

# Do something with it...
event = @events.first
event.name        # =&gt; :system_mail
event.payload     # =&gt; { :at =&gt; Wed Jan 16 00:51:14 -0600 2010 }
event.duration    # =&gt; 0.063
system_log(event) # =&gt; &lt;whatever&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Of course, this is arbitrary, but it adds a really powerful way to respond to certain events in your application.  For example, someone could probably rewrite &lt;code&gt;exception_notification&lt;/code&gt; to use the instrumentation framework to handle and send error e-mails.&lt;/p&gt;

&lt;h2&gt;Getting started&lt;/h2&gt;

&lt;p&gt;So, how does one get a piece of this sweet, sweet Rails 3 action?  You can install the old pre-release gems, but they’re pretty out of date.  To get rolling on edge, I’ve found two ways that work well.  First, you can use &lt;a href="http://yehudakatz.com/2009/12/31/spinning-up-a-new-rails-app/" target="_blank"&gt;Yehuda’s directions&lt;/a&gt;; these worked great for me and they’re not a lot of hassle (and how awesome is the bundler?).  If that seems a bit much or you want to automate it, Bryan Goines has made a &lt;a href="http://github.com/bry4n/rails3-install" target="_blank"&gt;pretty awesome script&lt;/a&gt; to handle installing and bundling all you need to make it work.&lt;/p&gt;

&lt;p&gt;So, go ahead, install Rails 3, get setup, and I’ll meet you on the other side.  I’ll be dropping another post this week about how to get started upgrading an existing application/plugin to Rails 3.&lt;/p&gt;

&lt;h2&gt;Posts in this series&lt;/h2&gt;

&lt;p&gt;I’m posting a whole series on Rails 3; be sure to catch these other posts!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;&lt;a href="http://omgbloglol.com/post/353978923/the-path-to-rails-3-approaching-the-upgrade" target="_blank"&gt;Approaching the Upgrade&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</description><link>http://omgbloglol.com/post/344792822</link><guid>http://omgbloglol.com/post/344792822</guid><pubDate>Wed, 20 Jan 2010 16:23:00 -0500</pubDate><category>rails3</category></item></channel></rss>
