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

Ability to provide extra fields #70

Closed
wants to merge 3 commits into from
Closed

Conversation

naps62
Copy link
Contributor

@naps62 naps62 commented Jun 23, 2020

I'm trying to incorporate this into a current project. I'm having a problem though. I want to provide atomic transitions where additional fields are required.

E.g: when transitioning from state A to state B, field "extra" is needed on the struct, but I want to update both fields (:state and :extra) in a single query.

This PR is based off of the work @TalkingQuickly did on #54 , but goes a bit further. I added an extra field (defaulting to %{}) to all operations, and that allows me to do this:

def guard_transition(struct, state, extra) do
  changeset = Struct.changeset(extra) |> put_change(:state, state)

  if is_nil(Ecto.Changeset.get_field(changeset, :some_required_field)) do
    {:error, "some_required_field is mandatory when transitioning to #{state}"}
  else
    true
end

def persist(struct, state, extra) do
  {:ok, updated_struct} = 
    Struct.changeset(extra)
    |> put_change(:state, state)
    |> Repo.update()
    
    updated_struct
end

However, this is a slight breaking change, since all the overridable functions now receive an extra argument. Even though it can be ignored, that requires updating existing state machines to include it

However it feels like a safe enough change that provides abilities not possible with the current API

PS: I haven't updated the readme or the documentation yet. I'll wait for feedback on this change before proceeding with that

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 21ca243 on naps62:mp/extra into 61df98d on joaomdmoura:master.

Copy link
Owner

@joaomdmoura joaomdmoura left a comment

Choose a reason for hiding this comment

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

LGTM, I like the idea, don't think we need to care about the breaking change, as you mentioned this add new functionality, and we can cut a new breaking version using SEMVER.

# Code to simulate and force an exception inside a
# guard function.
if Map.get(struct, :force_exception) do
IO.inspect "raising"
Copy link
Owner

Choose a reason for hiding this comment

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

Missing inspect 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whoops! sorry 😄

@joaomdmoura
Copy link
Owner

Fixed conflicts, will review and merge later today, cut a new version later in the week probably since there were other PRs merged as well

@Nuno-Silva-Bose
Copy link

@joaomdmoura do you still intend to merge this PR?

Was checking the PR and extra is mandatory. Shouldn't it be put as optional? There are several scenarios where extra parameters are not needed.

Thanks!

@RodolfoSilva
Copy link

@joaomdmoura is there any chance of this PR be merged?

@kyleboe
Copy link

kyleboe commented Nov 11, 2022

@joaomdmoura are you planning on still maintaining this library? If not, I'd be happy to take over maintenance.

@joaomdmoura
Copy link
Owner

I'm picking up the work on this one oh #85 , I decided to go with another implementation that wont brake any implementation for current projects using this that decide to update.

Because of that I'm closing this in favor of the other one.
This was a great contribution tho, as it was the main inspiration for the work on #85.

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

Successfully merging this pull request may close these issues.

6 participants