Radio Bantik:
Days in the Life of an Alpha Geek

Radio Bantik is an alpha-geek blog covering topics in Mac OS X, software and web development including Ruby on Rails, Java, Cocoa, and WebObjects, corporate survival, LEGO robotics, and other stuff important in the life of a technologist.

Archive for the ‘Ruby on Rails’ Category

The 48 Hour Application Challenge

Posted May 20th, 2008 in Development, GenTech, Ruby on Rails | Permanent Link

I was inspired this week by a post on the Rails Google Group by Melvin at 48hourlaunch.wordpress.com, who was asking for suggestions on a software problem to tackle with 48 hours of effort.

My magic notebook...

I’ve got what I call a “magic notebook”– several, to be precise– full of app ideas in various stages of ideation. So I decided to take on this challenge as well and see what I can do with one of them.

Like Mel, I’m taking on a project that meets one of my immediate needs and is well within my area of expertise. So I’ll be creating something that is useful to me and also has a broader market appeal.

One of my defining characteristics is the inability to do things halfway; unfortunately, this can sometimes manifest as “analysis paralysis”, overengineering, etc. Although Rails inherently short-circuits a lot of these tendencies (as does my business partner, for that matter!) the strict time limit I’m putting on myself makes this something of a competition between completion and perfection. At worst, I will move one of my unrealized applications one step closer to market, and hopefully learn something about my internal process along the way.

So what’s the big project going to be? Can’t talk about that yet, but if all goes according to plan there should be something public in within the next 30 days (48 hours / 2 hours per day = 24 days). I’ll also be posting notes about my progress as I go.


File_column bug with case sensitivity

Posted March 4th, 2008 in Ruby on Rails | Permanent Link

After banging my head against a wall wondering why certain image uploads would silently fail, while others succeeded, I stumbled across a file_column bug (and yes, I reported it).

If you have the following in your model:

validates_file_format_of :image, :in => ["bmp", "gif", "jpg", "pdf", "png"]

And you upload a file with one of these extensions in a different case, for example “SAMPLE.JPG”, the file format validation fails.

Workaround (non-DRY): add case variations, like this:

validates_file_format_of :image, :in => ["bmp", "gif", "jpg", "pdf", "png", "BMP", "GIF", "JPG", "PDF", "PNG"]

Grr.

Captcha Gotcha

Posted February 23rd, 2008 in Ruby on Rails | Permanent Link

While implementing Sur Max’s excellent Simple Captcha plugin, I ran into a very frustrating issue. No matter what was entered, the valid_with_captcha? method always returned false.

After some digging I finally realized that there was a conflict with restful_authentication. The resolution is simple, luckily. You just need to add the following to your user controller:

attr_accessible :captcha, :captcha_key

That did it for me! Hopefully this will help someone else as well.

My New Favorite Rails Plug-In…

Posted February 7th, 2008 in Development, Office Adventures, Ruby on Rails | Permanent Link

From the description for Acts As Enterprisey:

Rails make life easy for us but– and it’s a big but– we don’t want it to look easy. acts_as_enterprisey is your friend. How does acts_as_enterprisey make webapp development look hard? Well, the only way your client can judge your app is by playing around with it. What better gives the feeling of heavy weights being lifted behind the scenes than slow response times? Exactly. That’s what acts_as_enterprisey does.

Simply insert acts_as_enterprisey in your ActiveRecord model [and set the delay to whatever value you want.] So while your client clicks, …waits…, and then gets the page, you can blather on heroically about wrestling with clustered indexes, cache expiration strategies, n log n seek times, etc ad nauseam.

Clients pay you to solve their technical problems. They want to feel like they’re getting their money’s worth. Especially after you start submitting enterprisey invoices.
So make their wishes come true: it’s only fair. Ethical, even. Crank the delay up as the deadline approaches, make them sweat, display fortitude and perseverance, etc. And when they can’t take it (the app’s sluggishness, your bills, whatever) any more, [remove] acts_as_enterprisey from your models and book the flights to Vegas.

This is akin to the infamous “make_it_go_faster” flag. Yay.

Using Faker to Pre-Populate a Dev Database

Posted February 4th, 2008 in Development, Ruby on Rails | Permanent Link

I’m currently working on a project to create a travel website, and I needed to have a bunch of member profiles to use for testing the member directory. I came across a great Ruby gem called Faker which I’ve used to create the dummy accounts.

Installation is easy, of course: gem install Faker

Next, I created a Rake task for populating the accounts:

