Skip to content

Commit

Permalink
Merge pull request #156 from VentureCraft/develop
Browse files Browse the repository at this point in the history
Allow limiting revision count, and removing old revisions.
  • Loading branch information
duellsy committed Jul 6, 2015
2 parents 2626d8b + 8d64fdd commit fddb69b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ 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`.

```php
namespace MyApp\Models;

class Article extends Eloquent {
use Venturecraft\Revisionable\RevisionableTrait;

protected $revisionEnabled = true;
protected $revisionCleanup = true; //Remove old revisions (works only when used with $historyLimit)
protected $historyLimit = 500; //Maintain a maximum of 500 changes at any point of time, while cleaning up old revisions.
}
```

### Storing soft deletes

Expand Down
13 changes: 12 additions & 1 deletion src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,14 @@ public function postSave()
} else {
$LimitReached = false;
}
if (isset($this->revisionCleanup)){
$RevisionCleanup=$this->revisionCleanup;
}else{
$RevisionCleanup=false;
}

// check if the model already exists
if (((!isset($this->revisionEnabled) || $this->revisionEnabled) && $this->updating) && !$LimitReached) {
if (((!isset($this->revisionEnabled) || $this->revisionEnabled) && $this->updating) && (!$LimitReached || $RevisionCleanup)) {
// if it does, it means we're updating

$changes_to_record = $this->changedRevisionableFields();
Expand All @@ -179,6 +184,12 @@ public function postSave()
}

if (count($revisions) > 0) {
if($LimitReached && $RevisionCleanup){
$toDelete = $this->revisionHistory()->orderBy('id','asc')->limit(count($revisions))->get();
foreach($toDelete as $delete){
$delete->delete();
}
}
$revision = new Revision;
\DB::table($revision->getTable())->insert($revisions);
}
Expand Down

0 comments on commit fddb69b

Please sign in to comment.