Created
March 21, 2010 04:56
-
-
Save pathsny/339102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 37e5373cf03468b4c9c6579ea82a0fcda4c4a3ca Mon Sep 17 00:00:00 2001 | |
From: Vishnu Iyengar <[email protected]> | |
Date: Sun, 21 Mar 2010 10:24:51 +0530 | |
Subject: [PATCH] updating collection#update! and resource _update to validate attributes similarly | |
--- | |
lib/dm-core/collection.rb | 6 +++--- | |
lib/dm-core/resource.rb | 34 ++++++++++++++++++++++------------ | |
2 files changed, 25 insertions(+), 15 deletions(-) | |
diff --git a/lib/dm-core/collection.rb b/lib/dm-core/collection.rb | |
index 3418119..050707a 100644 | |
--- a/lib/dm-core/collection.rb | |
+++ b/lib/dm-core/collection.rb | |
@@ -836,12 +836,12 @@ module DataMapper | |
assert_update_clean_only(:update!) | |
model = self.model | |
- | |
- dirty_attributes = model.new(attributes).dirty_attributes | |
+ model_instance = model.new(attributes) | |
+ dirty_attributes = model_instance.dirty_attributes | |
if dirty_attributes.empty? | |
true | |
- elsif dirty_attributes.any? { |property, value| !property.valid?(value) } | |
+ elsif !model_instance.attributes_valid? | |
false | |
else | |
unless _update(dirty_attributes) | |
diff --git a/lib/dm-core/resource.rb b/lib/dm-core/resource.rb | |
index c483fa5..2dc9bf3 100644 | |
--- a/lib/dm-core/resource.rb | |
+++ b/lib/dm-core/resource.rb | |
@@ -645,6 +645,19 @@ module DataMapper | |
Query.new(repository, model, :fields => fields, :conditions => conditions) | |
end | |
+ #Validates the attribute list | |
+ # | |
+ # this is to allow various update methods to validate whether the proposed list | |
+ # of attributes is valid | |
+ # | |
+ # @return [Boolean] | |
+ # true if the list is valid | |
+ # | |
+ # @api semipublic | |
+ def attributes_valid? | |
+ self.original_attributes.all? {|property, _value| property.valid?(property.get!(self))} | |
+ end | |
+ | |
protected | |
# Method for hooking callbacks on resource creation | |
@@ -924,24 +937,21 @@ module DataMapper | |
# @api private | |
def _update | |
original_attributes = self.original_attributes | |
- | |
- if original_attributes.any? { |property, _value| !property.valid?(property.get!(self)) } | |
- false | |
- else | |
+ | |
+ return false unless self.attributes_valid? | |
# remove from the identity map | |
- remove_from_identity_map | |
+ remove_from_identity_map | |
- repository.update(dirty_attributes, collection_for_self) | |
+ repository.update(dirty_attributes, collection_for_self) | |
- original_attributes.clear | |
+ original_attributes.clear | |
- # remove the cached key in case it is updated | |
- remove_instance_variable(:@_key) | |
+ # remove the cached key in case it is updated | |
+ remove_instance_variable(:@_key) | |
- add_to_identity_map | |
+ add_to_identity_map | |
- true | |
- end | |
+ true | |
end | |
# @api private | |
-- | |
1.6.5.2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment