Skip to content

Commit

Permalink
Allows programmatic toggling of soft deletes
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed May 14, 2024
1 parent c3d4ba6 commit cf8b4cb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/Database/Scopes/MultisiteScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public function apply(BuilderBase $builder, ModelBase $model)
}
}

/**
* extend the Eloquent query builder.
*/
public function extend(BuilderBase $builder)
{
foreach ($this->extensions as $extension) {
$this->{"add{$extension}"}($builder);
}
}

/**
* addWithSite removes this scope and includes the specified site
*/
Expand Down Expand Up @@ -67,14 +77,4 @@ protected function addWithSyncSites(BuilderBase $builder)
return $builder->withSites($builder->getModel()->getMultisiteSyncSites());
});
}

/**
* extend the Eloquent query builder.
*/
public function extend(BuilderBase $builder)
{
foreach ($this->extensions as $extension) {
$this->{"add{$extension}"}($builder);
}
}
}
24 changes: 24 additions & 0 deletions src/Database/Scopes/SoftDeleteScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php namespace October\Rain\Database\Scopes;

use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Database\Eloquent\Model as ModelBase;
use Illuminate\Database\Eloquent\Builder as BuilderBase;

/**
* SoftDeleteScope
*
* @package october\database
* @author Alexey Bobkov, Samuel Georges
*/
class SoftDeleteScope extends SoftDeletingScope
{
/**
* apply the scope to a given Eloquent query builder.
*/
public function apply(BuilderBase $builder, ModelBase $model)
{
if ($model->isSoftDeleteEnabled()) {
$builder->whereNull($model->getQualifiedDeletedAtColumn());
}
}
}
26 changes: 18 additions & 8 deletions src/Database/Traits/SoftDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Collection as CollectionBase;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use October\Rain\Database\Scopes\SoftDeleteScope;

/**
* SoftDelete trait for flagging models as deleted instead of actually deleting them.
Expand All @@ -22,7 +22,7 @@ trait SoftDelete
*/
public static function bootSoftDelete()
{
static::addGlobalScope(new SoftDeletingScope);
static::addGlobalScope(new SoftDeleteScope);

static::softDeleted(function($model) {
/**
Expand Down Expand Up @@ -106,17 +106,18 @@ public function forceDelete()
*/
protected function performDeleteOnModel()
{
if ($this->forceDeleting) {
if ($this->forceDeleting || !$this->isSoftDeleteEnabled()) {
$this->performDeleteOnRelations();

$this->setKeysForSaveQuery($this->newQuery()->withTrashed())->forceDelete();

$this->exists = false;
}
else {
$this->performSoftDeleteOnRelations();

$this->performSoftDeleteOnRelations();

$this->runSoftDelete();
$this->runSoftDelete();
}
}

/**
Expand Down Expand Up @@ -248,7 +249,7 @@ public function trashed()
*/
public static function withTrashed()
{
return with(new static)->newQueryWithoutScope(new SoftDeletingScope);
return with(new static)->newQueryWithoutScope(new SoftDeleteScope);
}

/**
Expand All @@ -261,7 +262,7 @@ public static function onlyTrashed()

$column = $instance->getQualifiedDeletedAtColumn();

return $instance->newQueryWithoutScope(new SoftDeletingScope)->whereNotNull($column);
return $instance->newQueryWithoutScope(new SoftDeleteScope)->whereNotNull($column);
}

/**
Expand Down Expand Up @@ -294,6 +295,15 @@ public static function restored($callback)
static::registerModelEvent('restored', $callback);
}

/**
* isSoftDeleteEnabled allows for programmatic toggling
* @return bool
*/
public function isSoftDeleteEnabled()
{
return true;
}

/**
* getDeletedAtColumn gets the name of the "deleted at" column.
* @return string
Expand Down

0 comments on commit cf8b4cb

Please sign in to comment.