Skip to content

Commit

Permalink
Add ignore timestamp (#169)
Browse files Browse the repository at this point in the history
* add the necessary config

* ignore timestamps if config is set and stop ignoring after setting new order

* add dummy model with timestamps

* add set up with timestamp

* test default behavior to make sure we haven't broken anything

* test it can set order without touching timestamps

* update readme to reflect config changes
  • Loading branch information
mokhosh authored Jan 22, 2024
1 parent eef2103 commit 4e4d017
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
```

Expand Down
6 changes: 6 additions & 0 deletions config/eloquent-sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
8 changes: 8 additions & 0 deletions src/SortableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions tests/DummyWithTimestamps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\EloquentSortable\Test;

use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class DummyWithTimestamps extends Model implements Sortable
{
use SortableTrait;

protected $table = 'dummies';
protected $guarded = [];
}
36 changes: 36 additions & 0 deletions tests/SortableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ 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_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()
{
Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
}

0 comments on commit 4e4d017

Please sign in to comment.