Learnings for Week 33 2010

  • If you want to test that that an exception is raised and then test the message that is easily accomplished with assert_raise:
exception = assert_raise ArgumentError do
  #something
end
assert_match /something/, exception.message
  • If you’re working on an older Rails project (pre Rails 2.1) you will be missing config.gem for Gem dependency management. If you have multiple versions of gems on the machine you can still load a specific version by using rubygems:
require 'rubygems'
gem 'RedCloth', '~>3.0'
  • Rails doesn’t check authenticity tokens on GET requests and doesn’t ever check them in the functional tests. More info on Rails authenticity token here.
September 11, 2010 | View Comments

Kernel Panic, TimeMachine to the Rescue!

A couple weeks ago I sat down at my computer after dinner to do a little bit of work and was greeted with a very, very sluggish machine. This isn’t normal with my Macbook so I tried shutting down a few applications and pull up Activity Monitor to see what was chewing up the cycles. This seemed to be too much for the machine and it completely locked up, what next? Well, the only thing I could do, hold down the power button until it shut off and try turning it back on (did you try turning it off and on again?).

At this point I wasn’t too worried, and sat waiting for OS X to load. Waiting. And Waiting. And Waiting. It just sat there spinning away. This is quite the dilemma. I hopped onto my iPhone and Goggled to see if there was a way to start up to a command prompt. I found this link which had a bunch of information, including holding command-s to boot to single user mode.

Off and On again, this time booting to single user mode and the command prompt. The same link mentioned running ‘/sbin/fsck’ (file system check) to fix any problems with the file system. I ran it. A billion errors, half a billion couldn’t be fixed. Great… Well, it says you may need to run it multiple times to fix everything, so I run it again. And again. And again. Shit.

Maybe I should try reseting the PRAM like this link says. Off and On again. Kernel Panic. Off and On. Kernel Panic. Shit. (Kernel Panic is the OS X equivalent to a Windows Blue Screen. They are rare so it is likely you have never seen one, but you can check it out here)

Well, now I’m screwed. At least I have Apple Care, into the Mac shop it goes.

“Did you try turning it Off and On again?”, she asks.
“Yep. I tried running the file system check, I tried reseting the PRAM and I turned it Off and On a dozen times,” I told her.
“Well, I’ll just try reseting the PRAM,” she said. “Oh, ya, Kernel Panic. That’s not good. We’ll take a look and let you know what we can do.”

Later that day, the Mac Store calls. “You need a new hard drive, we’ll be sending your old one to Apple and probably get a new one in 3-5 business days.”
“You don’t have them in stock?” I asked.
“No, we don’t stock the brand Apple uses and to keep it covered by the warranty we have to use one of theirs,” the girl from The Mac Store says.

Four days later and I’m bringing my Macbook home with it’s fresh new hard drive. I pop in my Snow Leopard disk and watch some TV while OS X reinstalls. Once it finishes I’m asked if I want to import user settings or restore from a Time Machine backup. Hell Yes, I’ll restore from the Time Machine backups I started doing ONE WEEK EARLIER! A couple hours later and I could barely remember that my hard drive had failed and I had a new one in my machine. All my Applications were restored. All my settings. Heck, even my desktop wallpaper came back.

The only thing that I had to reinstall was parallels, and that’s because I ignored the folder where the Windows VM was saved (it seems like it wouldn’t have worked from a restore anyways). The moral of the story is spend $60 on a 500 gig hard drive and USE Time Machine. I’ve ignored a number of places on my machine to keep the backup a little smaller and taking less time, you can do the same (with my setup it is PROVEN that you can still restore and get everything back):

~/.Trash
/.Trashes
/Library/Audio
~/Library/Caches
/Library/Caches
~/Downloads
~/Movies
~/Documents/Parallels
~/Music/iTunes/iTunes Music/Podcasts

Currently I have 20 days of backups and 300 gigs remaining on the drive. Do it.

I have to note that my little dialog with the girl at the Mac shop was a little contrived. They were actually really helpful and did a great job bringing my Macbook (and me) back to life.

September 02, 2010 | View Comments

Learnings for Week 31 2010

Here’s my learnings from a couple weeks ago.

  • Homebrew is a package manager for OS X. Packages are defined by formulas that are simple Ruby scripts which is stored in Git underneath.
recipe.to_json(:except=>:photo)
August 24, 2010 | View Comments

Learnings for Week 30 2010

Well, I’ve been slacking again, here’s the learnings from a couple weeks ago.

  • IE must “haveLayout for opacity”:http://joseph.randomnetworks.com/archives/2006/08/16/css-opacity-in-internet-explorer-ie/ (alpha filter) to work. You can use the IE zoom rule to give an element layout if needed:
zoom: 1;
  • To add custom Date/Time formats in Rails:
Time::DATE_FORMATS.merge!(:article => "%A, %B %d, %Y")
Date::DATE_FORMATS.merge!(:article => "%A, %B %d, %Y")

Don’t forget to add your custom formats to Date as well, they use a different hash. This can then be used like so:

Date.today.to_s(:article)
  • When using Paypal buttons you can change the Currency that is used by “adding a hidden field”:https://www.paypal.com/us/cgi-bin/webscr?cmd=p/sell/mc/mc_wa-outside:
