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

Default values should not be marked dirty #18

Merged
merged 4 commits into from
Aug 17, 2021

Conversation

markedmondson
Copy link
Contributor

@markedmondson markedmondson commented Jul 16, 2021

Fixes #12

What is the purpose of this pull request?

Don't mark attributes that have a default value as dirty when they haven't actually changed

What changes did you make? (overview)

From https://api.rubyonrails.org/classes/ActiveModel/Type/Value.html#method-i-changed_in_place-3F

Only mark the value as changed if it differs from the read value from the database, before being cast.

Overwrite changes and compare the previous and current values and remove them from the hash if they're the same. It's sloppy but effective.

Is there anything you'd like reviewers to focus on?

This is unfamiliar Rails core territory to me so my confidence is not super high. I tried a few approaches and this seemed to get the closest to where I was expecting to need to make the call but I could be missing something.

Checklist

  • I've added tests for this change
  • I've added a Changelog entry
  • I've updated a documentation (Readme)

@@ -167,7 +167,9 @@ def _define_accessors_methods(store_name, *keys, prefix: nil, suffix: nil) # :no
end

define_method(accessor_key) do
read_store_attribute(store_name, key)
next_value = read_store_attribute(store_name, key)
clear_attribute_change(store_name) unless changed?
Copy link
Owner

Choose a reason for hiding this comment

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

It seems that this change is redundant (or we don't have a test which proves it's required 🙂).

Also, if we still need it, shouldn't we check for this particular store attribute changed only? Currently, we consider all the attributes changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm let me take another look. I think the changed? is what calls changed_in_place? which allows us to do the comparison against the raw DB value.

You might be right about clearing the entire store though, I'll add a test where we change a couple of attributes.

Copy link
Owner

@palkan palkan left a comment

Choose a reason for hiding this comment

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

Thanks, Mark! Great work on figuring this out.

Just one minor comment to resolve, and I'll be ready to release it.

@markedmondson
Copy link
Contributor Author

Ok came at this with a different angle because changes was still flagging the entire accessor as changed if one of the other attributes was changed.

The tests should now cover that too.

@palkan
Copy link
Owner

palkan commented Jul 28, 2021

Awesome.

We have one failing test for Rails 5, could you please take a look? (A quick fix could be to not include the patch for Rails <6—no need to spend a lot of time on supporting old versions 🙂)

@markedmondson
Copy link
Contributor Author

markedmondson commented Aug 11, 2021

Let's see if that works... I think calling it twice actually found a potential issue, I think it got memoized in Rails 6 vs 5.

@palkan palkan merged commit bf24aa4 into palkan:master Aug 17, 2021
@palkan
Copy link
Owner

palkan commented Aug 17, 2021

Thanks a lot! Released in 0.9.0

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.

Reading store attribute with the default value populates changes
2 participants