As part of getting my SuppressValidations gem to load automatically when launching
script/console I took some time to configure my .irbrc file a bit and I thought I’d share it.
So here it is in all it’s glory:
require 'rubygems'
require 'irb/completion'
require 'irb/ext/save-history'
# Tell irb how much it should remember and where
# to save it's history
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
# Simple prompt
IRB.conf[:PROMPT_MODE] = :SIMPLE
# Auto indent code
IRB.conf[:AUTO_INDENT] = true
# This is only done when using the Rails console
if rails_env = ENV['RAILS_ENV']
# Setup a Rails specific prompt
rails_root = File.basename(Dir.pwd)
IRB.conf[:PROMPT] ||= {}
IRB.conf[:PROMPT][:RAILS] = {
ROMPT_I => "#{rails_root}> ",
ROMPT_S => "#{rails_root}* ",
ROMPT_C => "#{rails_root}? ",
:RETURN => "=> %s\n"
}
IRB.conf[:PROMPT_MODE] = :RAILS
# This is only done when the irb session and
# rails are fully loaded (from Mike Clark)
IRB.conf[:IRB_RC] = Proc.new do
# Log ActiveRecord calls to standard out
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Setup a [] alias for find so you can say:
# Post[1] instead of Post.find(1)
ActiveRecord::Base.instance_eval { alias :[] :find }
# Require and mixin the SuppressValidations gem
# (gem install polly-suppress_validations)
require "suppress_validations"
include SuppressValidations
end
end
And here’s a sample session:
$ script/console
Loading development environment (Rails 2.3.2)
test> User[1]
User Load (0.2ms) SELECT * FROM "users" WHERE ("users"."id" = 1)
=> #
test> User[1].save
User Load (0.4ms) SELECT * FROM "users" WHERE ("users"."id" = 1)
User Exists (0.2ms) SELECT "users".id FROM "users" WHERE ("users"."username" = 'foobar' AND "users".id <> 1) LIMIT 1
User Exists (0.1ms) SELECT "users".id FROM "users" WHERE ("users"."email" = 'foo@bar.baz' AND "users".id <> 1) LIMIT 1
=> true
test> def some_method
puts "Hello from some method!"
end
=> nil
test> some_method
Hello from some method!
=> nil
Tagged with irb, Rails, Ruby. Written by: Patrik Hedman
