state_machines icon indicating copy to clipboard operation
state_machines copied to clipboard

Repetetive DSL in some cases with separate after_transition vs. including do: in the transition

Open rosskevin opened this issue 10 years ago • 3 comments

It appears that I cannot specify do: on a transition definition such as

      event :expire do
        transition :active => :expired, do: :process_superceded, if: [:has_next?, :ended?]
        transition :trial => :trial_expired, do: :process_trial_expiration, if: :ended?
        transition :active => :grace, do: :process_grace, if: :within_grace_period?
        transition [:active, :grace] => :expired, do: :process_expired, if: :exceeded_grace_period?
      end

If that's the case, I'll have to copy a very similar block to create after_transitions. This appears to be the only way:

      event :expire do
        transition :active => :expired, if: [:has_next?, :ended?]
        transition :trial => :trial_expired, if: :ended?
        transition :active => :grace, if: :within_grace_period?
        transition [:active, :grace] => :expired, if: :exceeded_grace_period?
      end

      after_transition :active => :expired, do: :process_superceded
      after_transition :trial => :trial_expired
      after_transition :active => :grace, do: :process_grace
      after_transition [:active, :grace] => :expired

This seems quite verbose and prone to error upon changes since I'm explicitly mirroring state transitions defined above. Am I correct in my understanding? Is there a reason not to allow do: on the transition?

Actually, there's a nuance that I can capture with the desired syntax, that I haven't with the second syntax: only run process_superceded when [:has_next?, :ended?], so do I need to copy those conditions too? The unexpected behavior comes with the potential for :active => :expired without meeting those conditions.

rosskevin avatar Apr 20 '15 15:04 rosskevin

This is a potential enhancement if others like the idea, or we can close this. I'd rather not have the repetetive DSL, yet I'm not too motivated by the issue to dive in and write code to solve it.

rosskevin avatar Aug 05 '15 15:08 rosskevin

I love this idea.

But for now, I learn this gem syntax. So I will not write a pull request shortly.

gagalago avatar Sep 21 '15 14:09 gagalago

Yeah I think this is a nice idea.

jponc avatar Feb 20 '18 07:02 jponc