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:

Amazon S3 Backup via Rails Plugin

January 02, 2007 · 9 comments

I think Amazon S3 is awesome. I was looking into building a RAID NAS (Network-Attached Storage) for backing up all my important data and I nearly bought a setup that would run into the hundreds of dollars - but then I did a little fancy multiplication and addition and realized S3 would cost me less than one one-hundredth what the NAT would have cost.

In case you’re as in the dark about S3 as I recently was, here’s a little rundown: it’s a very simple, super fast, extremely large backup system that Amazon uses for all of it’s own storage needs. It’s opened the service up to the public on a pay-as-you-go basis.

Costs:

  • $0.15 per GB-Month of storage used
  • $0.20 per GB of data transferred

In other words, it’s damn cheap. Say you want to upload 500 MS Word documents that are around 45KB each? How much would it cost to store those on some easy-to-access, highly secure, permanent backup place? Less than one cent per month. Eight years later you’d only be out a half-dollar ($0.32 to be precise).

So S3 is my new storage/backup location of choice. The one difficulty of using it is that I need to figure out some way of automating the backups so that the backups are actually useful and can easily be recovered if necessary. In particular, I need some way to automatically backup the website data that is so crucial to me.

So I made a plugin.

The S3 plugin will allow you to backup your crucial website data to S3 via a handy Rake task (written by the talented Adam Greene).

Amazon has been an excellent supporter of Ruby/Rails lately (they fund 43things.com among other things) and they’ve made sure to release a ruby library for S3. I’ve combined that with Adam’s S3 rake task into a handy S3 backup plugin.

You can install it via the following two commands:

1
2
ruby script/plugin source http://svn.6brand.com/projects/plugins
ruby script/plugin install -x s3

Then backing up is easy as:

1
2
3
rake s3:backup:db
rake s3:backup:code
rake s3:backup:scm

or, to get them all together:

1
rake s3:backup

→ 9 comments Tags: