Hoopla!

now with extra whiz-bang!

Hoopla!

Adventures in patch-land

August 09, 2007 · 2 comments

Dr. Nic threw down the guantlet about submitting a patch to the scope_out plugin to allow the use of the ActiveRecord::Base#count() method within a scope.

I’m just not sure why it hadn’t occurred to me to submit a patch before extending the plugin. Perhaps I was trying to show off my Ruby-fu, perhaps I didn’t realize GoogleCode allowed patch submissions, or perhaps I’m just trying to artificially raise my post count by doing them separately.

Patching a plugin is dead easy. Knowing which tools to use is about the only tough part.

  1. Check out the plugin (svn checkout http://scope-out-rails.googlecode.com/svn/trunk/ scope-out-rails)
  2. in your shell change the current working directory to the root of the plugin (cd scope-out-rails)
  3. Edit the file that you want changed. For this job it’s ./lib/scope_out.rb
  4. type ‘svn diff’ and take a look at what shows up. Does that look about right?
  5. type ‘svn diff > my_clearly_named_patch.diff’ to create a patch file from your changes.
  6. upload your patch to the project (http://code.google.com/p/scope-out-rails/issues/detail?id=10)
It’s far less work to make a patch than work around the problem:

Index: lib/scope_out.rb
===================================================================
--- lib/scope_out.rb    (revision 29)
+++ lib/scope_out.rb    (working copy)
@@ -112,6 +112,10 @@
         def find_#{name}(*args)
           with_#{name} {find(*args)}
         end
+        
+        def count_#{name}(*args)
+          with_#{name} {count(*args)}
+        end

         def calculate_#{name}(*args)
           with_#{name} {calculate(*args)}

Update: For shame! Here I go spewing out a patch without any tests! Many thanks to Duncan for pointing this out.

The new patch (uploaded to the scope_out project):

Index: test/scope_out_test.rb
===================================================================
--- test/scope_out_test.rb      (revision 29)
+++ test/scope_out_test.rb      (working copy)
@@ -173,6 +173,17 @@
                  Student.calculate_freshmen_active(:count, :all))
   end

+  def test_count_methods
+    assert_equal(Student.count(:conditions => "level = 'Freshman'"),
+                 Student.count_freshmen)
+    assert_equal(Student.count(:conditions => "age = 18"),
+                 Student.count_eighteen_year_olds)
+    assert_equal(Student.count(:conditions => ["active = ?", true]),
+                 Student.count_active)
+    assert_equal(Student.count(:conditions => ["active = ? and level = ?", true, 'Freshman']),
+                 Student.count_freshmen_active)
+  end
+  
   def test_passes_limit_option_to_with_scope
     assert_equal(Student.find_all_by_active(true, :limit => 1), Student.find_one_active(:all))
   end
Index: lib/scope_out.rb
===================================================================
--- lib/scope_out.rb    (revision 29)
+++ lib/scope_out.rb    (working copy)
@@ -112,6 +112,10 @@
         def find_#{name}(*args)
           with_#{name} {find(*args)}
         end
+        
+        def count_#{name}(*args)
+          with_#{name} {count(*args)}
+        end

         def calculate_#{name}(*args)
           with_#{name} {calculate(*args)}

→ 2 comments Tags:

Simply_Helpful with nested routes hack

February 27, 2007 · 0 comments

If you've played around with Simply Helpful you've probably enjoyed just how helpful it is. That is, until you've needed to use it for nested routes. As of this post Simply Helpful is in total denial of nested routes. The form_for helper just trips over itself and sprawls across your datacenter floor if you try to use it with two objects.

I enjoy the approach that this patch takes but it didn't get committed and the code keeps on changing. Rather than attempt to chase the code with a complicated patch I've created a simple hack. Modifying a single line allows me to use SH in my nested-route-loving code.

form_helper_extensions.rb, line 21


#url         = SimplyHelpful::PolymorphicRoutes.polymorphic_url(object, self)
url         = options[:url] || SimplyHelpful::PolymorphicRoutes.polymorphic_url(object, self)

Wow. I know, it's mind-blowing. Just pass an explicit url to form for and you're all set.


<%  form_for @item, :url => item_path(@project, @item) do |f| %>
<%  end %>

→ 0 comments Tags: