diff --git a/models/BaseEntity.cfc b/models/BaseEntity.cfc index a3f7a943..c0095c84 100644 --- a/models/BaseEntity.cfc +++ b/models/BaseEntity.cfc @@ -857,6 +857,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 d976c894..04bb6aa2 100644 --- a/tests/specs/integration/BaseEntity/IsDirtySpec.cfc +++ b/tests/specs/integration/BaseEntity/IsDirtySpec.cfc @@ -17,6 +17,19 @@ component extends="tests.resources.ModuleIntegrationSpec" { user.fill( { "last_name" : "Peterson" } ); expect( user.isDirty() ).toBeFalse(); } ); + + 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(); + } ); } ); }