8000 run update_resource inside a transaction to avoid autosaving relationships through assign_attributes when the record is invalid by deivid-rodriguez · Pull Request #7437 · activeadmin/activeadmin · GitHub
[go: up one dir, main page]

Skip to content

run update_resource inside a transaction to avoid autosaving relationships through assign_attributes when the record is invalid #7437

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

Merged

Conversation

deivid-rodriguez
Copy link
Member

A copy of #6202, to not let it be forgotten.

Fixes #5430

When submitting a form with invalid attributes, some associations are autosaved via assign_attributes before calling save_resource. As a consequence, when updating a resource, the user sees a validation error and is not aware that the changes to the associations got persisted.

This PR runs the code inside the update_resource method in an ActiveRecord::Transaction so that everything is rolled back when the model is not valid.

@javierjulio javierjulio force-pushed the 5430_fix_autosaving_relationships_on_invalid_resource branch from c4bc6c4 to 50a9275 Compare March 12, 2023 02:15
@javierjulio javierjulio self-requested a review March 12, 2023 02:42
Copy link
Member
@javierjulio javierjulio left a comment

Choose a reason for hiding this comment

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

Thanks David! Not sure if you wanted to get this in as is or with tests.

@javierjulio javierjulio force-pushed the 5430_fix_autosaving_relationships_on_invalid_resource branch from 50a9275 to ab27f84 Compare March 12, 2023 23:46
@codecov
Copy link
codecov bot commented Mar 12, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.11%. Comparing base (6baaefd) to head (0c7167e).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7437   +/-   ##
=======================================
  Coverage   99.11%   99.11%           
=======================================
  Files         141      141           
  Lines        4064     4076   +12     
=======================================
+ Hits         4028     4040   +12     
  Misses         36       36           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@matgaw
Copy link
matgaw commented Apr 10, 2023

@deivid-rodriguez @javierjulio is this planned to be merged at some point? it solves an important issue

@thibaudgg
Copy link
Contributor

I also just encountered that issue, this fix is important and should be merged.

@ggstroligo
Copy link
ggstroligo commented Sep 20, 2024

Is this PR going to be merged, or is there another fix for this issue already in the codebase?

@deivid-rodriguez
Copy link
Member Author

Hello, sorry for the radio silence here. I'll try to find some time to re-evaluate this in the following weeks!

@deivid-rodriguez
Copy link
Member Author

Is this PR going to be merged, or is there another fix for this issue already in the codebase?

Are you experiencing this problem? I personally don't know if it has been fixed independently, but if you're experiencing the issue, I think you're very well equipped to verify it!

@mgrunberg
Copy link
Contributor

Is this PR going to be merged, or is there another fix for this issue already in the codebase?

Are you experiencing this problem? I personally don't know if it has been fixed independently, but if you're experiencing the issue, I think you're very well equipped to verify it!

@deivid-rodriguez I manage to replicate the original issue in v4 branch adding some models to the template app. This confirms the errors hasn't been fixed.

I want to write a feature test about the change. I don't have time now but should have it in the following days.

@mgrunberg mgrunberg force-pushed the 5430_fix_autosaving_relationships_on_invalid_resource branch 5 times, most recently from c3cbefd to ca0c7a5 Compare September 26, 2024 21:15
@mgrunberg mgrunberg force-pushed the 5430_fix_autosaving_relationships_on_invalid_resource branch 2 times, most recently from 1f00911 to a12c203 Compare September 26, 2024 21:28
@mgrunberg mgrunberg force-pushed the 5430_fix_autosaving_relationships_on_invalid_resource branch 2 times, most recently from 27365d4 to 325f093 Compare September 26, 2024 21:35
…ships through assign_attributes when the record is invalid
@mgrunberg
Copy link
Contributor

@deivid-rodriguez I found time to write the spec.

I rebased the PR. The first commit shows the failure and the second one, a cherry-pick of the original code, fixes it.

About the spec. It involved a new model so there are a lot of new files just for this. It may be the wrong call but I considered it cleaner than trying to find a way to mimic the issue scenario with existing models (for example: using Post and Tag).

Now we have Company with a has_and_belongs_to Store. The feature test changes the Company to a state where validations fail (name can't be blank) and the test expects stores changes to be rollbacked.

@javierjulio
Copy link
Member

@mgrunberg than 10000 k you. Did you consider or try doing a controller or unit test instead for this? Cucumber adds significant overhead that makes it hard to follow. Then again I don't know how easy it is to do in a controller or unit test, it would just be ideal if we could I think.

@mgrunberg
Copy link
Contributor

@mgrunberg thank you. Did you consider or try doing a controller or unit test instead for this? Cucumber adds significant overhead that makes it hard to follow. Then again I don't know how easy it is to do in a controller or unit test, it would just be ideal if we could I think.

@javierjulio I didn't consider a controller test. No strong argument here. I simply went for a feature test. It should be possible to add a spec in spec/unit/resource_controller_spec.rb (at least I can't think a blocker) but I won't have time to do this for a few days. If that's not a problem I'm ok on give it a try to controller specs.

Just one question, are you fine with the introduction of the new modals?
Because even if I replace the feature spec with a controller one I still need the new model, migrations, etc. The amount of new files won't decrease.

@javierjulio
Copy link
Member

@mgrunberg no worries, take your time. Thank you. Yes the models, migrations, etc. are ok I just prefer to avoid Cucumber if we can. Although feel free to leave the feature spec as is since it's already written. I just think a unit or controller test would be easier to understand and review if we can. Thank you again.

@mgrunberg
Copy link
Contributor

@javierjulio I wrote the controller spec. Let me know what you think

Copy link
Member
@javierjulio javierjulio left a comment

Choose a reason for hiding this comment

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

Thanks!

@mgrunberg mgrunberg enabled auto-merge (squash) September 30, 2024 20:53
@mgrunberg mgrunberg merged commit fc5bcdd into master Sep 30, 2024
22 checks passed
@mgrunberg mgrunberg deleted the 5430_fix_autosaving_relationships_on_invalid_resource branch September 30, 2024 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Associations are persisted before validation of main record
6 participants
0