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
1 change: 1 addition & 0 deletions ModuleConfig.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ component {
"quickInstanceReady",
"quickPreLoad",
"quickPostLoad",
"quickPostReplicate",
"quickPreSave",
"quickPostSave",
"quickPreInsert",
Expand Down
25 changes: 25 additions & 0 deletions models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,31 @@ component accessors="true" {
return entityClone;
}

/**
* Creates a new, unloaded entity with this entity's attributes except for
* its primary key and any additional excluded attributes.
*
* @except Additional attribute aliases or column names to exclude.
*
* @return quick.models.BaseEntity
*/
public any function replicate( array except = [] ) {
var attributes = retrieveAttributesData( withoutKey = true, withNulls = true );
for ( var attribute in arguments.except ) {
attributes.delete( retrieveColumnForAlias( attribute ) );
}

var replica = newEntity().fill( attributes );
replica.fireEvent(
"postReplicate",
{
"entity" : replica,
"original" : this
}
);
return replica;
}



/*===========================================
Expand Down
10 changes: 7 additions & 3 deletions tests/resources/app/models/Song.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ component extends="quick.models.BaseEntity" accessors="true" {
request.preLoadCalled = eventData;
}

function postLoad( eventData ) {
request.postLoadCalled = eventData;
}
function postLoad( eventData ) {
request.postLoadCalled = eventData;
}

function postReplicate( eventData ) {
request.postReplicateCalled = eventData;
}

function preInsert( eventData ) {
request.preInsertCalled = {
Expand Down
23 changes: 23 additions & 0 deletions tests/specs/integration/BaseEntity/CloneSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ component extends="tests.resources.ModuleIntegrationSpec" {
expect( clonedUser.isLoaded() ).toBeTrue( "The cloned user instance should be marked as loaded, but was not." );
} );

it( "can replicate a loaded entity as a new entity without its key", function() {
var user = getInstance( "User" ).findOrFail( 1 );
var replica = user.replicate();

expect( replica.isLoaded() ).toBeFalse();
expect( replica.retrieveAttributesData() ).notToHaveKey( "id" );
expect( replica.getUsername() ).toBe( user.getUsername() );

replica
.setUsername( "replicated-user" )
.setEmail( "replicated@example.com" )
.save();
expect( replica.isLoaded() ).toBeTrue();
expect( replica.getId() ).notToBe( user.getId() );
} );

it( "can exclude additional attributes when replicating", function() {
var replica = getInstance( "User" ).findOrFail( 1 ).replicate( [ "email" ] );

expect( replica.retrieveAttributesData() ).notToHaveKey( "id" );
expect( replica.retrieveAttributesData() ).notToHaveKey( "email" );
} );

it( "can clone a QuickBuilder instance", function() {
var userBuilder = getInstance( "User" ).orderBy( "id" );
var userBuilder2 = userBuilder.clone();
Expand Down
51 changes: 51 additions & 0 deletions tests/specs/integration/BaseEntity/Events/PostReplicateSpec.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
component extends="tests.resources.ModuleIntegrationSpec" {

function beforeAll() {
super.beforeAll();
controller
.getInterceptorService()
.registerInterceptor( interceptorObject = this, interceptorName = "PostReplicateSpec" );
}

function afterAll() {
controller.getInterceptorService().unregister( "PostReplicateSpec" );
super.afterAll();
}

function run() {
describe( "postReplicate spec", function() {
it( "announces a quickPostReplicate interception point", function() {
var original = getInstance( "Song" ).findOrFail( 1 );
var replica = original.replicate();

expect( variables ).toHaveKey( "quickPostReplicateCalled" );
expect( variables.quickPostReplicateCalled.entity.isLoaded() ).toBeFalse();
expect( variables.quickPostReplicateCalled.entity.getTitle() ).toBe( replica.getTitle() );
expect( variables.quickPostReplicateCalled.original.getId() ).toBe( original.getId() );
structDelete( variables, "quickPostReplicateCalled" );
} );

it( "calls a postReplicate method on the replicated component", function() {
var original = getInstance( "Song" ).findOrFail( 1 );
var replica = original.replicate();

expect( request ).toHaveKey( "postReplicateCalled" );
expect( request.postReplicateCalled.entity.isLoaded() ).toBeFalse();
expect( request.postReplicateCalled.entity.getTitle() ).toBe( replica.getTitle() );
expect( request.postReplicateCalled.original.getId() ).toBe( original.getId() );
structDelete( request, "postReplicateCalled" );
} );
} );
}

function quickPostReplicate(
event,
interceptData,
buffer,
rc,
prc
) {
variables.quickPostReplicateCalled = arguments.interceptData;
}

}
Loading