Skip to content

Commit

Permalink
Merge pull request #104 from tal7aouy/tal7aouy
Browse files Browse the repository at this point in the history
getStatusNames
  • Loading branch information
freekmurze authored May 25, 2023
2 parents 6d7c676 + ce0c0f6 commit 56beef2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ You can also get latest status of a given name:
```php
$model->latestStatus('pending'); // returns an instance of `Spatie\ModelStatus\Status` that has the name `pending`
```
Get all available status names for the model.

```php
$statusNames = $model->getStatusNames(); // returns a collection of all available status names.
```

The following examples will return statusses of type `status 1` or `status 2`, whichever is latest.

Expand Down
12 changes: 12 additions & 0 deletions src/HasStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use \Illuminate\Support\Collection;
use Spatie\ModelStatus\Events\StatusUpdated;
use Spatie\ModelStatus\Exceptions\InvalidStatus;

Expand Down Expand Up @@ -178,4 +179,15 @@ public function __get($key): mixed

return parent::__get($key);
}
/*
* Get all available status names for the model.
*
* @return \Illuminate\Support\Collection
*/
public function getStatusNames(): Collection
{
$statusModel = app($this->getStatusModelClassName());

return $statusModel->pluck('name');
}
}
27 changes: 27 additions & 0 deletions tests/HasStatusesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,30 @@
expect($model->status)->toEqual('pending')
->and($model->status()->model_id)->toEqual($model->id);
});

it('returns all available status names', function () {

$model = TestModel::create(['name' => 'model1']);
// Set up some test statuses
$model->setStatus('status1');
$model->setStatus('status2');
$model->setStatus('status3');

// Get the status names
$statusNames = $model->getStatusNames();

// Assert the returned status names
expect($statusNames)->toContain('status1')
->toContain('status2')
->toContain('status3');
});

it('returns an empty collection when there are no statuses', function () {
$model = TestModel::create(['name' => 'model1']);

// Get the status names
$statusNames = $model->getStatusNames();

// Assert the returned status names
expect($statusNames)->toBeEmpty();
});

0 comments on commit 56beef2

Please sign in to comment.