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

Add ignore timestamp #169

Merged
merged 7 commits into from
Jan 22, 2024
Merged
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
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();
});
}
}