Skip to content

Commit 4c1a670

Browse files
committed
Use enum instead of boolean
1 parent 84ef9bc commit 4c1a670

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

grails-datastore-gorm-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.grails.datastore.gorm
22

33
import grails.gorm.annotation.AutoTimestamp
4+
import static grails.gorm.annotation.AutoTimestamp.EventType.*;
45
import grails.gorm.tests.GormDatastoreSpec
56
import grails.persistence.Entity
67

@@ -39,6 +40,6 @@ class CustomAutoTimestampSpec extends GormDatastoreSpec {
3940
class RecordCustom {
4041
Long id
4142
String name
42-
@AutoTimestamp(false) Date created
43+
@AutoTimestamp(ON_INSERT) Date created
4344
@AutoTimestamp Date modified
4445
}

grails-datastore-gorm/src/main/groovy/grails/gorm/annotation/AutoTimestamp.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@
3030
@Retention(RetentionPolicy.RUNTIME)
3131
@Target({ElementType.FIELD})
3232
public @interface AutoTimestamp {
33+
34+
/**
35+
* Enum to specify when auto-timestamping should occur.
36+
*/
37+
enum EventType {
38+
ON_INSERT,
39+
ON_UPDATE
40+
}
41+
3342
/**
34-
* Whether to include auto-timestamping on update events.
35-
* Setting value to false only performs auto-timestamping on insert events.
43+
* When to apply auto-timestamping
3644
*/
37-
boolean value() default true;
45+
EventType value() default EventType.ON_UPDATE;
3846
}

grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected void storeDateCreatedAndLastUpdatedInfo(PersistentEntity persistentEnt
170170
Field field = getFieldFromHierarchy(persistentEntity, property.getName());
171171
if (field != null && field.isAnnotationPresent(AutoTimestamp.class)) {
172172
AutoTimestamp autoTimestamp = field.getAnnotation(AutoTimestamp.class);
173-
if (autoTimestamp.value()) {
173+
if (autoTimestamp.value() == AutoTimestamp.EventType.ON_UPDATE) {
174174
storeTimestampAvailability(entitiesWithLastUpdated, persistentEntity, property);
175175
} else {
176176
storeTimestampAvailability(entitiesWithDateCreated, persistentEntity, property);

0 commit comments

Comments
 (0)