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

44 refactor model data serialization #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
26 changes: 21 additions & 5 deletions src/Mpociot/Versionable/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ public function getResponsibleUserAttribute()
*/
public function getModel()
{
$modelData = is_resource($this->model_data)
? stream_get_contents($this->model_data)
: $this->model_data;

$model = new $this->versionable_type();
$model->unguard();
$model->fill(unserialize($modelData));
$model->fill($this->modelData());
$model->exists = true;
$model->reguard();
return $model;
Expand Down Expand Up @@ -104,4 +100,24 @@ public function diff(Version $againstVersion = null)
return $diffArray;
}

/**
* Copy attributes of a model to be saved with the version
*
* @param Model $model
*/
public function copyModelData(Model $model)
{
$this->model_data = serialize($model->attributesToArray());
}

/**
* Model attributes
*/
protected function modelData()
{
$serialized = is_resource($this->model_data)
? stream_get_contents($this->model_data)
: $this->model_data;
return unserialize($serialized);
}
}
2 changes: 1 addition & 1 deletion src/Mpociot/Versionable/VersionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected function versionablePostSave()
$version->versionable_id = $this->getKey();
$version->versionable_type = get_class($this);
$version->user_id = $this->getAuthUserId();
$version->model_data = serialize($this->getAttributes());
$version->copyModelData($this);

if (!empty( $this->reason )) {
$version->reason = $this->reason;
Expand Down
16 changes: 16 additions & 0 deletions tests/VersionableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ public function testGetVersionModel()

}

public function testGetVersionModelWithJsonField()
{
$model = new ModelWithJsonField();
$model->json_field = ["foo" => "bar"];
$model->save();

$this->assertEquals(["foo" => "bar"], $model->getVersionModel(1)->json_field);
}

public function testUseReasonAttribute()
{
// Create 3 versions
Expand Down Expand Up @@ -547,4 +556,11 @@ class ModelWithDynamicVersion extends Model
use VersionableTrait ;
protected $versionClass = DynamicVersionModel::class ;
}
class ModelWithJsonField extends Model
{
const TABLENAME = 'table_with_json_field';
public $table = self::TABLENAME ;
use VersionableTrait ;
protected $casts = ['json_field' => 'array'];
}

6 changes: 6 additions & 0 deletions tests/VersionableTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@ public function migrateUsersTable()
$table->index('versionable_id');
$table->timestamps();
});

$this->app['db']->connection()->getSchemaBuilder()->create(ModelWithJsonField::TABLENAME, function ($table) {
$table->increments('id');
$table->json('json_field');
$table->timestamps();
});
}
}