Skip to content

Commit

Permalink
feat(Events): Add newAttributes and originalAttributes to the `pr…
Browse files Browse the repository at this point in the history
…eUpdate` event
  • Loading branch information
elpete committed May 31, 2024
1 parent 084a089 commit 4dd8496
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
9 changes: 8 additions & 1 deletion models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,14 @@ component accessors="true" {
variables._saving = true;
var builder = newQuery();
if ( variables._loaded ) {
fireEvent( "preUpdate", { entity : this } );
fireEvent(
"preUpdate",
{
"entity" : this,
"newAttributes" : get_data(),
"originalAttributes" : get_originalAttributes()
}
);
builder
.where( function( q ) {
arrayZipEach( [ keyNames(), keyValues() ], function( keyName, keyValue ) {
Expand Down
4 changes: 1 addition & 3 deletions models/Relationships/HasOneOrMany.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,7 @@ component
* @return quick.models.BaseEntity
*/
public any function create( struct attributes = {} ) {
return newEntity()
.fill( arguments.attributes )
.save();
return newEntity().fill( arguments.attributes ).save();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/specs/integration/BaseEntity/Events/PreUpdateSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ component extends="tests.resources.ModuleIntegrationSpec" {
expect( request.quickPreUpdateCalled[ 1 ].entity.getDownloadUrl() ).toBe(
"https://open.spotify.com/track/0GHGd3jYqChGNxzjqgRZSv"
);
expect( request.quickPreUpdateCalled[ 1 ] ).toHaveKey( "originalAttributes" );
expect( request.quickPreUpdateCalled[ 1 ].originalAttributes.download_url ).toBe( "https://open.spotify.com/track/4Nd5HJn4EExnLmHtClk4QV" );
expect( request.quickPreUpdateCalled[ 1 ] ).toHaveKey( "newAttributes" );
expect( request.quickPreUpdateCalled[ 1 ].newAttributes.download_url ).toBe( "https://open.spotify.com/track/0GHGd3jYqChGNxzjqgRZSv" );

structDelete( request, "quickPreUpdateCalled" );
} );

Expand Down

0 comments on commit 4dd8496

Please sign in to comment.