Vim Tips and Tricks

The following tips and tricks can be put into your .vimrc file

Remap jj to esc

imap jj <Esc>

Quickly leave edit mode and back to visual mode with jj. This is probably the fastest key combination possible and one of the most frequent key combinations while editing in vim.

Remap semicolon “;”

nnoremap ; :

Instead of shift, semicolon (which produces a colon) just remap the semicolon to a colon while in visual mode.

You can alias commands with vim

command Tab, :tabnew

The only caveat is that your alias must start with a capital letter.

How to Upgrade RVM on Mac OS X

I had an old version of rvm installed and wanted to upgrade. So old in fact that the resource for upgrading no longer existed.

rvm update

just returned a 301, redirect.

Luckily, the following worked

# checks out from repo
rvm update --head
# will reload rvm environment
rvm reload
# finally, the upgrade command works!
rvm get latest

Ruby Reload! Method in Non Rails IRB Sessions

I love the Rails reload! function when in the console. I need it in Irb. To get it back this is what I did. If you don’t already have an .irbrc file in your home directory, just create it.

vim ~/.irbrc
or textmate if you prefer
mate ~/.irbrc

Add this little snippet to it…

unless defined?(reload!)
  $files = []
  def load!(file)
    $files << file
    load file
  end
  def reload!
    $files.each { |f| load f }
  end
end

Usage : To load files in irb just use the method we defined in the .irbrc file “load!” notice the bang “!”. I don’t want to overwrite the actual load method. This load! method will just put the file in an array before loading it, so when running reload! it will iterate over this collection and load them again with whatever changes have since taken place. The unless conditional is so that you don’t overwrite the reload! method if you’re actually in the rails console.

How to Recover a Mistakenly Deleted Branch

Workflow

git checkout -b _new_branch_name
# do some work and commit changed
git checkout master
git branch -d _new_branch_name
# doh... i meant to merge first

Fortunately, you can easily recover from this mistake.

git reflog
395b1ea HEAD@{0}: checkout: moving from _master_cleanup_akismet to _master_cleanup
bd7df04 HEAD@{1}: commit: spam handling using akismet for form submissions on contact_submission and applicant models
395b1ea HEAD@{2}: checkout: moving from _master_cleanup to _master_cleanup_akismet
395b1ea HEAD@{3}: commit: cleaning up and adding some basic features
a828ef3 HEAD@{4}: checkout: moving from master to _master_cleanup

Should show you a list of commits across all branches in desc chronological order.

To merge the branch you just deleted you can give merge the sha.

git merge bd7df04

Which would match the commit at HEAD@{1}

Aliasing Attributes in Ruby on Rails

Alias an attribute with alias_attribute method.

The first argument is the name for the new attribute, while the second argument is to identify the attribute that is already defined.

class User < ActiveRecord::Base
  alias_attribute :username, :login
end

Rails 3 Config Auto Load Paths in Application.rb

In Rails 3 files in lib/ are no longer loaded by default. It’s a snap to auto load these classes by adding the following line to config/application.rb

    config.autoload_paths += %W(#{config.root}/lib)

This is commented out around line 16. Either replace it or uncomment it out to reflect the location where your lib classes are located.

How To Get A List of All Registered Mime Types in Rails

When mime types are registered they are placed in a hash constant EXTENSION_LOOKUP in the module Mime. For reference, the file with the relevant code is in rails/action_pack/lib/action_dispatch/http/mime_type.rb available on Github at https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/mime_type.rb

You can check for existence of certain mime types with

Mime::Type.lookup_by_extension(:json)

which will return the object or nil if it’s not registered.
You can also iterate over all registered mime types by cycling through the constant like so

Mime::EXTENSION_LOOKUP.each { |m| puts m}

Api documentation is here: http://api.rubyonrails.org/classes/Mime/Type.html#method-c-lookup_by_extension

Git Feature Branch Naming Strategy

There are only two hard things in Computer Science: cache invalidation and naming things.
– Phil Karlton

Typically, we have three main branches at any given time in a project lifecycle.

master, development and staging.

Master is production ready code, development is actively being worked on and staging is pre-flight testing before deploying master.

Features are branched off of development and use underscores to indicate distance from originating branch. For instance

_development_users
__development_users_reset_password

indicates that __development_users_reset_password branched off of _development_users branch.

___development_users_reset_password_experiment

(3 underscores away) would indicate that I was experimenting with a branch off of development users reset password branch.

This provides a nice visual hierarchy when running git branch from the command line. It’s also helpful when you have multiple features being worked on by several people and you want to know where these branches are in relation to each other without needing to ask anyone. Here is a more complex example that illustrates the usefulness in using underscores in naming feature branches

  __development_people_importing_csv
* __development_people_importing_xml
  __development_users_password_reset
  _development_people_importing
  _development_users
  development
  master
  staging

My First Ruby Gem: Hashed_Attributes

I just wrote and released my first Ruby Gem, Hashed Attributes https://rubygems.org/gems/hashed_attributes. It is a very simple ActiveRecord extension for saving variables through a serialized hash. The Gem will let you declare getter and setter methods that use a hash to store assigned data. For instance, instead of doing something like

@user.preferences[:favorite_color] = 'orange'

you can do

@user.favorite_color = 'orange'

The value for favorite color will be kept in a serialized hash.

  person = Person.new(:favorite_color=>"orange")
  {
               :id => nil,
      :preferences => {
          :favorite_color => "orange",
      },
       :created_at => nil,
       :updated_at => nil
  }

Setup in the model is very straight forward. The first argument is the column name you want to use followed by a list of methods used as keys on the hash.

# Example model
class Person < ActiveRecord::Base
  hashed_attributes :preferences, :favorite_color, :theme, :plan # etc...
end

ActiveRecord is required to use this gem. Additionally, you need to add a column in your database.

# Sample migration
create_table :people do |t|
   t.text :preferences
end

To install

gem install hashed_attributes

Or place in your Rails Gemfile

gem 'hashed_attributes'

The project code is hosted on Github: https://github.com/bseanvt/hashed_attributes and https://rubygems.org/gems/hashed_attributes

Releasing this gem was a lot of fun and I’m looking forward to writing more gems!

Rails Send_File in Production Delivers an Empty File

If you’re running Rails in production it will by default be configured to let apache or nginx send files for you. If you’re handling file downloads yourself with send_file

send_file("path/to/file.txt")

you will notice that the downloaded files are empty. To get around this just comment out the following in your config/environments/production.rb file.

  # comment out for production because apache/nginx are not doing this for us
  #config.action_dispatch.x_sendfile_header = "X-Sendfile"