Insights on Ruby, Git, jQuery, Cappuccino, WordPress, Debian and OS X. Please subscribe if you find something useful!

Amending Git Commits

Posted: August 16th, 2009 | Author: Jerod | Filed under: Git | Tags: | Comments

I recently learned that you can fix you’re previous commit (modify commit message, add more files, etc.) quite easily with git. An example:

You’re in early stages of developing a Rails app and you decide that you want to go back and add some indexes to your tables. No need to create a new migration at this point, just add the indexes to the old migrations and run them again. After making the changes, you create a commit

git commit -a -m "Added missing indexes to tables"

Next you re-run all your migrations to get the indexes in there.

rake db:migrate:reset

At this point, you check git status and remember that now your schema file has changed. This probably should have been included in the last commit! Piece of cake.

git commit db/schema.rb --amend

You’ll be prompted to optionally change the commit message.

At this point git status will tell you that your working directory is clean and the changes to your schema were tracked in the same commit as the migration changes.

Butter.


jQuery – Open External Links In New Window/Tab

Posted: May 18th, 2009 | Author: Jerod | Filed under: jQuery | Tags: | Comments

The common technique to force links to open a new window or tab is to add a “target” attribute like so:

<a href="http://hulu.com" target="_blank">check it out</a>

This works just fine but is not actually valid markup. Fortunately, we can use jQuery to add the target attribute so it doesn’t muck up the HTML.

$(document).ready(function() {
  $('a[rel="external"]').click(function(){
    $(this).attr('target','_blank');
  });
});

Change that invalid link to look like this instead:

<a href="http://hulu.com" rel="external">check it out</a>

Now the link forces browsers to open a new window/tab and the markup is still valid. Easy cheesy.


jQuery – Set Mouse Focus On Page Load

Posted: May 9th, 2009 | Author: Jerod | Filed under: jQuery | Tags: | Comments

First Input:

$(document).ready(function() {
  $('input:text:first').focus();
});

First Empty Input:

$(document).ready(function() {
  $('input:text[value=""]:first').focus();
});