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

Update AASM compiler to handle custom name and namespace #1387

Merged
merged 3 commits into from
Feb 9, 2023

Conversation

jeffcarbs
Copy link
Contributor

@jeffcarbs jeffcarbs commented Feb 8, 2023

Motivation

When trying to replace our home-rolled DSL compiler for AASM, we realized the tapioca one does not handle two features of AASM we're relying on:

  • using a custom name (i.e. aasm(:status) { }).
  • using a namespace (i.e. aasm(namespace: :foo) { }).

This PR adds tests for those and updates the compiler to support them.

Implementation

When you call SomeClass.aasm you're actually implicitly calling SomeClass.aasm(:default) which tells AASM to create a state machine with the name :default. This means that if you define your state machine like aasm(:something), it's saved under the name :something so when you call aasm (again, which is really aasm(:default)) you're going to see an "empty" state machine with no states or events because you're looking at a totally different state machine.

Here's the source: https://github.com/aasm/aasm/blob/2e952ff2088082c9f00c95e3c1261afc826f737c/lib/aasm/aasm.rb#L27-L64

This PR updates the compiler to look at all state machines for a given constant when gathering the states and events for which to generate RBI.

If you're using a namespace:

  • for states, the namespace is added to some_state? and STATE_SOME_STATE (source)
  • for events, methods are created with and without the namespace (source)

Tests

I added failing specs, then in the second commit wrote the code to make them pass.

@jeffcarbs jeffcarbs requested a review from a team as a code owner February 8, 2023 05:18
@paracycle paracycle added the enhancement New feature or request label Feb 8, 2023
@jeffcarbs jeffcarbs changed the title Update AASM compiler to handle custom name Update AASM compiler to handle custom name and namespace Feb 8, 2023
Comment on lines +155 to +157
aasm do
state :existing, initial: true
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a default state machine here as well, to test a class having multiple state machines.

Copy link
Member

@paracycle paracycle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@paracycle paracycle merged commit c85c400 into Shopify:main Feb 9, 2023
@jeffcarbs jeffcarbs deleted the aasm branch February 10, 2023 05:07
@shopify-shipit shopify-shipit bot temporarily deployed to production February 21, 2023 12:01 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants