From 0b1c79c6f7d7b0abb268aa8b9e3686252adaecfb Mon Sep 17 00:00:00 2001 From: Mike Gunderloy Date: Thu, 26 Nov 2009 11:09:31 -0600 Subject: [PATCH] Allow overriding validations - pass :perform_validations => false to save from create_or_update while skipping model validations. --- lib/create_or_update.rb | 4 ++-- test/db_populate_test.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/create_or_update.rb b/lib/create_or_update.rb index 1e40311..58272da 100644 --- a/lib/create_or_update.rb +++ b/lib/create_or_update.rb @@ -10,14 +10,14 @@ class ActiveRecord::Base # Returns the record. def self.create_or_update(options = {}) id = options.delete(primary_key.to_sym) - validate = options.delete(:perform_validations) || true + validate = options.delete(:perform_validations){|k| true} record = send("find_by_#{primary_key}", id) || new record.id = id record.attributes = options if validate record.save! else - record.save!(false) + record.save(false) end record end diff --git a/test/db_populate_test.rb b/test/db_populate_test.rb index bf390cc..57b10b4 100644 --- a/test/db_populate_test.rb +++ b/test/db_populate_test.rb @@ -49,7 +49,7 @@ class DbPopulateTest < Test::Unit::TestCase def test_creates_new_record_without_validation Customer.delete_all - Customer.create_or_update(:cust_id => 1, :name => "Me") + Customer.create_or_update(:cust_id => 1, :name => "Me", :perform_validations => false) assert_equal Customer.count, 1 c = Customer.find(:first) assert_equal c.name, "Me"