articles tagged with gems

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

Installing your own Ruby Gems on Dreamhost

no comments yet, post one now

I came across this problem when trying to setup Capistrano on my dreamhost box.

Capistrano (orginally SwitchTower); is a standalone deployment utility that can integrate nicely with Rails. It allows you to deploy your apps across multiple servers from a subversion; Its handy for any shared environment (such as dreamhost), since you can use it to migrate databases and reset running fcgi processes.

Checking the Dreamhost Gemlist (or; gem list —local) I found that Capistrano wasnt installed – so I had to go about setting up my shared box so I could install any Gem I liked, in my home directory.

Its easier than I thought, but I had some trouble searching Google to find an answer so Im posting it up here; share the knowlegde and all that …

First up, create a new .gems folder in your home directory;

mkdir ~/.gems

Next open up your ~/.bashrc and ~/.bash_profile files and make sure to add the following lines as new environment variables;

export GEM_HOME=$HOME/.gems
export GEM_PATH=/usr/lib/ruby/gems/1.8:$GEM_HOME

Also adjust your PATH variable to include your new ~/.gems folder;

export PATH=~/bin:~/.gems/bin:$PATH

Thats basically it ! – For any gems you want to install you’ll need to grab them from somewhere online; I picked up Capistrano from here with

wget http://rubyforge.org/frs/?group_id=1420&release_id=4528

Then ran this command to install the gem (from my home dir)

gem install ~/capistrano-1.1.0.gem

Since we added the ~/.gems folder to the PATH variable in your bash files, you can simply type cap -V to check capistrano is installed.

Although this is not nessecary for Capistrano – in order to get your rails app to use other gems installed in your home directory, you first have to unpack them in RAILS_ROOT/vendor. Therefore, to be able to require them in your code, enter the RAILS_ROOT/vendor directory and do the following:

gem unpack gem_name
← (k) prev | next (j) →