From 41ec2bb36739ace42f6cc26453e528bcdb235c75 Mon Sep 17 00:00:00 2001 From: Mo Khosh Date: Sun, 21 Jan 2024 02:51:23 +0330 Subject: [PATCH 1/7] add the necessary config --- config/eloquent-sortable.php | 6 ++++++ 1 file changed, 6 insertions(+) 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, ]; From 1e7b3696a20bc40bfab01663050533e268caf3a9 Mon Sep 17 00:00:00 2001 From: Mo Khosh Date: Sun, 21 Jan 2024 02:52:04 +0330 Subject: [PATCH 2/7] ignore timestamps if config is set and stop ignoring after setting new order --- src/SortableTrait.php | 8 ++++++++ 1 file changed, 8 insertions(+) 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) From 046e523444dfa031daf82d30fe56eeea12afd8fc Mon Sep 17 00:00:00 2001 From: Mo Khosh Date: Sun, 21 Jan 2024 02:52:24 +0330 Subject: [PATCH 3/7] add dummy model with timestamps --- tests/DummyWithTimestamps.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/DummyWithTimestamps.php 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 @@ + Date: Sun, 21 Jan 2024 02:52:47 +0330 Subject: [PATCH 4/7] add set up with timestamp --- tests/TestCase.php | 7 +++++++ 1 file changed, 7 insertions(+) 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(); + }); + } } From 70e6b24d7b3e04d851f0e21c1b10ee964ddf9dc6 Mon Sep 17 00:00:00 2001 From: Mo Khosh Date: Sun, 21 Jan 2024 02:57:39 +0330 Subject: [PATCH 5/7] test default behavior to make sure we haven't broken anything --- tests/SortableTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/SortableTest.php b/tests/SortableTest.php index d5f7068..378dc78 100644 --- a/tests/SortableTest.php +++ b/tests/SortableTest.php @@ -42,6 +42,24 @@ public function it_can_set_a_new_order() } } + /** @test */ + public function it_can_touch_timestamps_when_setting_a_new_order() + { + $this->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_by_custom_column() { From 11c7b490131452a212fff2fb8222b7a47c86ccbd Mon Sep 17 00:00:00 2001 From: Mo Khosh Date: Sun, 21 Jan 2024 02:58:01 +0330 Subject: [PATCH 6/7] test it can set order without touching timestamps --- tests/SortableTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/SortableTest.php b/tests/SortableTest.php index 378dc78..b77ae93 100644 --- a/tests/SortableTest.php +++ b/tests/SortableTest.php @@ -60,6 +60,24 @@ public function it_can_touch_timestamps_when_setting_a_new_order() } } + /** @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() { From 596d99a30c3e6eef2f05b414b90c566467d610d2 Mon Sep 17 00:00:00 2001 From: Mo Khosh Date: Sun, 21 Jan 2024 03:01:33 +0330 Subject: [PATCH 7/7] update readme to reflect config changes --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) 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, ]; ```