Skip to content

Commit 5c9f953

Browse files
authored
Merge pull request #5 from efureev/add-sorting-restrictions
Add sorting restrictions and rework Sortable booting
2 parents d2a479e + b01a0b1 commit 5c9f953

20 files changed

+381
-114
lines changed

src/Sorting/Model/Sortable.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ trait Sortable
3232
*
3333
* @return void
3434
*/
35-
protected static function defaultSortableBooting()
35+
protected static function bootSortable()
3636
{
3737
if (static::$sortingGlobalScope) {
3838
static::addGlobalScope(
@@ -91,14 +91,20 @@ protected function normalizeSortingPosition($sortingPosition)
9191
*/
9292
protected function formDefaultSQL(): string
9393
{
94+
$where = '';
95+
$defaultSortingRestrictions = $this->getDefaultSortingRestrictionsSql();
96+
if ($defaultSortingRestrictions) {
97+
$where .= "WHERE {$defaultSortingRestrictions}";
98+
}
99+
94100
return <<<SQL
95101
(SELECT
96102
CASE
97103
WHEN MAX(sorting_position) IS NOT NULL
98104
THEN MAX(sorting_position) + 1
99105
ELSE 1
100106
END
101-
FROM {$this->getTable()})
107+
FROM {$this->getTable()} {$where})
102108
SQL;
103109
}
104110

@@ -136,6 +142,15 @@ public function downInSorting(int $steps)
136142
return $this;
137143
}
138144

145+
/**
146+
* @param Builder $query
147+
* @return Builder
148+
*/
149+
protected function forSortingRestrictions(Builder $query): Builder
150+
{
151+
return $query;
152+
}
153+
139154
/**
140155
* @param int $oldPosition
141156
* @param int $newPosition
@@ -173,8 +188,10 @@ function (Blueprint $blueprint) {
173188
*/
174189
protected function incrementInReorder(int $oldPosition, int $newPosition): void
175190
{
176-
static::where('sorting_position', '>=', $newPosition)
177-
->where('sorting_position', '<', $oldPosition)
191+
$this->forSortingRestrictions(
192+
static::where('sorting_position', '>=', $newPosition)
193+
->where('sorting_position', '<', $oldPosition)
194+
)
178195
->increment('sorting_position');
179196
}
180197

@@ -184,8 +201,18 @@ protected function incrementInReorder(int $oldPosition, int $newPosition): void
184201
*/
185202
protected function decrementInReorder(int $oldPosition, int $newPosition): void
186203
{
187-
static::where('sorting_position', '<=', $newPosition)
188-
->where('sorting_position', '>', $oldPosition)
204+
$this->forSortingRestrictions(
205+
static::where('sorting_position', '<=', $newPosition)
206+
->where('sorting_position', '>', $oldPosition)
207+
)
189208
->decrement('sorting_position');
190209
}
210+
211+
/**
212+
* @return string
213+
*/
214+
protected function getDefaultSortingRestrictionsSql(): string
215+
{
216+
return '';
217+
}
191218
}

tests/Functional/CasterTraitTest.php

100644100755
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ public function testCreateAndGetWithFillArrayParams(): void
8989
'str' => 'string',
9090
'str_empty' => 'string empty',
9191
'int' => 7,
92-
'config' => [
93-
'key' => 2,
94-
],
92+
'config' => ['key' => 2],
9593
'params' => [
9694
'key' => 1,
9795
'config' => ['test' => 'value'],
@@ -140,9 +138,7 @@ public function testCreateAndGetWithClassParams(): void
140138
[
141139
'str_empty' => null,
142140
'config' => [
143-
'key' => [
144-
'key' => 2,
145-
],
141+
'key' => ['key' => 2],
146142
],
147143
'params' => new Params(
148144
[

tests/Functional/PgArrayTraitTest.php

100644100755
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Php\Support\Laravel\Tests\Functional;
66

7-
87
use Php\Support\Laravel\Tests\TestClasses\Models\PgArrayModel;
98

109
class PgArrayTraitTest extends AbstractFunctionalTestCase
@@ -70,5 +69,4 @@ protected function setUp(): void
7069

7170
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
7271
}
73-
7472
}

tests/Functional/UuidTraitTest.php

100644100755
File mode changed.

tests/Rules/AuthorizedTest.php

100644100755
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Php\Support\Laravel\Tests\TestClasses\Models\TestModel;
1313
use Php\Support\Laravel\Tests\TestClasses\Policies\TestModelPolicy;
1414

15-
1615
class AuthorizedTest extends AbstractTestCase
1716
{
1817
protected function setUp(): void
@@ -80,9 +79,7 @@ public function it_will_return_false_if_the_gate_returns_false()
8079

8180
$user = factory(User::class)->create();
8281
$model = factory(TestModel::class)->create(
83-
[
84-
'user_id' => 2,
85-
]
82+
['user_id' => 2]
8683
);
8784

8885
$this->assertFalse($rule->passes('attribute', '1'));
@@ -92,9 +89,7 @@ public function it_will_return_false_if_the_gate_returns_false()
9289
public function it_passes_attribute_ability_and_class_name_to_the_validation_message()
9390
{
9491
Lang::addLines(
95-
[
96-
'messages.authorized' => ':attribute :ability and :className',
97-
],
92+
['messages.authorized' => ':attribute :ability and :className'],
9893
Lang::getLocale(),
9994
'laravelSupport'
10095
);

tests/Rules/DelimitedTest.php

100644100755
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Php\Support\Laravel\Rules\Delimited;
88
use Php\Support\Laravel\Tests\AbstractTestCase;
99

10-
1110
class DelimitedTest extends AbstractTestCase
1211
{
1312
/**

tests/Sorting/Database/SortableTest.php

100644100755
File mode changed.

0 commit comments

Comments
 (0)