diff --git a/models/BaseEntity.cfc b/models/BaseEntity.cfc index 6ef81f1..f2fa40e 100644 --- a/models/BaseEntity.cfc +++ b/models/BaseEntity.cfc @@ -910,6 +910,31 @@ component accessors="true" { return compare( variables._originalAttributesHash, computeAttributesHash( retrieveAttributesData() ) ) != 0; } + /** + * Returns whether the entity, or one specific attribute, is unchanged from + * its originally loaded state. + * + * @attribute An optional attribute alias or column name to inspect. + */ + public boolean function isClean( string attribute ) { + if ( isNull( arguments.attribute ) ) { + return !isDirty(); + } + + guardAgainstNonExistentAttribute( arguments.attribute ); + var column = retrieveColumnForAlias( arguments.attribute ); + var currentAttributes = retrieveAttributesData( withNulls = true ); + var originalAttribute = {}; + var currentAttribute = {}; + if ( variables._originalAttributes.keyExists( column ) ) { + originalAttribute[ column ] = variables._originalAttributes[ column ]; + } + if ( currentAttributes.keyExists( column ) ) { + currentAttribute[ column ] = currentAttributes[ column ]; + } + return compare( computeAttributesHash( originalAttribute ), computeAttributesHash( currentAttribute ) ) == 0; + } + /** * Retrieves a value for an attribute. * diff --git a/tests/specs/integration/BaseEntity/IsDirtySpec.cfc b/tests/specs/integration/BaseEntity/IsDirtySpec.cfc index 6e1c41a..b753c7e 100644 --- a/tests/specs/integration/BaseEntity/IsDirtySpec.cfc +++ b/tests/specs/integration/BaseEntity/IsDirtySpec.cfc @@ -37,6 +37,19 @@ component extends="tests.resources.ModuleIntegrationSpec" { expect( user.isDirty( "username" ) ).toBeFalse(); expect( user.isDirty() ).toBeTrue(); } ); + + it( "can test whether the entity or a specific attribute is clean", function() { + var user = getInstance( "User" ).findOrFail( 1 ); + + expect( user.isClean() ).toBeTrue(); + expect( user.isClean( "username" ) ).toBeTrue(); + expect( user.isClean( "first_name" ) ).toBeTrue(); + + user.setUsername( "updated-username" ); + expect( user.isClean() ).toBeFalse(); + expect( user.isClean( "username" ) ).toBeFalse(); + expect( user.isClean( "firstName" ) ).toBeTrue(); + } ); } ); }