Skip to content

Instantly share code, notes, and snippets.

@jpr5

jpr5/fix.patch Secret

Created October 20, 2009 03:29
Show Gist options
  • Save jpr5/5b7effb4cf5f737aa28e to your computer and use it in GitHub Desktop.
Save jpr5/5b7effb4cf5f737aa28e to your computer and use it in GitHub Desktop.
dm-validations triggers lazy-loaded properties to load one at a time
diff --git a/dm-validations/lib/dm-validations/contextual_validators.rb b/dm-validations/lib/dm-validations/contextual_validators.rb
index f59824e..f6f1e39 100644
--- a/dm-validations/lib/dm-validations/contextual_validators.rb
+++ b/dm-validations/lib/dm-validations/contextual_validators.rb
@@ -54,6 +54,8 @@ module DataMapper
def execute(named_context, target)
target.errors.clear!
+ target.send(:lazy_load, context(named_context).map { |v| target.class.properties[v.field_name] })
+
context(named_context).map do |validator|
validator.execute?(target) ? validator.call(target) : true
end.all?
require 'dm-core'
require 'dm-types'
require 'dm-validations'
require 'dm-timestamps'
require 'rubygems'
require 'ruby-debug'
Debugger.start
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, "mysql://root@localhost/foo")
class Foo
include DataMapper::Resource
property :id, Serial
property :name, String, :lazy => true
property :email, Text, :lazy => true
property :created_at, DateTime, :lazy => true
property :balance, Float, :lazy => true
end
::DataMapper.auto_migrate!
3.times { Foo.create }
Foo.all.each { |f| f.update(:name => "suckit") }
__END__
$ ruby foo.rb
~ (0.000081) SET sql_auto_is_null = 0
~ (0.000064) SET SESSION sql_mode = 'ANSI,NO_BACKSLASH_ESCAPES,NO_DIR_IN_CREATE,NO_ENGINE_SUBSTITUTION,NO_UNSIGNED_SUBTRACTION,TRADITIONAL'
~ (0.049827) DROP TABLE IF EXISTS `foos`
~ (0.008254) SHOW TABLES LIKE 'foos'
~ (0.000235) SHOW VARIABLES LIKE 'character_set_connection'
~ (0.000130) SHOW VARIABLES LIKE 'collation_connection'
~ (0.027263) CREATE TABLE `foos` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(50), `email` TEXT, `created_at` DATETIME, `balance` FLOAT, PRIMARY KEY(`id`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci
~ (0.000573) INSERT INTO `foos` (`created_at`) VALUES ('2009-10-19 20:26:25')
~ (0.010989) INSERT INTO `foos` (`created_at`) VALUES ('2009-10-19 20:26:25')
~ (0.000459) INSERT INTO `foos` (`created_at`) VALUES ('2009-10-19 20:26:25')
~ (0.000230) SELECT `id` FROM `foos` ORDER BY `id`
~ (0.000191) SELECT `id`, `email` FROM `foos` ORDER BY `id`
~ (0.000152) SELECT `id`, `created_at` FROM `foos` ORDER BY `id`
~ (0.000232) SELECT `id`, `balance` FROM `foos` ORDER BY `id`
~ (0.000591) UPDATE `foos` SET `name` = 'suckit' WHERE `id` = 1
~ (0.000982) UPDATE `foos` SET `name` = 'suckit' WHERE `id` = 2
~ (0.000590) UPDATE `foos` SET `name` = 'suckit' WHERE `id` = 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment