Rails 3 bash aliases and .irbrc configs

13 comments

I’ve been upgrading some apps and gems to the latest and greatest Rails 3. I’ve taken the time to update my ~/.bash_aliases and ~/.irbrc files to be both Rails 3 and Rails 2.x compatible. See below for the code.

~/.bash_aliases

# rails 3 shortcut 'r'
alias r='rails'

# launching console/server
sc () {
  if [ -f ./script/rails ]; then 
    rails c $@
  else
    ./script/console $@
  fi
}

sg () {
  if [ -f ./script/rails ]; then
    rails g $@
  else
    ./script/generate $@
  fi
}

ss () {
  if [ -f ./script/rails ]; then 
    rails s $@
  else
    ./script/server $@
  fi
}

sspe () {
  if [ -f ./script/rails ]; then 
    sudo rails s -p80 $@
  else
    sudo ./script/server -p80 $@
  fi
}

# database migrate
alias rdbm='rake db:migrate'

# tests
alias rspec='rake spec'

# rails logs, tailing and cleaning
alias tdl='tail -f ./log/development.log'
alias ttl='tail -f ./log/test.log'
alias ctl='> ./log/test.log'
alias cdl='> ./log/development.log'

~/.irbrc

require 'rubygems' rescue nil
require 'wirble'
require 'hirb'
require 'ap'

# load wirble
Wirble.init
Wirble.colorize

# load hirb
Hirb::View.enable

IRB.conf[:AUTO_INDENT] = true

if ENV.include?('RAILS_ENV')
  if !Object.const_defined?('RAILS_DEFAULT_LOGGER')
    require 'logger'
    Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))
  end

  def sql(query)
    ActiveRecord::Base.connection.select_all(query)
  end
  
  if ENV['RAILS_ENV'] == 'test'
    require 'test/test_helper'
  end

# for rails 3
elsif defined?(Rails) && !Rails.env.nil?
  if Rails.logger
    Rails.logger =Logger.new(STDOUT)
    ActiveRecord::Base.logger = Rails.logger
  end
  if Rails.env == 'test'
    require 'test/test_helper'
  end
else
  # nothing to do
end

# annotate column names of an AR model
def show(obj)
  y(obj.send("column_names"))
end

puts "> all systems are go wirble/hirb/ap/show <"

Note that if your’e using Bundler in your Rails app, AND use gems in your ~/.irbrc file AND attempt to start the Rails console; you’ll get errors/warnings on requiring them UNLESS you define them in your Gemfile. I use a ‘development’ group in my Gemfile for these, like so.

Gemfile

group :development do
  gem "wirble"
  gem "hirb"
  gem "awesome_print"
end

platforms :ruby_18 do
  gem 'ruby-debug'
end 

platforms :ruby_19 do
  gem 'ruby-debug19'
end
September 19, 2010 18:33 by

13 comments so far

  • photo of Daniel Kristensen Daniel Kristensen Sep 23, 2010

    That’s some great tweaks :-D Thanks

  • photo of Heiko Seebach Heiko Seebach Sep 23, 2010

    Hi,

    if you start irb without Rails environment, it crashes at

    “elsif !Rails.env.nil?”

    because Rails is not defined.

    You could change it like this: elsif defined?(Rails) and !Rails.env.nil? if Rails.logger Rails.logger =Logger.new(STDOUT) ActiveRecord::Base.logger = Rails.logger end if Rails.env == ‘test’ require ‘test/test_helper’ end else

    1. nothing to do end

    cu, Heiko

  • photo of matt matt Sep 23, 2010

    Thanks for the heads up Heiko! I’ve updated the post and the gist with this fix.

  • photo of Stephen Touset Stephen Touset Sep 23, 2010

    The line `gem RUBY_VERSION.include?(‘1.9’) ? ‘ruby-debug19’ : ‘ruby-debug’` in your Gemfile is fundamentally broken. It will cause the Gemfile.lock to be vary upon the architecture `bundle install` is run on, and this should not happen, since the Gemfile.lock should be committed to your repository.

    Instead, use the “platform” block provided in Bundler.

  • photo of Matt Matt Sep 23, 2010

    Thanks Stephen (again) i’ve updated the post with the fix

  • photo of Brooke Kuhlmann Brooke Kuhlmann Sep 23, 2010

    Nice, write up, thanks. I’ve incorporated some of your ideas in my own .bash_profile file via my Mac OS Setup (http://github.com/aeonscope/macos_setup) project. You might find some of my settings to your liking as well. BTW, you might want to change the following alias commands since they clobber native Mac OS binaries of the same name: <ol> <li>rs</li> <li>dbm</li>

    </ol>
  • photo of Matt Matt Sep 24, 2010

    Nice one Brooke, I’ve cobbled together something like this on my for setting up my own mac development machines, only using bash and git.

    I’ve been meaning to standardize it more, and would consider creating a brew for it. I check out your git repo for some ideas.

  • photo of Mark Wilkinson Mark Wilkinson Sep 24, 2010

    I might be missing something, but it looks like the functions in your .bash_aliases file are using zsh syntax to pass the arguments through ($argv). For bash I think you should be using “$@”.

  • photo of matt matt Sep 28, 2010

    Thanks Mark, well spotted, I’ve updated that now to the $@ format instead.

  • photo of lucapette lucapette Nov 13, 2010

    Your gem development set is exactly what i was looking for.

Leave a comment