namespace :db do
  namespace :development do
    desc "Create user records in the development database."

    task :fake_user_data => :environment do
      require 'faker'

      @countries = ["United States", "Canada", "United Kingdom", "Germany", "Mexico"]
      @genders = ["Male","Female"]
      @privacy = ["members", "public"]

      def randomDate(params={})
        years_back = params[:year_range] || 5
        latest_year  = params [:year_latest] || 0
        year = (rand * (years_back)).ceil + (Time.now.year - latest_year - years_back)
        month = (rand * 12).ceil
        day = (rand * 31).ceil
        series = [date = Time.local(year, month, day)]
        if params[:series]
          params[:series].each do |some_time_after|
            series << series.last + (rand * some_time_after).ceil
          end
          return series
        end
        date
      end

      100.times do

        u = User.new(
          :first_name => Faker::Name.first_name,
          :last_name => Faker::Name.last_name,
          :birthdate => randomDate(:year_range => 60, :year_latest => 22),
          :created_at => randomDate(:year_range => 4, :year_latest => 0),
          :city => Faker::Lorem.words(1).to_s.capitalize,
          :state => Faker::Address.us_state(),
          :country => @countries.rand.to_s,
          :password => “greatpasswordhuh”,
          :password_confirmation => “greatpasswordhuh”,
          :accepts_terms_and_conditions => true,
          :gender => @genders.rand.to_s,
          :email => Faker::Internet.email
        )

        u.extended_profile = ExtendedProfile.new(
          :bio => Faker::Lorem.sentences(5).join(” “),
          :occupation => Faker::Lorem.words(1).to_s.capitalize
        )

        u.preferences = Preferences.new()
        u.save!

      end
    end
  end
end

Note that while Faker was good for creating random names, e-mail addresses, etc., I needed some other data that was outside of its scope, for example gender and country. For these, I simply created an array (like @genders = ["Male","Female"]) and called @genders.rand to pick a value. I also used its Lorem Ipsum generator to create random city names, occupations, and biographical text.

Once the Rake task was written, it was simply a matter of typing rake db:development:fake_user_data to populate the database with my junk records. Pretty sweet!

You can find Faker at http://faker.rubyforge.org/.

Post-Migration Ruby Leopard Blues

Posted February 3rd, 2008 in Apple, Development, Ruby on Rails | Permanent Link

I finally finished migrating everything over from my old desktop to my new iMac. (I’m still amazed that a consumer-level Mac is more than twice as fast as my old high-end desktop… such is the pace of hardware, of course.)

At any rate, when I started working on one of my Rails projects, I was surprised to see that performance was really poor– much slower that on the old box. Then I realized that my Ruby and Rails installations probably got migrated by Migration Assistant.

Sure enough, ruby -v in Terminal revealed Ruby 1.8.6 for PowerPC/Darwin. This means that Ruby is running in PPC emulation, which explains the performance I was seeing.

Since Leopard ships with Ruby, this means that I now have two versions: in /usr/bin/ruby there’s the version that came with the OS, which is

ruby 1.8.6 (2007-06-07 patchlevel 36) [universal-darwin9.0]

In /usr/local/bin/ruby there’s:

ruby 1.8.6 (2007-03-13 patchlevel 0) [powerpc-darwin8.10.0].

This is obviously a problem…

Some Google time later, I came across this post on Punctuated Productivity; that set me in the right direction for uninstalling the old version and getting the new one up to snuff. His approach was to completely wipe /usr/local/bin and /opt/, which I found a little scary. Here’s a slightly more surgical approach.

  1. Remove the following from /usr/local/bin (sudo, of course):

    gem*
    update_rubygems
    rails
    rake
    mongrel-rails
    cap
    capify
    gpgen
    index_gem_repository.rb
    libpng-config

  2. Unfortunately, I had just installed and configured ImageMagick on the old box, which is a long and tricky process that I’ll have to do all over again. But since it was the only thing that I had installed on that day, so I was able to open ./ to open /usr/local/bin in the Finder, sort by date, and select/delete all of the ImageMagick and RMagick files in a couple of clicks:

    Magick-config
    Wand-config
    animate
    compare
    composite
    conjure
    convert
    display
    freetype-config
    identify
    import
    libpng12-config
    libwmf-config
    libwmf-fontmap
    mogrify
    montage
    pkg-config
    stream
    wmf2eps
    wmf2fig
    wmf2gd
    wmf2svg
    wmf2x
    libpng-config

  3. My last uninstall step was to delete /opt after all, because only MacPorts (which I used to install ImageMagick) had put anything in there.
  4. Now hash -r followed by which ruby correctly reveals /usr/bin/ruby, and ruby -v reports ruby 1.8.6 (2007-06-07 patchlevel 36) [universal-darwin9.0]

    Now to reinstall Rails:

    sudo gem install rails --include-dependencies

    Next I have to reinstall ImageMagick and RMagick, which will hopefully be less painful this time around now that I know about MacPorts…