Skip to content

Commit

Permalink
Added snake_case() and camelCase() method for state machine field
Browse files Browse the repository at this point in the history
  • Loading branch information
asantibanez committed Dec 22, 2020
1 parent e0ecafb commit 3ac7509
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-eloquent-state-machines` will be documented in this file

## v2.2.1 - 2020-12-22

- Added `snake_case()` and `camelCase()` method for state machine field.

## v2.2.0 - 2020-12-21

- Added macros on query builder to interact with `state_history`
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ one method per each field mapped in `$stateMachines`. Eg.

For
```php
'status' => StatusStateMachine::class
'status' => StatusStateMachine::class,
'fulfillment_status' => FulfillmentStatusStateMachine::class
```

We will have an accompanying method
```php
status()
status();
fulfillment_status(); // or fulfillmentStatus()
```

with which we can use to check our current state, history and apply transitions.
Expand Down
6 changes: 5 additions & 1 deletion src/Traits/HasStateMachines.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ public static function bootHasStateMachines()

collect($model->stateMachines)
->each(function ($_, $field) use ($model) {
MacroableModels::addMacro(static::class, $field, function () use ($field) {
$stateMachine = new $this->stateMachines[$field]($field, $this);
return new State($this->{$stateMachine->field}, $stateMachine);
});

$camelField = Str::of($field)->camel();

MacroableModels::addMacro(static::class, $camelField, function () use ($field) {
$stateMachine = new $this->stateMachines[$field]($field, $this);
return new State($this->{$stateMachine->field}, $stateMachine);
});


$studlyField = Str::of($field)->studly();

Builder::macro("whereHas{$studlyField}", function ($callable) use ($field) {
Expand Down

0 comments on commit 3ac7509

Please sign in to comment.