Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/specs/integration/BaseEntity/IsDirtySpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
} );
} );
}

Expand Down
Loading