Skip to content

Commit

Permalink
Fix deleted event in PositionObserver to correctly handle position re…
Browse files Browse the repository at this point in the history
…calculate
  • Loading branch information
jakubdibala authored and pionl committed Jul 8, 2021
1 parent 89043bf commit d1c5d7d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/PositionObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function saving($model)
if ($model->isPositionUpdateDisabled() === false) {
// Get the position for current and old value
$position = $model->getPosition();

// Prevent modifying position column when updating and position columns has not changed
if ($model->exists === true && $model->isDirty($model->getPositionColumn()) === false) {
return;
Expand All @@ -70,7 +70,7 @@ public function saving($model)
*
* @param Model|PositionTrait $model
*/
public function deleting($model)
public function deleted($model)
{
if ($model->isPositionUpdateDisabled() === false) {
// Get the old position
Expand Down
4 changes: 4 additions & 0 deletions src/Query/LastPositionQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function runQuery($query)
$lastPosition = $query->max($this->model()->getPositionColumn()) ?: 0;

if (empty($this->oldPosition) === false) {
if ($this->oldPosition === $this->model->getPosition() && $lastPosition < $this->model->getPosition()) {
return;
}

(new MoveQuery($this->model, $lastPosition, $this->oldPosition))->run();
} else if ($this->oldPosition === null || $lastPosition != $this->oldPosition) {
// Check if the last position is not same as original position - the same object
Expand Down

0 comments on commit d1c5d7d

Please sign in to comment.