Skip to content

Commit

Permalink
Merge pull request #30 from tipoff/pdbreen/feature/scope-by-status
Browse files Browse the repository at this point in the history
Add scopeByStatus to HasStatuses trait
  • Loading branch information
drewroberts authored Mar 10, 2021
2 parents 6e8adda + 85cee3f commit 93b369d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/StatusesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Tipoff\Statuses\Models\Status;
use Tipoff\Statuses\Models\StatusRecord;
use Tipoff\Statuses\Policies\StatusRecordPolicy;
use Tipoff\Statuses\Policies\StatusPolicy;
use Tipoff\Statuses\Policies\StatusRecordPolicy;
use Tipoff\Support\TipoffPackage;
use Tipoff\Support\TipoffServiceProvider;

Expand Down
17 changes: 17 additions & 0 deletions src/Traits/HasStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tipoff\Statuses\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Tipoff\Statuses\Exceptions\UnknownStatusException;
use Tipoff\Statuses\Models\Status;
Expand All @@ -18,6 +19,22 @@ trait HasStatuses
// When false, exception occurs if unknown status value is used
protected bool $dynamicStatusCreation = false;

/**
* Return all records whose current/latest status value matches the status provided
*/
public function scopeByStatus(Builder $query, Status $status): Builder
{
return $query->whereHas('statusRecords', function ($query) use ($status) {
$query->where('id', function ($sub) use ($status) {
$sub->from('status_records')
->selectRaw('max(id)')
->whereColumn('statusable_type', $this->getMorphClass())
->whereColumn('statusable_id', $this->getTable() . '.' . $this->getKeyName())
->whereColumn('type', $status->type);
})->where('status_id', $status->id);
});
}

public function getStatusHistory(?string $type = null): Collection
{
return $this->statusRecords()
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Traits/HasStatusesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,36 @@ public function get_status_history()
$this->assertEquals(['2', '3', '2', '3'], $history);
}

/** @test */
public function scope_by_status()
{
TestModel::createTable();

$this->actingAs(User::factory()->create());

Status::publishStatuses('type', ['1', '2', '3', '4', '5']);

TestModel::factory()->count(20)->create()->each(function (TestModel $model, int $idx) {
// Generate history
$model->setStatus('1', 'type');
if ($idx >= 5) {
$model->setStatus('2', 'type');
if ($idx >= 10) {
$model->setStatus('3', 'type');
if ($idx >= 15) {
$model->setStatus('4', 'type');
}
}
}
});

$this->assertEquals(5, TestModel::query()->byStatus(Status::findStatus('type', '1'))->count());
$this->assertEquals(5, TestModel::query()->byStatus(Status::findStatus('type', '2'))->count());
$this->assertEquals(5, TestModel::query()->byStatus(Status::findStatus('type', '3'))->count());
$this->assertEquals(5, TestModel::query()->byStatus(Status::findStatus('type', '4'))->count());
$this->assertEquals(0, TestModel::query()->byStatus(Status::findStatus('type', '5'))->count());
}

/** @test */
public function set_unknown_status()
{
Expand Down

0 comments on commit 93b369d

Please sign in to comment.