Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create initial version #100

Merged
merged 3 commits into from
Mar 27, 2023
Merged
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ Every time you update your model, a new version containing the previous attribut

All timestamps and the optional soft-delete timestamp will be ignored.

<a name="existing" />

### Adding versions to existing data

Versionable creates a version on update() of the *updated* model. So, if you're installing this on an already existing application, you may want to create a version of the current model:

```php
$model->createInitialVersion();
```
If no version exists, this will create the initial version.

If you want to do this for all instances of a model:

```php
Model::initializeVersionOnAllRows();
```

<a name="exclude" />

### Exclude attributes from versioning
Expand Down
43 changes: 43 additions & 0 deletions src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,48 @@ protected function versionablePostSave()
}
}


/**
* Initialize a version on every instance of a model
* @return void
*/
public static function initializeVersionOnAllRows()
{
foreach (self::all() as $obj) {
$obj->createInitialVersion();
}
}

/**
* Save a new version.
* @return void
*/
public function createInitialVersion()
{
if( true === $this->fresh()->versions->isEmpty() &&
true === $this->versioningEnabled
) {

$class = $this->getVersionClass();
$version = new $class();
$version->versionable_id = $this->getKey();
$version->versionable_type = method_exists($this, 'getMorphClass') ? $this->getMorphClass() : get_class($this);
$version->user_id = $this->getAuthUserId();

$versionedHiddenFields = $this->versionedHiddenFields ?? [];
$this->makeVisible($versionedHiddenFields);
$version->model_data = serialize($this->attributesToArray());
$this->makeHidden($versionedHiddenFields);

if (!empty( $this->reason )) {
$version->reason = $this->reason;
}

$version->save();
}
}


/**
* Delete old versions of this model when they reach a specific count.
*
Expand All @@ -215,6 +257,7 @@ private function purgeOldVersions()

/**
* Determine if a new version should be created for this model.
* Checks if appropriate fields have been changed.
*
* @return bool
*/
Expand Down
42 changes: 42 additions & 0 deletions tests/VersionableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,48 @@ public function testWhereModelHasMorphMap()
$this->assertEquals( $user->attributesToArray(), $version->getModel()->attributesToArray() );
Relation::morphMap([], false);
}

public function testAddVersionableToExistingUser()
{
$user = new \Illuminate\Foundation\Auth\User();
$user->name = "Danny";
$user->email = "[email protected]";
$user->password = "12345";
$user->save();

$this->assertNull($user->versions );

$user = TestVersionableUser::find($user->id);
$this->assertCount(0, $user->versions );
$user->createInitialVersion();
$this->assertCount(1, $user->fresh()->versions );

//ASSERT THAT createInitialVersion() ONLY WORKS ONCE
$user->createInitialVersion();
$this->assertCount(1, $user->fresh()->versions );
}

public function testInitializeModel()
{
$user = new \Illuminate\Foundation\Auth\User();
$user->name = "Danny";
$user->email = "[email protected]";
$user->password = "12345";
$user->save();

$this->assertNull($user->versions );

$user = TestVersionableUser::find($user->id);
$this->assertCount(0, $user->versions );

TestVersionableUser::initializeVersionOnAllRows();
$this->assertCount(1, $user->fresh()->versions );

//ASSERT THAT createInitialVersion() ONLY WORKS ONCE
TestVersionableUser::initializeVersionOnAllRows();
$this->assertCount(1, $user->fresh()->versions );
}


}

Expand Down