Skip to content

Commit

Permalink
Merge pull request #53 from VentureCraft/develop
Browse files Browse the repository at this point in the history
Adding support for soft deletes
  • Loading branch information
duellsy committed Apr 3, 2014
2 parents 1747433 + 96d70a3 commit b22b23e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class Article extends Eloquent {
}
```

### Storing soft deletes

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

You can choose to ignore deletes and restores by adding `deleted_at` to your `$dontKeepRevisionOf` array.

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>
## More control

Expand Down Expand Up @@ -143,6 +151,7 @@ In cases where you want to have control over the format of the output of the val
protected $revisionFormattedFields = array(
'title' => 'string:<strong>%s</strong>',
'public' => 'boolean:No|Yes'
'deleted_at' => 'isEmpty:Active|Deleted'
);
```

Expand All @@ -160,6 +169,13 @@ Booleans by default will display as a 0 or a 1, which is pretty bland and won't
boolean:No|Yes
```

### Is Empty
This piggy backs off boolean, but instead of testing for a true or false value, it checks if the value is either null or an empty string.

```
isEmpty:No|Yes
```

<a name="loadhistory"></a>
## Load revision history

Expand Down
15 changes: 15 additions & 0 deletions src/Venturecraft/Revisionable/FieldFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ public static function format($key, $value, $formats)
}


/**
* Check if a field is empty
*
* @param $value
* @param array $options
*
* @return string
*/
public static function isEmpty($value, $options = array())
{
$value_set = isset($value) && $value != '';
return self::boolean($value_set, $options);
}


/**
* Boolean
*
Expand Down
30 changes: 30 additions & 0 deletions src/Venturecraft/Revisionable/Revisionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public static function boot()
$model->postSave();
});

static::deleted(function($model)
{
$model->preSave();
$model->postDelete();
});

}


Expand Down Expand Up @@ -138,6 +144,30 @@ public function postSave()
}


/**
* If softdeletes are enabled, store the deleted time
*/
public function postDelete()
{
if ((!isset($this->revisionEnabled) || $this->revisionEnabled)
&& $this->softDelete
&& $this->isRevisionable('deleted_at')) {
$revisions[] = array(
'revisionable_type' => get_class($this),
'revisionable_id' => $this->getKey(),
'key' => 'deleted_at',
'old_value' => null,
'new_value' => $this->deleted_at,
'user_id' => $this->getUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);
$revision = new \Venturecraft\Revisionable\Revision;
\DB::table($revision->getTable())->insert($revisions);
}
}


/**
* Attempt to find the user id of the currently logged in user
* Supports Sentry based authentication, as well as stock Auth
Expand Down
30 changes: 30 additions & 0 deletions src/Venturecraft/Revisionable/RevisionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public static function boot()
$model->postSave();
});

static::deleted(function($model)
{
$model->preSave();
$model->postDelete();
});

}


Expand Down Expand Up @@ -138,6 +144,30 @@ public function postSave()
}


/**
* If softdeletes are enabled, store the deleted time
*/
public function postDelete()
{
if ((!isset($this->revisionEnabled) || $this->revisionEnabled)
&& $this->softDelete
&& $this->isRevisionable('deleted_at')) {
$revisions[] = array(
'revisionable_type' => get_class($this),
'revisionable_id' => $this->getKey(),
'key' => 'deleted_at',
'old_value' => null,
'new_value' => $this->deleted_at,
'user_id' => $this->getUserId(),
'created_at' => new \DateTime(),
'updated_at' => new \DateTime(),
);
$revision = new \Venturecraft\Revisionable\Revision;
\DB::table($revision->getTable())->insert($revisions);
}
}


/**
* Attempt to find the user id of the currently logged in user
* Supports Sentry based authentication, as well as stock Auth
Expand Down

0 comments on commit b22b23e

Please sign in to comment.