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

How to rollback transaction when using compose #384

Closed
lulalala opened this issue Oct 6, 2016 · 5 comments · Fixed by #385
Closed

How to rollback transaction when using compose #384

lulalala opened this issue Oct 6, 2016 · 5 comments · Fixed by #385
Assignees

Comments

@lulalala
Copy link
Contributor

lulalala commented Oct 6, 2016

Is it possible to rollback if I use compose and outcome is invalid?

def execute
  Foo.transaction do
    compose(A)
    compose(B)
    raise ActiveRecord::Rollback if invalid?
  end
end

Currently compose would throw us out of the transaction when outcome is invalid, thus not rolling back.

My work around is to have a variation of compose, so execution continues.

    def compose_without_throw(other, *args)
      outcome = other.run(*args)

      unless outcome.valid?
        errors.merge!(outcome.errors)
      end

      outcome.result
    end

Oh and thanks for the hard work! I met you at lunch in RubyKaigi last month :)

@AaronLasseigne
Copy link
Owner

Yup, I remember you. 😃

I switched the code a while back from raise to throw because it's faster and it seemed more appropriate because I was thinking of this as an internal trigger inside the gem. I didn't account for the fact that it would not work with transactions.

It seems like the prudent thing to do is switch that back.

@tfausak
Copy link
Collaborator

tfausak commented Oct 10, 2016

Yeah, sounds like we should revert #326. I didn't think of transactions at the time.

@lulalala
Copy link
Contributor Author

Just thow in some idea: would having both compose and compose! be a good idea? the ! would raise error.

@AaronLasseigne
Copy link
Owner

I think we can just throw the error. I don't think this is something the user needs to concern themselves with. It should just work. I have a fix, I'm just working on writing a test for it. It should be out soon.

@lulalala
Copy link
Contributor Author

Thanks!

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

Successfully merging a pull request may close this issue.

3 participants