Skip to content

Commit

Permalink
[MOD] add modifyQuery with a callback instead of withoutGlobalScope f…
Browse files Browse the repository at this point in the history
…or setting a new order
  • Loading branch information
AbdelrahmanBl committed Dec 11, 2023
1 parent 58ff952 commit 1790384
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/SortableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function scopeOrdered(Builder $query, string $direction = 'asc')
return $query->orderBy($this->determineOrderColumnName(), $direction);
}

public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null, array $withoutGlobalScopes = []): void
public static function setNewOrder($ids, int $startOrder = 1, string $primaryKeyColumn = null, callable $modifyQuery = null): void
{
if (! is_array($ids) && ! $ids instanceof ArrayAccess) {
throw new InvalidArgumentException('You must pass an array or ArrayAccess object to setNewOrder');
Expand All @@ -56,7 +56,9 @@ public static function setNewOrder($ids, int $startOrder = 1, string $primaryKey

foreach ($ids as $id) {
static::withoutGlobalScope(SoftDeletingScope::class)
->withoutGlobalScopes($withoutGlobalScopes)
->when(is_callable($modifyQuery), function($query) use ($modifyQuery) {
return $modifyQuery($query);
})
->where($primaryKeyColumn, $id)
->update([$orderColumnName => $startOrder++]);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/SortableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public function it_can_set_new_order_without_global_scopes_models()

$newOrder = Collection::make(Dummy::all()->pluck('id'))->shuffle()->toArray();

DummyWithGlobalScope::setNewOrder($newOrder, 1, null, ['ActiveScope']);
DummyWithGlobalScope::setNewOrder($newOrder, 1, null, function($query) {
$query->withoutGlobalScope('ActiveScope');
});

foreach (Dummy::orderBy('order_column')->get() as $i => $dummy) {
$this->assertEquals($newOrder[$i], $dummy->id);
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\EloquentSortable\Test;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase as Orchestra;

abstract class TestCase extends Orchestra
Expand Down Expand Up @@ -31,7 +32,7 @@ protected function getPackageProviders($app)
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.default', 'mysql');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => ':memory:',
Expand All @@ -41,6 +42,8 @@ protected function getEnvironmentSetUp($app)

protected function setUpDatabase()
{
Schema::dropIfExists('dummies');

$this->app['db']->connection()->getSchemaBuilder()->create('dummies', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
Expand Down

0 comments on commit 1790384

Please sign in to comment.