Skip to content
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

Support passing event name as context to save upon firing of an event #798

Open
alecvn opened this issue Oct 12, 2022 · 0 comments
Open

Comments

@alecvn
Copy link

alecvn commented Oct 12, 2022

In order to run validations specific to certain events I've been using the following pattern:

aasm :state do
  state :state1
  state :state2
  state :state3
  
  event :myevent1 do
    before do
      class_eval do
        validate :some_custom_validation
      end
    end
    transitions from: :state1, to: :state2
  end
    
  event :myevent2 do
    before do
      class_eval do
        validate :some_custom_validation
      end
    end
    transitions from: :state2, to: :state3
  end
  
end

This works just fine but it has always felt a little clunky.

I've recently come across a blog post discussing validation contexts and it immediately made me think of aasm.

It would be really nice if one could configure your events to fire with a context equal to the event name - and/or to override the context passed to save.

This will allow one to end up with something like:

validate :some_custom_validation, on: [ :myevent1, :myevent2 ]

aasm :state, event_as_context: true do
  state :state1
  state :state2
  state :state3
  
  event :myevent1 do
    transitions from: :state1, to: :state2
  end
    
  event :myevent2 do
    transitions from: :state2, to: :state3
  end
  
end

Is this something that would be a useful contribution or am I barking up the wrong tree and should be handling things differently?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant