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

Add Attribute#changed? with to: and from: options #295

Merged
merged 5 commits into from
Feb 9, 2020
Merged

Add Attribute#changed? with to: and from: options #295

merged 5 commits into from
Feb 9, 2020

Conversation

wout
Copy link
Contributor

@wout wout commented Feb 6, 2020

Adds:

  • #changed? with optional from: and to: arguments
  • #changes
  • #original_value

@wout
Copy link
Contributor Author

wout commented Feb 6, 2020

I kept focus: true on the Attribute specs and the two specs with issues. Here is a proposal with passing specs:

https://github.com/wout/avram/tree/attribute-changed-with-explicit-nil/

But this might not be the ideal solution as it treats setting a nil value as change, even if the original value is nil too.

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

I haven’t been at my computer all day so haven’t done a deep dive but I don’t think we need to match the old behavior exactly. I think if the original value is nil and you set the value to nil again that changed? Should be false. Does that answer the question?

If not lmk and I’ll try to dive in deeper later :D

Thank you so much for this. This is SUPER exciting

spec/attribute_spec.cr Show resolved Hide resolved
src/avram/attribute.cr Outdated Show resolved Hide resolved
src/avram/attribute.cr Outdated Show resolved Hide resolved
@wout
Copy link
Contributor Author

wout commented Feb 7, 2020

I think if the original value is nil and you set the value to nil again that changed? Should be false. Does that answer the question?

With the original behaviour, setting any value would make changed? return true, regardless of its contents. That's why I created the second PR (attribute-changed-with-explicit-nil branch) where setting nil, when the original value was nil, results in changed? returning true, because the two mentioned specs (spec/polymorphic_spec.cr:112 and spec/json_column_spec.cr:20) would otherwise fail.

But it does not feel right to do it that way, just to make specs pass.Unless you disagree, of course. 😄

@paulcsmith
Copy link
Member

I didn’t notice the other PR! Oops. Yes I agree the old behavior did not make sense. Better to only return true if the value actually changed and change the specs that broke due to the new behavior.

@wout
Copy link
Contributor Author

wout commented Feb 7, 2020

Ok, so now everything passes, except for one spec, which I temporarily set to pending. That is a situation where a jsonb column has a default value and a record is created with a explicit nil value. Because, as we discussed before, setting nil on an attribute that was initialized with a nil value does not result in changed? being true, this test fails.

So for this to pass, we should either:

  • load the default value from the database and use it as an initial value in an Avram::Attribute if the record has not yet been persisted (if at all possible in a performant way)
  • remove this test (as the whole point of a default value on a column is avoiding empty/nil values)

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

I think you could fix the spec by setting the box to a json value. Then updating it to nil. Lmk if that fixes it! This is lookin hood

@wout
Copy link
Contributor Author

wout commented Feb 8, 2020

Here you go. Now all specs run green. :)

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

Looks great. Thanks for adding this. This will make things like auditing a lot easier!

@paulcsmith paulcsmith merged commit 3ce4635 into luckyframework:master Feb 9, 2020
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.

2 participants