Skip to content

Commit

Permalink
Merge pull request #167 from VentureCraft/develop
Browse files Browse the repository at this point in the history
Enable saving model creations
  • Loading branch information
duellsy committed Sep 23, 2015
2 parents b7f131b + 9f0f8ef commit d147508
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 5 deletions.
23 changes: 22 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Article extends Eloquent {
protected $historyLimit = 500; //Stop tracking revisions after 500 changes have been made.
}
```
In order to maintain a limit on history, but instead of stopping tracking revisions if you want to remove old revisions, you can accomodate that feature by setting `$revisionCleanup`.
In order to maintain a limit on history, but instead of stopping tracking revisions if you want to remove old revisions, you can accomodate that feature by setting `$revisionCleanup`.

```php
namespace MyApp\Models;
Expand All @@ -151,6 +151,16 @@ You can choose to ignore deletes and restores by adding `deleted_at` to your `$d
To better format the output for `deleted_at` entries, you can use the `isEmpty` formatter (see <a href="#format-output">Format output</a> for an example of this.)

<a name="control"></a>

### Storing creations
By default the creation of a new model is not stored as a revision.
Only subsequent changes to a model is stored.

If you want to store the creation as a revision you can override this behavior by setting `revisionCreationsEnabled` to `true` by adding the following to your model:
```php
protected $revisionCreationsEnabled = true;
```

## More control

No doubt, there'll be cases where you don't want to store a revision history only for certain fields of the model, this is supported in two different ways. In your model you can either specifiy which fields you explicitly want to track and all other fields are ignored:
Expand Down Expand Up @@ -252,6 +262,17 @@ The above would be the result from this:
@endforeach
```

If you have enabled revisions of creations as well you can display it like this:
```php
@foreach($resource->revisionHistory as $history)
@if($history->key == 'created_at' && !$history->old_value)
<li>{{ $history->userResponsible()->first_name }} created this resource at {{ $history->newValue() }}</li>
@else
<li>{{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}</li>
@endif
@endforeach
```

### userResponsible()

Returns the User that was responsible for making the revision. A user model is returned, or null if there was no user recorded.
Expand Down
37 changes: 37 additions & 0 deletions src/Venturecraft/Revisionable/Revisionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public static function boot()
$model->postSave();
});

static::created(function($model){
$model->postCreate();
});

static::deleted(function ($model) {
$model->preSave();
$model->postDelete();
Expand Down Expand Up @@ -155,6 +159,39 @@ public function postSave()
}
}

/**
* Called after record successfully created
*/
public function postCreate()
{

// Check if we should store creations in our revision history
// Set this value to true in your model if you want to
if(empty($this->revisionCreationsEnabled))
{
// We should not store creations.
return false;
}

if ((!isset($this->revisionEnabled) || $this->revisionEnabled))
{
$revisions[] = array(
'revisionable_type' => get_class($this),
'revisionable_id' => $this->getKey(),
'key' => 'created_at',
'old_value' => null,
'new_value' => $this->created_at,
'user_id' => $this->getUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);

$revision = new Revision;
\DB::table($revision->getTable())->insert($revisions);

}
}

/**
* If softdeletes are enabled, store the deleted time
*/
Expand Down
47 changes: 43 additions & 4 deletions src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public static function bootRevisionableTrait()
$model->postSave();
});

static::created(function($model){
$model->postCreate();
});

static::deleted(function ($model) {
$model->preSave();
$model->postDelete();
Expand Down Expand Up @@ -103,10 +107,10 @@ public static function classRevisionHistory($limit = 100, $order = 'desc')
}

/**
* Invoked before a model is saved. Return false to abort the operation.
*
* @return bool
*/
* Invoked before a model is saved. Return false to abort the operation.
*
* @return bool
*/
public function preSave()
{
if (!isset($this->revisionEnabled) || $this->revisionEnabled) {
Expand Down Expand Up @@ -196,6 +200,41 @@ public function postSave()
}
}

/**
* Called after record successfully created
*/
public function postCreate()
{

// Check if we should store creations in our revision history
// Set this value to true in your model if you want to
if(empty($this->revisionCreationsEnabled))
{
// We should not store creations.
return false;
}

if ((!isset($this->revisionEnabled) || $this->revisionEnabled))
{
$revisions[] = array(
'revisionable_type' => get_class($this),
'revisionable_id' => $this->getKey(),
'key' => 'created_at',
'old_value' => null,
'new_value' => $this->created_at,
'user_id' => $this->getUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);

$revision = new Revision;
\DB::table($revision->getTable())->insert($revisions);

}


}

/**
* If softdeletes are enabled, store the deleted time
*/
Expand Down

0 comments on commit d147508

Please sign in to comment.