Skip to content

Commit

Permalink
Allow artisan command to clear old revisions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeeptarat committed May 22, 2016
1 parent 4fbd51b commit 97d7ad5
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/composer.lock
/vendor
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
},
"require": {
"php": ">=5.5.9",
"illuminate/database": "5.*",
"illuminate/support": "5.*"
"illuminate/database": "~5.2",
"illuminate/support": "~5.2",
"illuminate/console": "~5.2"
}
}
26 changes: 13 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,28 @@ class Post extents Model
}
```

All models that use the trait should implement the abstract function
```asbstract public function by()``` which should return any string.
All models that use the trait must implement ```abstract public function by()``` which returns any string.
This function can be used to save any additional attributes such as the owner of the change.

This function can be used to save any additional attributes such as the
owner of the change.

Get the state of a record at a specific data/time.
###Get the state of a record at a specific data/time.
```php
Post::at('58781813')->find(1);
Post::at('58781813')->find(1);
```

Get the state of a record using a query string.
###Get the state of a record using a query string.

URL: ```timetravel.app/posts/1?at=58781813```
```php
Post::find(1);
Post::find(1);
```

###Get a model with revisions
```php
Post::with('revisions')->first();
```

You can clear the audits table records that are older than a specified range.
###You can clear the audits table records that are older than a specified range.
```php artisan time-traveller:clear```. This will read the config file and clear records that are older than the configured number of days.

## Todo
- Artisan command to clear revisions.
- Return revisions as models/collections - not as nested relations.
- Documentation.
- Artisan command to clear revisions.
39 changes: 39 additions & 0 deletions src/Console/ClearRevisionsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace ConceptByte\TimeTraveller\Console;

use DateTime;
use Illuminate\Console\Command;

class ClearRevisionsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'timetraveller:clear';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Clears the time traveller revisions';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$date = new DateTime;
$interval = config('timetraveller.clear');

if ($this->confirm('Do you wish to continue? [y|N]')) {
$date->modify("-$interval days")->format('Y-m-d H:i:s');
return Revision::where('created_at', '<=', $date)->delete();
}
}
}
2 changes: 1 addition & 1 deletion src/Models/Revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Revision extends Model
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function auditable()
public function revisionable()
{
return $this->morphTo();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Scopes/TimeTravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function revisions()
}

/**
* Get the revisions at the given timestamp
*
* @param $query
* @param $time
* @return mixed
Expand Down
17 changes: 2 additions & 15 deletions src/Scopes/TimeTravelScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ScopeInterface;
use Illuminate\Database\Eloquent\Scope;

class TimeTravelScope implements ScopeInterface
class TimeTravelScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
Expand All @@ -18,17 +18,4 @@ public function apply(Builder $builder, Model $model)
{
$model->scopeAt($builder, request()->get(config('timetraveller.at')));
}

/**
* Remove the scope from the given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
public function remove(Builder $builder, Model $model)
{
// TODO: Implement remove() method.
}
}

0 comments on commit 97d7ad5

Please sign in to comment.