diff --git a/README.md b/README.md index 3b0d140..8d0692e 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,12 @@ return [ * will automatically assign the highest order number to a new model */ 'sort_when_creating' => true, + + /* + * Define if the timestamps should be ignored when sorting. + * When true, updated_at will not be updated when using setNewOrder + */ + 'ignore_timestamps' => false, ]; ``` diff --git a/config/eloquent-sortable.php b/config/eloquent-sortable.php index 1bfd55a..f05614e 100644 --- a/config/eloquent-sortable.php +++ b/config/eloquent-sortable.php @@ -11,4 +11,10 @@ * When true, the package will automatically assign the highest order number to a new model */ 'sort_when_creating' => true, + + /* + * Define if the timestamps should be ignored when sorting. + * When true, updated_at will not be updated when using setNewOrder + */ + 'ignore_timestamps' => false, ]; diff --git a/src/SortableTrait.php b/src/SortableTrait.php index 92ac3dd..9b02e45 100644 --- a/src/SortableTrait.php +++ b/src/SortableTrait.php @@ -54,6 +54,10 @@ public static function setNewOrder($ids, int $startOrder = 1, string $primaryKey $primaryKeyColumn = $model->getKeyName(); } + if (config('eloquent-sortable.ignore_timestamps', false)) { + static::$ignoreTimestampsOn = array_values(array_merge(static::$ignoreTimestampsOn, [static::class])); + } + foreach ($ids as $id) { static::withoutGlobalScope(SoftDeletingScope::class) ->when(is_callable($modifyQuery), function ($query) use ($modifyQuery) { @@ -62,6 +66,10 @@ public static function setNewOrder($ids, int $startOrder = 1, string $primaryKey ->where($primaryKeyColumn, $id) ->update([$orderColumnName => $startOrder++]); } + + if (config('eloquent-sortable.ignore_timestamps', false)) { + static::$ignoreTimestampsOn = array_values(array_diff(static::$ignoreTimestampsOn, [static::class])); + } } public static function setNewOrderByCustomColumn(string $primaryKeyColumn, $ids, int $startOrder = 1) diff --git a/tests/DummyWithTimestamps.php b/tests/DummyWithTimestamps.php new file mode 100644 index 0000000..6e5fb99 --- /dev/null +++ b/tests/DummyWithTimestamps.php @@ -0,0 +1,15 @@ +setUpTimestamps(); + DummyWithTimestamps::query()->update(['updated_at' => now()]); + $originalTimestamps = DummyWithTimestamps::all()->pluck('updated_at'); + + $this->travelTo(now()->addMinute()); + + config()->set('eloquent-sortable.ignore_timestamps', false); + $newOrder = Collection::make(DummyWithTimestamps::all()->pluck('id'))->shuffle()->toArray(); + DummyWithTimestamps::setNewOrder($newOrder); + + foreach (DummyWithTimestamps::orderBy('order_column')->get() as $i => $dummy) { + $this->assertNotEquals($originalTimestamps[$i], $dummy->updated_at); + } + } + + /** @test */ + public function it_can_set_a_new_order_without_touching_timestamps() + { + $this->setUpTimestamps(); + DummyWithTimestamps::query()->update(['updated_at' => now()]); + $originalTimestamps = DummyWithTimestamps::all()->pluck('updated_at'); + + $this->travelTo(now()->addMinute()); + + config()->set('eloquent-sortable.ignore_timestamps', true); + $newOrder = Collection::make(DummyWithTimestamps::all()->pluck('id'))->shuffle()->toArray(); + DummyWithTimestamps::setNewOrder($newOrder); + + foreach (DummyWithTimestamps::orderBy('order_column')->get() as $i => $dummy) { + $this->assertEquals($originalTimestamps[$i], $dummy->updated_at); + } + } + /** @test */ public function it_can_set_a_new_order_by_custom_column() { diff --git a/tests/TestCase.php b/tests/TestCase.php index 3c92594..a606fd0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -69,4 +69,11 @@ protected function setUpIsActiveFieldForGlobalScope() $table->boolean('is_active')->default(false); }); } + + protected function setUpTimestamps() + { + $this->app['db']->connection()->getSchemaBuilder()->table('dummies', function (Blueprint $table) { + $table->timestamps(); + }); + } }