-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get transitionable states from state on Model #17
Conversation
This allows to get an array with possible toTransitions from the state and current from field. This array can for example be used in forms: $allowedTransactions = $payment->getAllowedTransitionsFrom('state', Created::class)
Could you give an example of when it would be useful to know the allowed transitions? |
I'm ok with adding this. Please check my review, also don't forget to add docs for this feature in the docs folder. |
Thanks for all the changes! One more question, would it be possible to also allow this? $transitionableStates = $payment->state->transitionableStates(); |
I agree, that would be a nicer syntax to use. I will look into it the coming days. |
Thank you very much. Great PR up until now! |
… docs for transitionableStates() feature
It's unfortunate that we'd have to do public function __construct(Model $model)
{
$this->model = $model;
$this->stateConfig = $model::getStateConfig()[get_parent_class($this)];
} Than the public function transitionableStates(): array
{
return $this->model->transitionableStates(get_class($this), $this->stateConfig->field);
} There are some issues with this approach still (an infinite loop for one), but I believe these could be solved. @bjrnblm if you're not up for it, feel free to let me know, I'll be happy to merge your PR in as is (after you made the last changes I requested), and I'll give it a go myself. |
Scratch that, it wouldn't work. I made a wrong assumption |
@brendt Yes, I was also struggling with this today. The state does not know to which key it is assigned to in the For now I copied your code from the Tried to work around this but got stuck on several occasions, so it seems like a good idea to fix the docblocks etc and look at the problem later with a fresh mind. |
Hi @brendt anything I can do for this to PR to get it merged? |
Oh sorry, I lost track of this one. I'll do my best to merge it later today! |
Ooh, this looks like a great addition! 👏 |
Thanks again, sorry for the delay. It's released as 1.3.0: https://github.com/spatie/laravel-model-states/releases/tag/1.3.0 |
It would be nice to get allowedTransitions from the current state in a model with HasStates enabled so we can use that to fill for example a dropdown in a form.
Example code:
Article.php
Code:
Result:
Improvements
Not completely happy with the end result, would prefer it if:
Could be:
So we get the transition classes instead of a string and we can do stuff like:
However I cannot get the tests to work that way, mainly because in
HasStates
the methodgetStateConfig
isprivate
.So I am not sure how to load it from the
Payment
model inside the test.Any input on this?