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] = {
:P ROMPT_I => "#{rails_root}> ",
:P ROMPT_S => "#{rails_root}* ",
:P 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

Posted in Rails, Ruby at July 31st, 2009. 2 Comments
Tagged with , , . Written by: Patrik Hedman

In my last post I showed how you can install my suppress_validations gem on a per-project basis but that’s a bit cumbersome. Wouldn’t it be nicer if it always was available in the console on every project?
I think it would, so here’s how you can accomplish just that:

Edit your ~/.irbrc (or create it if you don’t already have one):

require "rubygems"

if ENV['RAILS_ENV']
IRB.conf[:IRB_RC] = Proc.new do
require "suppress_validations"
include SuppressValidations
end
end

And thats it, you can now fire up script/console and start suppressing validations

$ script/console
Loading development environment (Rails 2.3.2)
>> post = Post.new
=> #
>> post.save
=> false
>> suppress_validations { post.save }
=> true
>> Post.last
=> #

Posted in Rails, Ruby, gems at July 28th, 2009. No Comments
Tagged with , , , . Written by: Patrik Hedman

Validations on ActiveRecord models can really get in the way while trying things out in the console sometimes so the other day when I stumbled over this railscast episode: http://railscasts.com/episodes/62-hacking-activerecord I thought, hey, with some modification that could easily be turned into a plugin.

Install it with:


$ gem install polly-suppress_validations

then add this to your config/environment.rb file:


config.gem "polly-suppress_validations", :source => "http://gems.github.com", :lib => "suppress_validations"

and then unpack it with:


$ rake gems:unpack

or just install it as a plugin:

$ script/plugin install git://github.com/polly/suppress_validations.git

Now say we have a model like this:

class Post < ActiveRecord::Base
validates_presence_of :title, :body
end

Now lets enter script/console and include the SuppressValidations module

$ script/console
Loading development environment (Rails 2.3.2)
>> include SuppressValidations
=> Object

However, trying to create a new post without the required attributes still fails as this example shows:

>> post = Post.new
=> #
>> post.save
=> false

To suppress validations just wrap the call to post.save in a suppress_validations block, like so:

>> suppress_validations { post.save }
=> true
>> Post.last
=> #

Posted in Projects, Rails, Ruby, gems at July 20th, 2009. No Comments
Tagged with , , . Written by: Patrik Hedman

pastbedti.me is using WP-Gravatar