Skip to content

Commit aabbb00

Browse files
committed
Fix dirty checking handling broken by #1232
1 parent 8a925af commit aabbb00

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/dirty/checking/DirtyCheckable.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ trait DirtyCheckable {
8080
def oldValue = ((GroovyObject) this).getProperty(propertyName)
8181
if ((newValue == null && oldValue != null) ||
8282
(newValue != null && oldValue == null) ||
83-
(newValue && !newValue.equals(oldValue))) {
83+
(newValue != null && !newValue.equals(oldValue))) {
8484
$changedProperties.put propertyName, oldValue
8585
}
8686
}
@@ -95,7 +95,7 @@ trait DirtyCheckable {
9595
if( $changedProperties != null && !$changedProperties.containsKey(propertyName)) {
9696
if ((newValue == null && oldValue != null) ||
9797
(newValue != null && oldValue == null) ||
98-
(newValue && !newValue.equals(oldValue))) {
98+
(newValue != null && !newValue.equals(oldValue))) {
9999
$changedProperties.put propertyName, oldValue
100100
}
101101
}

grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/dirty/checking/DirtyCheckableSpec.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ class DirtyCheckableSpec extends Specification {
3333
person.hasChanged('lastViewedPost')
3434
person.getOriginalValue('lastViewedPost').equals(blogPost)
3535
}
36+
37+
def 'setting a field that is a boolean dirty checks properly'() {
38+
given: 'a class with a boolean property'
39+
def animal = new Animal()
40+
animal.trackChanges()
41+
42+
when:"A boolean property is changed"
43+
animal.barks = true
44+
animal.markDirty("barks", true, false)
45+
46+
then:"the property changed"
47+
animal.barks
48+
animal.hasChanged()
49+
animal.hasChanged("barks")
50+
51+
when:"it is set to false"
52+
animal.trackChanges() // reset
53+
animal.barks = false
54+
animal.markDirty("barks", false, true)
55+
56+
then:"the property changed"
57+
!animal.barks
58+
animal.hasChanged()
59+
animal.hasChanged("barks")
60+
61+
}
62+
}
63+
64+
class Animal implements DirtyCheckable {
65+
boolean barks
3666
}
3767

3868
class Person implements DirtyCheckable {

0 commit comments

Comments
 (0)