Skip to content
Browse files

Keep dirty state for cached objects

  • Loading branch information...
1 parent 12ea5f8 commit 8b720c30f8000e4e8765694a64958bf80de8aebf @1602 committed Nov 23, 2011
Showing with 17 additions and 2 deletions.
  1. +6 −0 lib/abstract-class.js
  2. +11 −2 test/validations_test.coffee
View
6 lib/abstract-class.js
@@ -181,6 +181,12 @@ AbstractClass.all = function all(params, cb) {
// TODO: think about better implementation, test keeping dirty state
if (constr.cache[d.id]) {
obj = constr.cache[d.id];
+ // keep dirty attributes untouthed (remove from dataset)
+ Object.keys(obj.toObject()).forEach(function (attr) {
+ if (attr in d && obj.propertyChanged(attr)) {
+ delete d[attr];
+ }
+ });
constr.call(obj, d);
} else {
obj = new constr(d);
View
13 test/validations_test.coffee
@@ -272,5 +272,14 @@ it 'should validate uniqueness', (test) ->
user.email = 'unique@email.tld'
user.isValid (valid) ->
test.ok valid, 'valid with unique email'
- test.done()
-
+ user.save ->
+ test.done()
+
+it 'should save dirty state when validating uniqueness', (test) ->
+ User.all where: email: 'unique@email.tld' , (err, users) ->
+ u = users[0]
+ u.name = 'Hulk'
+ u.isValid (valid) ->
+ test.ok valid
+ test.equal u.name, 'Hulk'
+ test.done()

0 comments on commit 8b720c3

Please sign in to comment.
Something went wrong with that request. Please try again.