Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder soft-deleted records #32

Merged
merged 2 commits into from
Nov 20, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions 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 @@ -48,6 +49,32 @@ public function scopeOrdered(Builder $query, string $direction = 'asc')
return $query->orderBy($this->determineOrderColumnName(), $direction);
}

/**
* Query records which are ordered above the given position.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $position
*
* @return \Illuminate\Database\Query\Builder
*/
public function scopeAbove(Builder $query, int $position)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like scopes being added that solely exist for internal working and will not be used publicly. Could you remove these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I thought these could be handy for public use, but I'll remove them right way.

{
return $query->where($this->determineOrderColumnName(), '<', $position);
}

/**
* Query records which are ordered below the given position.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $position
*
* @return \Illuminate\Database\Query\Builder
*/
public function scopeBelow(Builder $query, int $position)
{
return $query->where($this->determineOrderColumnName(), '>', $position);
}

/**
* This function reorders the records: the record with the first id in the array
* will get order 1, the record with the second it will get order 2, ...
Expand All @@ -69,7 +96,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 Expand Up @@ -107,7 +136,7 @@ public function moveOrderDown()

$swapWithModel = static::limit(1)
->ordered()
->where($orderColumnName, '>', $this->$orderColumnName)
->below($this->$orderColumnName)
->first();

if (! $swapWithModel) {
Expand All @@ -128,7 +157,7 @@ public function moveOrderUp()

$swapWithModel = static::limit(1)
->ordered('desc')
->where($orderColumnName, '<', $this->$orderColumnName)
->above($this->$orderColumnName)
->first();

if (! $swapWithModel) {
Expand Down Expand Up @@ -217,7 +246,7 @@ public function moveToEnd()
$this->save();

static::where($this->getKeyName(), '!=', $this->id)
->where($orderColumnName, '>', $oldOrder)
->below($oldOrder)
->decrement($orderColumnName);

return $this;
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();
});
}
}