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

Dispatch event on sortable update #177

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions src/EloquentModelSortedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\EloquentSortable;

class EloquentModelSortedEvent
{
public string $model;

public function __construct(string $model)
{
$this->model = $model;
}
}
2 changes: 2 additions & 0 deletions src/SortableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public static function setNewOrder($ids, int $startOrder = 1, string $primaryKey
->update([$orderColumnName => $startOrder++]);
}

event(new EloquentModelSortedEvent(static::class));

if (config('eloquent-sortable.ignore_timestamps', false)) {
static::$ignoreTimestampsOn = array_values(array_diff(static::$ignoreTimestampsOn, [static::class]));
}
Expand Down
9 changes: 9 additions & 0 deletions tests/SortableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Spatie\EloquentSortable\Test;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Event;
use Spatie\EloquentSortable\EloquentModelSortedEvent;

class SortableTest extends TestCase
{
Expand Down Expand Up @@ -33,13 +35,20 @@ public function it_can_get_the_highest_order_number_with_trashed_models()
/** @test */
public function it_can_set_a_new_order()
{

Event::fake(EloquentModelSortedEvent::class);

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

Dummy::setNewOrder($newOrder);

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

Event::assertDispatched(EloquentModelSortedEvent::class, function (EloquentModelSortedEvent $event) {
return $event->model === Dummy::class;
});
}

/** @test */
Expand Down