Skip to content

Commit

Permalink
Merge pull request #80 from pheeque/version-hidden-fields
Browse files Browse the repository at this point in the history
Allow versioning of hidden fields in model_data.
  • Loading branch information
nonoesp authored Apr 30, 2021
2 parents e2cb4da + 1c0a7ae commit 043b311
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,28 @@ class User extends Model {
}
```

<a name="hiddenFields" />

### Hidden fields

There are times you might want to include hidden fields in the version data. You might have hidden the fields with the `visible` or `hidden` properties in your model.

You can have those fields that are typically hidden in the rest of your project saved in the version data by adding them to the `versionedHiddenFields` property of the versionable model.

```php
class User {

use VersionableTrait;

// Typically hidden fields
protected $hidden = ['email', 'password'];

// Save these hidden fields
protected $versionedHiddenFields = ['email', 'password'];

}
```

<a name="maximum" />

### Maximum number of stored versions
Expand Down
4 changes: 4 additions & 0 deletions src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ protected function versionablePostSave()
$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;
Expand Down
34 changes: 34 additions & 0 deletions tests/VersionableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,31 @@ public function testKeepMaxVersionCount()

$this->assertEquals( $name_v3, $model->name );
}

public function testAllowHiddenFields() {
$user = new TestHiddenFieldsUser();
$user->name = "Marcel";
$user->email = "[email protected]";
$user->password = "12345";
$user->save();
sleep(1);

$user->name = "John";
$user->email = "[email protected]";
$user->password = "6789";
$user->save();
sleep(1);

$diff = $user->previousVersion()->diff();

$this->assertArrayHasKey('email', $diff);
$this->assertArrayHasKey('password', $diff);
$this->assertEquals( 'John', $diff['name'] );
$this->assertEquals( '[email protected]', $diff['email'] );
$this->assertEquals( '6789', $diff['password'] );

$this->assertArrayNotHasKey('password', $user->toArray());
}

}

Expand Down Expand Up @@ -545,3 +570,12 @@ class ModelWithJsonField extends Model
protected $casts = ['json_field' => 'array'];
}

class TestHiddenFieldsUser extends \Illuminate\Foundation\Auth\User {
use \Mpociot\Versionable\VersionableTrait;

protected $table = "users";

protected $hidden = ['email', 'password'];

protected $versionedHiddenFields = ['email', 'password'];
}

0 comments on commit 043b311

Please sign in to comment.