Skip to content

Commit 1cd4c3c

Browse files
authored
Fix position grouping (#23)
* Update README.md * wip * fix grouping
1 parent 0ec689c commit 1cd4c3c

File tree

3 files changed

+69
-54
lines changed

3 files changed

+69
-54
lines changed

src/PositionObserver.php

+1-21
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,11 @@ public function saving(Model $model): void
104104
*/
105105
protected function assignPosition(Model $model): void
106106
{
107-
if ($this->shouldSetPosition($model)) {
107+
if ($model->getAttribute($model->getPositionColumn()) === null) {
108108
$model->setPosition($this->getNextPosition($model));
109109
}
110110
}
111111

112-
/**
113-
* Determine if a position should be set for the model.
114-
*
115-
* @param Model|HasPosition $model
116-
*/
117-
protected function shouldSetPosition(Model $model): bool
118-
{
119-
$positionColumn = $model->getPositionColumn();
120-
121-
if ($model->isDirty($positionColumn)) {
122-
return false;
123-
}
124-
125-
if ($model->getAttribute($positionColumn) === null) {
126-
return true;
127-
}
128-
129-
return $this->isGroupChanging($model);
130-
}
131-
132112
/**
133113
* Get the next position for the model.
134114
*

tests/GroupTest.php

+65-33
Original file line numberDiff line numberDiff line change
@@ -101,55 +101,56 @@ public function it_syncs_positions_when_group_is_changed(): void
101101
$category = CategoryFactory::new()->create();
102102
$anotherCategory = CategoryFactory::new()->create();
103103

104-
$books = BookFactory::new()
104+
$book = BookFactory::new()
105105
->forCategory($category)
106-
->createMany(3);
107-
108-
static::assertSame(0, $books[0]->getPosition());
109-
static::assertSame(1, $books[1]->getPosition());
110-
static::assertSame(2, $books[2]->getPosition());
106+
->create();
111107

112-
BookFactory::new()
108+
$anotherBook = BookFactory::new()
113109
->forCategory($anotherCategory)
114110
->create();
115111

116-
$books[1]->category()
117-
->associate($anotherCategory)
118-
->save();
112+
$book->update([
113+
'category_id' => $anotherCategory->id,
114+
]);
119115

120-
static::assertSame(0, $books[0]->fresh()->getPosition());
121-
static::assertSame(1, $books[1]->fresh()->getPosition());
122-
static::assertSame(1, $books[2]->fresh()->getPosition());
116+
static::assertSame(0, $book->fresh()->getPosition());
117+
static::assertSame(1, $anotherBook->fresh()->getPosition());
123118
}
124119

125120
/**
126121
* @test
127122
*/
128-
public function it_sets_next_position_correctly_when_group_is_changed(): void
123+
public function it_updates_group_along_with_position_correctly(): void
129124
{
130125
$category = CategoryFactory::new()->create();
131126
$anotherCategory = CategoryFactory::new()->create();
132127

133-
$book = BookFactory::new()
128+
$books = BookFactory::new()
134129
->forCategory($category)
135-
->create();
130+
->createMany(3);
136131

137-
$anotherBook = BookFactory::new()
132+
$anotherBooks = BookFactory::new()
138133
->forCategory($anotherCategory)
139-
->create();
134+
->createMany(3);
140135

141-
$book->category()
142-
->associate($anotherCategory)
143-
->save();
136+
$books[0]->update([
137+
'category_id' => $anotherCategory->id,
138+
'position' => 1,
139+
]);
144140

145-
static::assertSame(0, $anotherBook->fresh()->getPosition());
146-
static::assertSame(1, $book->fresh()->getPosition());
141+
static::assertSame(0, $books[1]->fresh()->position);
142+
static::assertSame(1, $books[2]->fresh()->position);
143+
144+
static::assertSame(0, $anotherBooks[0]->fresh()->position);
145+
static::assertSame(1, $books[0]->fresh()->position);
146+
static::assertSame(2, $anotherBooks[1]->fresh()->position);
147+
static::assertSame(3, $anotherBooks[2]->fresh()->position);
147148
}
148149

149150
/**
150151
* @test
151152
*/
152-
public function it_updates_group_along_with_position_correctly(): void
153+
public function it_moves_model_at_start_of_sequence_of_another_group(): void
153154
{
154155
$category = CategoryFactory::new()->create();
155156
$anotherCategory = CategoryFactory::new()->create();
@@ -162,20 +163,50 @@ public function it_updates_group_along_with_position_correctly(): void
162163
->forCategory($anotherCategory)
163164
->createMany(3);
164165

165-
$books[0]->category()
166-
->associate($anotherCategory)
167-
->setPosition(1)
168-
->save();
166+
$books[0]->update([
167+
'category_id' => $anotherCategory->id,
168+
'position' => 0,
169+
]);
169170

170171
static::assertSame(0, $books[1]->fresh()->position);
171172
static::assertSame(1, $books[2]->fresh()->position);
172173

173-
static::assertSame(0, $anotherBooks[0]->fresh()->position);
174-
static::assertSame(1, $books[0]->fresh()->position);
174+
static::assertSame(0, $books[0]->fresh()->position);
175+
static::assertSame(1, $anotherBooks[0]->fresh()->position);
175176
static::assertSame(2, $anotherBooks[1]->fresh()->position);
176177
static::assertSame(3, $anotherBooks[2]->fresh()->position);
177178
}
178179

180+
/**
181+
* @test
182+
*/
183+
public function it_moves_model_at_end_of_sequence_of_another_group(): void
184+
{
185+
$category = CategoryFactory::new()->create();
186+
$anotherCategory = CategoryFactory::new()->create();
187+
188+
$books = BookFactory::new()
189+
->forCategory($category)
190+
->createMany(3);
191+
192+
$anotherBooks = BookFactory::new()
193+
->forCategory($anotherCategory)
194+
->createMany(3);
195+
196+
$books[0]->update([
197+
'category_id' => $anotherCategory->id,
198+
'position' => -1,
199+
]);
200+
201+
static::assertSame(0, $books[1]->fresh()->position);
202+
static::assertSame(1, $books[2]->fresh()->position);
203+
204+
static::assertSame(0, $anotherBooks[0]->fresh()->position);
205+
static::assertSame(1, $anotherBooks[1]->fresh()->position);
206+
static::assertSame(2, $anotherBooks[2]->fresh()->position);
207+
static::assertSame(3, $books[0]->fresh()->position);
208+
}
209+
179210
/**
180211
* @test
181212
*/
@@ -190,9 +221,10 @@ public function it_executes_3_queries_to_move_model_at_end_of_sequence_of_anothe
190221

191222
Category::query()->getConnection()->enableQueryLog();
192223

193-
$book->category()
194-
->associate($anotherCategory)
195-
->save();
224+
$book->update([
225+
'category_id' => $anotherCategory->id,
226+
'position' => -1,
227+
]);
196228

197229
self::assertCount(3, Category::query()->getConnection()->getQueryLog());
198230
}

tests/TestCase.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Nevadskiy\Position\Tests;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Orchestra\Testbench\TestCase as OrchestraTestCase;
67

78
class TestCase extends OrchestraTestCase
@@ -16,6 +17,8 @@ protected function setUp(): void
1617
$this->loadMigrationsFrom(__DIR__ . '/App/Migrations');
1718

1819
$this->artisan('migrate', ['--database' => 'testbench'])->run();
20+
21+
Model::unguard();
1922
}
2023

2124
/**

0 commit comments

Comments
 (0)