Skip to content

Commit

Permalink
Reorder soft-deleted records (#32)
Browse files Browse the repository at this point in the history
* Reorder soft-deleted records

Adds the ability to reorder models that use the `SoftDeletes` trait.

Fixes #31

* Remove above and below scopes
  • Loading branch information
sebdesign authored and freekmurze committed Nov 20, 2016
1 parent 9d1ad75 commit b5cf0fa
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/SortableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ArrayAccess;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use InvalidArgumentException;

trait SortableTrait
Expand Down Expand Up @@ -69,7 +70,9 @@ public static function setNewOrder($ids, int $startOrder = 1)
$primaryKeyColumn = $model->getKeyName();

foreach ($ids as $id) {
static::where($primaryKeyColumn, $id)->update([$orderColumnName => $startOrder++]);
static::withoutGlobalScope(SoftDeletingScope::class)
->where($primaryKeyColumn, $id)
->update([$orderColumnName => $startOrder++]);
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/DummyWithSoftDeletes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\EloquentSortable\Test;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class DummyWithSoftDeletes extends Model implements Sortable
{
use SoftDeletes, SortableTrait;

protected $table = 'dummies';
protected $guarded = [];
public $timestamps = false;
}
44 changes: 44 additions & 0 deletions tests/SortableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public function it_can_get_the_highest_order_number()
$this->assertEquals(Dummy::all()->count(), (new Dummy())->getHighestOrderNumber());
}

/** @test */
public function it_can_get_the_highest_order_number_with_trashed_models()
{
$this->setUpSoftDeletes();

DummyWithSoftDeletes::first()->delete();

$this->assertEquals(DummyWithSoftDeletes::withTrashed()->count(), (new DummyWithSoftDeletes())->getHighestOrderNumber());
}

/** @test */
public function it_can_set_a_new_order()
{
Expand All @@ -44,6 +54,40 @@ public function it_can_set_a_new_order_from_collection()
}
}

/** @test */
public function it_can_set_a_new_order_with_trashed_models()
{
$this->setUpSoftDeletes();

$dummies = DummyWithSoftDeletes::all();

$dummies->random()->delete();

$newOrder = Collection::make($dummies->pluck('id'))->shuffle();

DummyWithSoftDeletes::setNewOrder($newOrder);

foreach (DummyWithSoftDeletes::withTrashed()->orderBy('order_column')->get() as $i => $dummy) {
$this->assertEquals($newOrder[$i], $dummy->id);
}
}

/** @test */
public function it_can_set_a_new_order_without_trashed_models()
{
$this->setUpSoftDeletes();

DummyWithSoftDeletes::first()->delete();

$newOrder = Collection::make(DummyWithSoftDeletes::pluck('id'))->shuffle();

DummyWithSoftDeletes::setNewOrder($newOrder);

foreach (DummyWithSoftDeletes::orderBy('order_column')->get() as $i => $dummy) {
$this->assertEquals($newOrder[$i], $dummy->id);
}
}

/** @test */
public function it_will_determine_to_sort_when_creating_if_sortable_attribute_does_not_exist()
{
Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ protected function setUpDatabase()
Dummy::create(['name' => $i]);
});
}

protected function setUpSoftDeletes()
{
$this->app['db']->connection()->getSchemaBuilder()->table('dummies', function (Blueprint $table) {
$table->softDeletes();
});
}
}

0 comments on commit b5cf0fa

Please sign in to comment.