<input type="hidden" name="currency_code" value="CAD">
  • The Rails Request object has a number of useful methods for “finding the url that was used for the request”:http://programming-tut.blogspot.com/2010/06/ruby-on-rails-request-url.html:
request.host_with_port => example.com:3000
request.path           => /controller/action
request.url            => http://example.com:3000/controller/action
August 17, 2010 | View Comments

Learnings for Week 29 2010

This past week I had learnings all over the place; javascript, rails, oracle, css and probably more that I’ve already forgotten.

  • Interesting post by Remy Sharp on “Throttling Javascript Function Calls”:http://remysharp.com/2010/07/21/throttling-function-calls/
function throttle(fn, delay) {
  var timer = null;
  return function () {
    var context = this, args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function () {
      fn.apply(context, args);
    }, delay);
  };
}

Usage:

$('input.username').keypress(throttle(function (event) {
  // do the Ajax request
}, 250));

This will require there to be 250ms between a keypress before it will execute the given function. Useful for things like Autocomplete where you don’t want an AJAX request to fire until the person stops typing.

  • To access the Host machine from a VirtualBox Windows VM ‘\\vboxsvr\share’ . You can also turn on Windows filesharing and access the VM from the Host.
  • When Rails (as of 2.3.5) runs the ‘environment’ Rake task it sets the global variable $rails_rake_task = true. Since most tasks depend on the environment you can use this to do slightly different behavior if your code is running within a Rake task.
  • Oracle DB treats blank strings in VARCHAR/VARCHAR2 columns as NULL

  • IE has a property ‘hasLayout’ that determines “how to draw and bound elements”:http://www.satzansatz.de/cssd/onhavinglayout.html. Some elements “have layout by default”:http://www.satzansatz.de/cssd/onhavinglayout.html#wherefrom and others do not. Sometimes when an element does not have layout you can see odd behavior, like lists being numbered incorrectly or margins collapsing. To fix this you can “add certain css properties”:http://www.satzansatz.de/cssd/onhavinglayout.html#prop to elements so that they will gain layout.

.element {
  height:1%;
}

Height is a property that will have an element gain layout but sometimes you might still want the element to auto expand. Setting the height to 1% will give the element layout as well as allowing it to expand with its content. This is known as the Holly Hack. **Note: hasLayout may not be an issue anymore as of IE8.

August 01, 2010 | View Comments

Learnings for Week 28 2010

  • SQLServer compatibility mode doesn’t make the server behave exactly what is specified, rather, it pulls in any functionality that may have been deprecated in the later releases (I think)
  • SendGrid, Google Apps, AuthSMTP are paid services that can be used for sending email so you don’t need to do it through your own server (or if you’re on EC2 and can’t reliably send mail from your server)
July 29, 2010 | View Comments

Learnings for Week 27 2010

Late again, I know, but here’s my learnings from a couple weeks ago.

  • If you need to use the Rails url helpers within a model or even script/console you just need these two lines:
include ActionController::UrlWriter
default_url_options[:host] = DEFAULT_HOST
  • Alternatively within script/console you can use the “‘app’ or ‘helper’ variables to interact with the application”:http://stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-rails
helper.number_to_currency('2342.33')
app.get '/'
  • If you want some piece of code to run after each request you can use a ‘config.to_prepare’ block. This might be useful if you have classes reloading in development and want to set settings after they have reloaded.

  • Git can sometimes consider text files to be binary if they have Null characters at the end. You can still diff these files with this command:

git diff -a
  • Facebook applications must be created on a Personal Account, they can not be created with a Business Account.
July 25, 2010 | View Comments

Learnings for Week 26 2010

  • Git interactive rebase can reorder commits (just reorder them in the file)
  • “No such device eth0” after cloning VMWare Image
    • run ‘sudo mv /etc/udev/rules.d/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules.old’ and then restart the machine to fix the problem. Found here.
  • Any word completion with vim
    • type some characters then ctrl-N or ctrl-P (next or previous match)
  • EngineYard App Cloud makes a lot of mundane server admin tasks very simple and still allowing complete customization with chef scripts or with root access on the server.
  • Many EC2 IPs are on Spam blacklists. Use a thirdparty such as gmail to do the emailing for you.
July 13, 2010 | View Comments

Learnings for Week 25 2010

  • Checking if a javascript function exists in the page:
    • typeof functionName == ‘function’
  • Tig a simple command-line yet visual interface to Git
  • Filename completion in Vim with ctrl-x then ctrl-f
  • Had issues with parts of the page not redrawing correctly, the culprit was a position: relative - not really sure why.
  • jQuery delegate method is the ‘new’ way to do live events, cleaner syntax and promotes using a context so the events don’t have to bubble all the way up the DOM. Learning JQuery has a pretty good explanation.
July 07, 2010 | View Comments

Learnings for Week 24 2010

  • Latest Rails 3 beta doesn’t work in ruby 1.9.1, as stated in the release notes
  • Mongrel doesn’t build in Ruby 1.9.2.preview3 on Snow Leopard (for me at least)
  • Factory Girl factories don’t autoload in Rails 3 anymore, do it yourself or use factory_girl_rails gem
  • Colored grep: grep —color=auto
  • Vim visual block I ctrl-r-+ to insert copied text
  • IRB.conf[:SAVE_HISTORY] = 100
  • :set spell in Vim to turn on spell checking
  • Ruby do…while loop begin; code; end while(true)
July 04, 2010 | View Comments