Prevent going to an invalid transition when using setter directly
Hi @amatsuda !
I've been playing around with this gem and I really like how AR enum is extended and how that brings us state_machine core functionality while still making use of an Integer column in the DB side.
WHY
There should be something that prevents records from transitioning to an invalid state through default AR methods like update(status: 1), status = 1
HOW
- Extended
ARenum setters Desiredstate is being validated againststateful_enum.possible_states- If
stateis not included in thepossible_statesarray an exception is thrown - This transition validation is only executed when:
- record's
current stateis not blank desiredstate is different thatcurrent state
- record's
Also, I fixed/added tests.
P.S. This PR does not add support for triggering before/after callbacks when using AR setters
Please, let me know you thoughts about this.
Thank you
@jairovm Thank you for a fabulous patch!
I guess I totally understand where you came from, and yes I'm also hoping to solve that problem.
However, I generally don't want my models to raise an error when I just put an attribute value. IOW, immediately running a validation with an attribute write doesn't seem like a regular Rails way.
How about changing the implementation to just automatically add a simple model validation?
@amatsuda thanks for taking the time to review this.
It's funny that after having this fix running for a few days in our rails app my team and I decided to move this patch to a rails validation 👍
So yes, I'll work on that and let you know when it's ready.
@jairovm 👍👍👍
Hi @amatsuda!
I'd like to continue working on these changes, please let me know your comments when you get a chance 👌🏼
Thanks for your time 😃