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

Allow a model to set 'minor' attributes that won't create a new version #11

Open
wants to merge 1 commit 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
19 changes: 19 additions & 0 deletions src/Traits/Versioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
trait Versioned
{

protected $minorAttributes = [];

protected $isVersioned = true;

protected $hideVersioned = [
Expand Down Expand Up @@ -111,6 +113,10 @@ public function saveMinor(array $options = array())
*/
public function save(array $options = array())
{
if ($this->exists && $this->onlyHasMinorEdits()) {
return $this->saveMinor($options);
}

$query = $this->newQueryWithoutScopes();

$db = $this->getConnection();
Expand Down Expand Up @@ -334,4 +340,17 @@ public static function onlyOldVersions()
return (new static)->newQueryWithoutScope(new VersioningScope)
->where(static::getQualifiedIsCurrentVersionColumn(), 0);
}

public function onlyHasMinorEdits()
{
$changedAttributes = $this->getDirty();

foreach ($changedAttributes as $key => $value) {
if (!in_array($key, $this->minorAttributes)) {
return false;
}
}

return true;
}
}
10 changes: 10 additions & 0 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,15 @@ protected function migrateTables()
$table->string('name');
$table->timestamps();
});

DBM::schema()->create('bars', function ($table) {
$table->increments('id');
$table->integer('model_id')->unsigned()->default(1);
$table->integer('version')->unsigned()->default(1);
$table->integer('is_current_version')->unsigned()->default(1);
$table->string('name');
$table->string('title');
$table->timestamps();
});
}
}
13 changes: 13 additions & 0 deletions tests/Models/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace EloquentVersioned\Tests\Models;

class Bar extends BaseVersionedModel
{
protected $minorAttributes = [
'name',
];

protected $table = 'bars';

protected $fillable = ['*'];
}
47 changes: 47 additions & 0 deletions tests/VersionedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ public function testMinorSave($data)
$this->assertEquals(1, count($models));
}

/**
* Making changes to fields marked as minor shouldn't create a new version
*
* @param array $data
*
* @dataProvider createMinorEditsDataProvider
*/
public function testMinorEdits($data)
{
$className = $this->modelPrefix . $data['name'];
$model = $className::create($data)->fresh();

$newName = 'Updated ' . $data['name'];
$model->name = $newName;
$model->save();

// was the model updated with a minor edit?
$this->assertEquals(1, $model->id);
$this->assertEquals($newName, $model->name);
$this->assertEquals(1, $model->version);

$newTitle = 'The New Bar';
$model->title = $newTitle;
$model->save();

// was the model updated with a major edit?
$this->assertEquals($newTitle, $model->title);
$this->assertEquals(2, $model->version);
}

/**
* Provides objects to use by tests
*
Expand All @@ -119,4 +149,21 @@ public function createDataProvider()
array(array('name' => 'Doodad', 'widget_id' => 1, 'gadget_id' => 1))
);
}

/**
* Provides objects to use by tests
*
* @return array
*/
public function createMinorEditsDataProvider()
{
return [
[
[
'name' => 'Bar',
'title' => 'The Bar',
],
],
];
}
}