Skip to content

Commit 4443bde

Browse files
committed
Refactor state management to use StateHistory model and update README and migration files accordingly
1 parent cb3acfd commit 4443bde

File tree

8 files changed

+13
-110
lines changed

8 files changed

+13
-110
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A Laravel package for managing **enum-based model states** with enforced transit
66

77
- **Native PHP Enums** (PHP 8.2+)
88
- **Enforced Transitions** with a pluggable state machine
9-
- **Automatic History** with metadata & correlation IDs
9+
- **Automatic History** with metadata
1010
- **Smart Casting** of historical `from`/`to` values (enums, dates, primitives, custom casts)
1111
- **Atomic Operations** – state change + history in one transaction
1212
- **Current State Columns** (`current_{field}`) for indexing & querying
@@ -203,7 +203,7 @@ Config (`config/state-history.php`):
203203
return [
204204
'use_current_columns' => true,
205205
'prefix' => 'current_',
206-
'model' => \App\Models\CustomModelState::class,
206+
'model' => \App\Models\CustomStateHistory::class,
207207
];
208208
```
209209

config/state-history.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
|--------------------------------------------------------------------------
4444
|
4545
| The model class to use for state history records. This should be a class
46-
| that extends the base ModelState model.
46+
| that extends the base StateHistory model.
4747
|
4848
*/
4949

50-
'model' => env('STATE_HISTORY_MODEL', \NathanDunn\StateHistory\Models\ModelState::class),
50+
'model' => env('STATE_HISTORY_MODEL', \NathanDunn\StateHistory\Models\StateHistory::class),
5151
];

database/migrations/create_model_states_table.php.stub renamed to database/migrations/create_state_histories_table.php.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ return new class extends Migration
1212
*/
1313
public function up(): void
1414
{
15-
Schema::create('model_states', function (Blueprint $table) {
15+
Schema::create('state_histories', function (Blueprint $table) {
1616
$table->id();
1717
$table->morphs('model');
1818
$table->string('field');
@@ -36,6 +36,6 @@ return new class extends Migration
3636
*/
3737
public function down(): void
3838
{
39-
Schema::dropIfExists('model_states');
39+
Schema::dropIfExists('state_histories');
4040
}
4141
};

src/Models/ModelState.php

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/StateHistoryServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function boot(): void
2626
], 'config');
2727

2828
$this->publishes([
29-
__DIR__ . '/../database/migrations/create_model_states_table.php.stub' => database_path('migrations/' . date('Y_m_d_His') . '_create_model_states_table.php'),
29+
__DIR__ . '/../database/migrations/create_state_histories_table.php.stub' => database_path('migrations/' . date('Y_m_d_His') . '_create_state_histories_table.php'),
3030
], 'migrations');
3131
}
3232
}

src/StateManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use NathanDunn\StateHistory\Events\StateTransitioning;
1515
use NathanDunn\StateHistory\Exceptions\InvalidStateTransitionException;
1616
use NathanDunn\StateHistory\Exceptions\StateTransitionBlockedException;
17-
use NathanDunn\StateHistory\Models\ModelState;
17+
use NathanDunn\StateHistory\Models\StateHistory;
1818
use NathanDunn\StateHistory\Support\StateMachineConfig;
1919

2020
class StateManager
@@ -117,7 +117,7 @@ protected function getCurrentState(Model $model, string $field): ?string
117117
}
118118
}
119119

120-
$latestState = app(ModelState::class)
120+
$latestState = app(StateHistory::class)
121121
->where('model_type', get_class($model))
122122
->where('model_id', $model->getKey())
123123
->where('field', $field)

src/Traits/HasState.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Database\Eloquent\Relations\MorphMany;
88
use Illuminate\Support\Facades\Schema;
99
use NathanDunn\StateHistory\Exceptions\NoStateMachineConfiguredException;
10-
use NathanDunn\StateHistory\Models\ModelState;
10+
use NathanDunn\StateHistory\Models\StateHistory;
1111
use NathanDunn\StateHistory\StateManager;
1212
use NathanDunn\StateHistory\Support\StateMachineConfig;
1313

@@ -65,7 +65,7 @@ public function states(string $field): MorphMany
6565
/**
6666
* Get the latest state for a specific field.
6767
*/
68-
public function latestState(string $field): ?ModelState
68+
public function latestState(string $field): ?StateHistory
6969
{
7070
return $this->states($field)->first();
7171
}

tests/Feature/HasStateTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private function createTables(): void
3737
$table->timestamps();
3838
});
3939

40-
$this->app['db']->connection()->getSchemaBuilder()->create('model_states', function (Blueprint $table) {
40+
$this->app['db']->connection()->getSchemaBuilder()->create('state_histories', function (Blueprint $table) {
4141
$table->id();
4242
$table->string('model_type');
4343
$table->unsignedBigInteger('model_id');
@@ -269,7 +269,7 @@ public function it_uses_config_based_model_resolution()
269269
{
270270
// Test that the package uses the configured model class
271271
$this->assertEquals(
272-
\NathanDunn\StateHistory\Models\ModelState::class,
272+
\NathanDunn\StateHistory\Models\StateHistory::class,
273273
config('state-history.model')
274274
);
275275

0 commit comments

Comments
 (0)