Skip to content

Commit 13ff955

Browse files
committed
Merge pull request VentureCraft#138 from VentureCraft/develop
Revision Limiting; Get all revisions for a class
2 parents 973e805 + 6f6753b commit 13ff955

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ class Article extends Eloquent {
110110
}
111111
```
112112

113+
You can also disable revisioning after X many revisions have been made by setting `$historyLimit` to the number of revisions you want to keep before stopping revisions.
114+
115+
```php
116+
namespace MyApp\Models;
117+
118+
class Article extends Eloquent {
119+
use Venturecraft\Revisionable\RevisionableTrait;
120+
121+
protected $revisionEnabled = true;
122+
protected $historyLimit = 500; //Stop tracking revisions after 500 changes have been made.
123+
}
124+
```
125+
113126
### Storing soft deletes
114127

115128
By default, if your model supports soft deletes, revisionable will store this and any restores as updates on the model.

src/Venturecraft/Revisionable/Revision.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ public function userResponsible()
187187
}
188188
}
189189

190+
191+
/**
192+
* Returns the object we have the history of
193+
* @return Object or false
194+
*/
195+
public function historyOf()
196+
{
197+
if(class_exists($class = $this->revisionable_type))
198+
{
199+
return $class::find($this->revisionable_id);
200+
}
201+
return false;
202+
}
203+
190204
/*
191205
* Egzamples:
192206
array(

src/Venturecraft/Revisionable/RevisionableTrait.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ public function revisionHistory()
5353
return $this->morphMany('\Venturecraft\Revisionable\Revision', 'revisionable');
5454
}
5555

56+
/**
57+
* Generates a list of the last $limit revisions made to any objects of the class it is being called from.
58+
*
59+
* @param int $limit
60+
* @param string $order
61+
* @return mixed
62+
*/
63+
public static function classRevisionHistory($limit=100,$order='desc')
64+
{
65+
return \Venturecraft\Revisionable\Revision::where('revisionable_type',get_called_class())->orderBy('updated_at',$order)->limit($limit)->get();
66+
}
5667
/**
5768
* Invoked before a model is saved. Return false to abort the operation.
5869
*
@@ -104,9 +115,14 @@ public function preSave()
104115
*/
105116
public function postSave()
106117
{
118+
if (isset($this->historyLimit) && $this->revisionHistory()->count() >= $this->historyLimit){
119+
$LimitReached=true;
120+
}else{
121+
$LimitReached=false;
122+
}
107123

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

112128
$changes_to_record = $this->changedRevisionableFields();

0 commit comments

Comments
 (0)