14 Jun 2012

Rails 2.3.14 on Ruby 1.9.3

Rails 2 hasn’t been maintained for quite a while, and it last officially supported Ruby 1.9.1. I was working on getting an old Rails 2 app upgraded to Rails 3 and discovered something frustrating: Rails 2.3.14 don’t boot under Ruby 1.9.3 unless every single controller has a helper class defined in a helper file. I didn’t want to sit around creating 60 helper files, so I did the expedient thing, patching the app’s boot.rb file to not raise exceptions on missing helper files.

It turned out to not be a terribly large amount of code, but here’s a diff just in case anyone else is upgrading a really old Rails app and runs into the same issue:

diff --git a/config/boot.rb b/config/boot.rb
index 69b1a51..2c979cf 100644
--- a/config/boot.rb
+++ b/config/boot.rb
@@ -61,12 +61,30 @@ module Rails
       require 'initializer'
     end
 
+    def monkeypatch_helpers
+      require "active_support"
+      require 'action_controller/helpers'
+      ActionController::Helpers::ClassMethods.send(:define_method, :inherited_with_helper) do |child|
+        inherited_without_helper(child)
+
+        begin
+          child.master_helper_module = Module.new
+          child.master_helper_module.__send__ :include, master_helper_module
+          child.__send__ :default_helper_module!
+        rescue MissingSourceFile => e
+          raise unless e.is_missing?("helpers/#{child.controller_path}_helper")
+        rescue LoadError
+        end
+      end
+    end
+
     def load_rails_gem
       if version = self.class.gem_version
         gem 'rails', version
       else
         gem 'rails'
       end
+      monkeypatch_helpers
     rescue Gem::LoadError => load_error
       $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
       exit 1

I’ve also posted it as a gist if that’s more up your alley.