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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for discard 1.1.0 #3202

Merged
merged 2 commits into from May 9, 2019

Conversation

kennyadsl
Copy link
Member

@kennyadsl kennyadsl commented May 7, 2019

Description

This PR fixes a bug with the last version of discard. I think the bug was more related to how we were using discard than to the discard update itself.

TLDR;

discarded? was returning true, even when the record updated to deleted_at wasn't yet persisted. This behavior seems to be changed (probably correctly?) so this validation

validate :validate_no_amount_used, if: :discarded?

was never performed on the first discard call, which is exactly what we need.

This PR changes that way to validate the record into a before_discard callback (similar to what we have with the after_destroy), throwing abort to interrupt the callback chain.

Asking for a @jarednorman review as well, since he's the new discard maintainer. 馃帀

Checklist:

We already had this but with the last version of discard the validation
is no more performing.

I think it's something related to this commit
jhawthorn/discard@dfdd984
which is included in the last release.

Before the release, calling discarded? on the line:

validate :validate_no_amount_used, if: :discarded?

was returning true, even if the record change wasn't yet persisted. We
don't need that line anymore since now records can't be discarded
without passing the after_discard validation so we should not have any of
those. Anyway, even if that happens we don't want to block other updates
on those records.
@kennyadsl kennyadsl added the type:bug Error, flaw or fault label May 7, 2019
@kennyadsl kennyadsl requested a review from jarednorman May 7, 2019 15:52
@kennyadsl kennyadsl self-assigned this May 7, 2019
This is needed to avoid discard to set deleted_at anyway.

Now that we are not running this method as a validation anymore we need
to stop the callback chain by throwing abort, see:

https://api.rubyonrails.org/classes/ActiveModel/Callbacks.html
@kennyadsl kennyadsl force-pushed the kennyadsl/discard-fix-1.1.0 branch from 9538146 to 892e5dc Compare May 7, 2019 16:03
@kennyadsl
Copy link
Member Author

@jarednorman do you think we need to update the discard dependency along with this change? I'm not sure the old discard version is compatible with this new code. Let me know!

@jarednorman
Copy link
Member

In reviewing the discard gem, I'm not sure what caused this change. The only change I see to how discarded? works since v1.0.0 is this one, which doesn't seem like it should change the behaviour.

Copy link
Contributor

@ericsaupe ericsaupe left a comment

Choose a reason for hiding this comment

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

LGTM assuming it is backwards compatible. If not we'll need to update the discard version in the gemspec

@kennyadsl
Copy link
Member Author

I've just run specs with discard 1.0.0 and they pass so I think it's backward compatible. 馃帀

Investigation that leads me to open jhawthorn/discard/issues/51
I've also tried to run `master` specs with discard `1.0.0` and they pass. Next attempt done is using previous specs and discard `1.1.0`, which had the failure that originated this PR. I changed in discard locally:
run_callbacks(:discard) do
  update_attribute(self.class.discard_column, Time.current)
end

to:

with_transaction_returning_status do
  run_callbacks(:discard) do
    self[self.class.discard_column] = Time.current
    save
  end
end

and specs pass, so I assumed the problem is here. I then changed that block into:

run_callbacks(:discard) do
  self[self.class.discard_column] = Time.current
  save
end

removing with_transaction_returning_status and they still pass, so I assume the problem is with the with_transaction_returning_status change.

From with_transaction_returning_status API reference, it:

Executes method within a transaction and captures its return value as a status flag. If the status is true the transaction is committed, otherwise a ROLLBACK is issued. In any case the status flag is returned.

I think that removing this method somehow changes the state the record is when it's time to execute validations, but I'm not sure if it's an issue worth spending too much time on so I've opened this PR on discard.

I'd say to wait to see if we receive some feedback on that issue otherwise, we can safely merge this.

Copy link
Member

@jarednorman jarednorman left a comment

Choose a reason for hiding this comment

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

Now that I understand the change that caused this to break, I'm in favour of this fix. Thanks for addressing this! 馃馃徎

@kennyadsl kennyadsl merged commit 97a98b6 into solidusio:master May 9, 2019
@kennyadsl kennyadsl deleted the kennyadsl/discard-fix-1.1.0 branch May 9, 2019 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Error, flaw or fault
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants